views:

198

answers:

1

I am passing lat and lng variables and display google sreet view in a div. The problem is that when the StreetView is unavilable then nothing is displayed. I would like to check if there is a streetview for a given lat and lng and display a message. Here is my code:

var myPano = new GStreetviewPanorama(document.getElementById("street2"), panoOpts);
var location = new GLatLng(lat,lng)
myPano.setLocationAndPOV(location);

Maybe I should use something like: Event.addListener(myPano, "error", errorMessage());

Any ideas?

+3  A: 

You may want to check out the following reference:

Determining whether a road supports Street View by visual inspection of the GStreetviewOverlay is not often feasible, or desirable from a user's perspective. For that reason, the API provides a service which programmatically requests and retrieves Street View data. This service is facilitated through use of the GStreetviewClient object.

Basically you can use the getNearestPanoramaLatLng() method of the GStreetviewClient class, which will return you a GLatLng of the nearest point where street view is available. You can then use the distanceFrom() method to check if the nearest street view point is within a certain threshold from your source point.

Here is a full example, which I believe should be self explanatory:

<!DOCTYPE html>
<html> 
<head> 
    <meta http-equiv="content-type" content="text/html; charset=UTF-8"/> 
    <title>Google Maps API - Street View Availability</title> 
    <script src="http://maps.google.com/maps?file=api&amp;amp;v=2&amp;amp;sensor=false"
            type="text/javascript"></script> 
  </head> 
  <body> 
    <script type="text/javascript"> 
       var testPoint = new GLatLng(40.7140, -74.0062);   // Broadway, New York
       var svClient = new GStreetviewClient();

       svClient.getNearestPanoramaLatLng(testPoint, function (nearest) {
          if ((nearest !== null) && (testPoint.distanceFrom(nearest) <= 100)) {
             alert('Street View Available');             // Within 100 meters
          }
          else {
             alert('Street View Noet Available');        // Not within 100 meters
          }
       });
    </script> 
  </body> 
</html>
Daniel Vassallo
Really nice, thanks a lot!
Vafello