Continued from Adding Google Streets View, using Google API V3 instead of V2
Or actually “ops…”
During the testing stage for our application my Team Leader installed the application on another server and tried to run it. He got this pop-up for his trouble:
Message from webpage
This web site needs a different Google Maps API key. A new key can be
generated at http://code.google.com/apis/maps/signup.html.
Now while using the original code I had a problem with one line of code:
- <script src="http://maps.google.com/maps?file=api&v=2&key=DioG342lPJG3WTn3zmQqebsjVg" type="text/javascript"></script>
More precisely the part of: key=DioG342lPJG3WTn3zmQqebsjVg
But I thought to myself – “It works…”
Well the bite in the ass has come…
So what is this key?
It seems Google Map API V2 requires each domain to be registered and then to use an API key. I didn’t want to do that because we had too many servers with different domains (with new environments added all the time).
Getting such a key though is as easy as registering here, but be sure to first read the FAQ (more on that in my previous post).
Google API V3 to the rescue…
In Google API V3 they removed the need for API key, but they also changed the API so that the current JavaScript code doesn’t work.
So I rewrote it to this:
- <script src="http://maps.google.com/maps/api/js?sensor=false" type="text/javascript"></script>
- <script type="text/javascript">
- function streetView(lat, lng) {
- var client = new google.maps.StreetViewService();
- var streetViewLocation = new google.maps.LatLng(lat, lng);
- client.getPanoramaByLocation(streetViewLocation, 50, function (result,
- status) {
- if (status == google.maps.StreetViewStatus.OK) {
- var panoramaOptions = {
- position: streetViewLocation,
- pov: {
- heading: 34,
- pitch: 10,
- zoom: 1
- },
- visible: true,
- addressControl: true
- };
- document.getElementById("GoogleStreetViewContainer").style.display = 'block';
- var panorama = new google.maps.StreetViewPanorama(document.getElementById("GoogleStreetviewObject"), panoramaOptions);
- }
- });
- }
- </script>
Much more simple code:
getPanoramaByLocation – returns status OK for coordinates that have Street View 50 (set in a parameter) meters from them.
Be sure though to place (line 20):
- document.getElementById("GoogleStreetViewContainer").style.display = 'block';
Before calling StreetViewPanorama or you will get an “Out of stack space” error (I opened a bug on this but since the work around as fairly simple it probably won’t be fixed any time soon…).
One last warning the functionality is almost the same. The only difference I could find is that V3 hasn’t implemented Full Screen mode yet (unknown when it will be added or if it will) where V2 has it out of the box.
V2:
V3:
Seems almost the same…
Code sample can be found in my CodePlex Project, or directly here.
Edit (13/09/2011): Fixed Firefox bug in JavaScript
Keywords: Silverlight, Map, Google Street View, Google Map API