Nearest Street View Photo to Coordinates

3k views Asked by At

I'm working on proof-of-concept application using Google Maps V3 API. I have a database with a list of all cities and their center points in (lat,lng) format. When I open a random city from the database, the map is centered to the center point and switches to Street View view.

The problem is: sometimes there are user submitted photos and no "walk" feature in this case. Here is an example of such a photo which is also a part of Street View layer:

enter image description here

Is there a way to avoid these user photos and are they different from Google Maps API standpoint from the real photos made by Google drivers with "walk" feature? I need a way to skip to the next nearest point with "proper" Street View photo from a Google car.

Another question: is there a way/algorithm to find the nearest street view point to the coordinates given?

Here is the current code snippet:

      var point = new google.maps.LatLng(data[0].lat, data[0].lng);

      var webService = new google.maps.StreetViewService();
      var checkaround = 5000;
      webService.getPanoramaByLocation(point,checkaround ,function(panoData) {
        if(panoData){

             if(panoData.location){

                if(panoData.location.latLng){
                      window.map.setCenter(panoData.location.latLng);
                      window.map.setZoom(14);
                      var panoramaOptions = {
                        position: panoData.location.latLng,
                        pov: {
                          heading: 34,
                          pitch: 10
                        }
                      };
                      var panorama = new google.maps.StreetViewPanorama(document.getElementById('pano'), panoramaOptions);
                      map.setStreetView(panorama);
                }
            }
        }
      });

I can't publish it on jsfiddle as there is a php backend with database, but with the code above and a test point (lat,lng) = (54.226978, 49.568459) you can recreate the problem, you'll see the photo and not the Street View. However, this city and this very point is covered by Street View.

2

There are 2 answers

2
Nathan Wilson On BEST ANSWER

The problem might be that your calling getPanoramaByLocation(). It might work if you omit that, and just do this:

  var point = new google.maps.LatLng(54.226978, 49.568459);

  window.map.setCenter(point);
  window.map.setZoom(14);
  var panoramaOptions = {
    position: point,
    pov: {
      heading: 34,
      pitch: 10
    }
  };
  var panorama = new google.maps.StreetViewPanorama(document.getElementById('pano'), panoramaOptions);
  map.setStreetView(panorama);

Or if you want to getPanoramaByLocation() you should call setPano() in the callback like this:

  var point = new google.maps.LatLng(data[0].lat, data[0].lng);

  var panorama = new google.maps.StreetViewPanorama(document.getElementById('pano'));

  var webService = new google.maps.StreetViewService();
  var checkaround = 5000;
  webService.getPanoramaByLocation(point,checkaround ,function(panoData) {
    if(panoData){

         if(panoData.location){

            if(panoData.location.latLng){
                  window.map.setCenter(panoData.location.latLng);
                  window.map.setZoom(14);

                  panorama.setPano(panoDdata.location.pano);
                  panorama.setPov({
                    heading: 34,
                    pitch: 10
                  });
                  panorama.setVisible(true);

                  map.setStreetView(panorama);
            }
        }
    }
  });
1
Verma On

You can try to use Roads API snapToRoads method.

This method takes GPS points collected along a route, and returns with the points snapped to the most likely roads the vehicle was traveling along