Google Maps StreetView from Address

℡╲_俬逩灬. 提交于 2019-11-28 01:23:23

问题


When you go to Google Maps web mapping service and search for an address, say "666 5th avenue, New York, NY 10019" you will be served a map with a marker showing the location, along with various controls and links in the upper left of the map. Among those are the Street View link. Clicking it you will get a panorama showing the front of the building (from 5th Avenue in this case).

However, if I am using the JavaScript API and want to show the StreetView, the panorama will not show the front of the building. I can geocode the address, but when I get a panorama, it will not be looking at the front of the building (in this case, it will be the side of the building from the nearest cross-street, 52nd Street). To make matters worse, if the supplied address is a large venue, such as a shopping mall, you will need to increase the tolerance from the 50 meter default, and you usually end up with the view from a small side street.

Is it possible to obtain a StreetView panorama from an address, preferably the same one that Google's web mapping service will provide, rather than a latitude, longitude, and optionally a heading or pitch with the API v3?


回答1:


Using the code from this answer to the question: Request main road StreetView panoramas instead of back alleys from API with your "example" address of "666 5th avenue, New York, NY 10019" gives me the same result I get on Google Maps.

code snippet:

var sv = new google.maps.StreetViewService();
var geocoder = new google.maps.Geocoder();
var directionsService = new google.maps.DirectionsService();
var panorama;
var address = "666 5th avenue, New York, NY 10019";
var myLatLng;

function initialize() {

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

  geocoder.geocode({
    'address': address
  }, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
      myLatLng = results[0].geometry.location;

      // find a Streetview location on the road
      var request = {
        origin: address,
        destination: address,
        travelMode: google.maps.DirectionsTravelMode.DRIVING
      };
      directionsService.route(request, directionsCallback);
    } else {
      alert("Geocode was not successful for the following reason: " + status);
    }
  });
}
google.maps.event.addDomListener(window, 'load', initialize);

function processSVData(data, status) {
  if (status == google.maps.StreetViewStatus.OK) {

    panorama.setPano(data.location.pano);

    var heading = google.maps.geometry.spherical.computeHeading(data.location.latLng, myLatLng);
    panorama.setPov({
      heading: heading,
      pitch: 0,
      zoom: 1
    });
    panorama.setVisible(true);

  } else {
    alert("Street View data not found for this location.");
  }
}

function directionsCallback(response, status) {
  if (status == google.maps.DirectionsStatus.OK) {
    var latlng = response.routes[0].legs[0].start_location;
    sv.getPanoramaByLocation(latlng, 50, processSVData);
  } else {
    alert("Directions service not successfull for the following reason:" + status);
  }
}
<script src="https://maps.googleapis.com/maps/api/js?libraries=geometry"></script>
<div id="pano" style="width: 425px; height: 400px;float:left"></div>


来源:https://stackoverflow.com/questions/31384664/google-maps-streetview-from-address

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!