来自地址的Google Maps StreetView

时间:2015-07-13 13:26:21

标签: google-maps-api-3 google-street-view

当您访问Google地图网络地图服务并搜索地址时,请说“666 5th avenue,New York,NY 10019”,您将获得一张地图,其中包含显示位置的标记,以及各种控件和链接地图的左上角。其中包括街景视图链接。单击它将显示建筑物正面的全景图(在本例中为第5大道)。

但是,如果我使用JavaScript API并想要显示StreetView,则全景图不会显示建筑物的正面。我可以对地址进行地理编码,但是当我得到全景图时,它不会看到建筑物的正面(在这种情况下,它将是距离最近的街道,第52街的建筑物的一侧)。更糟糕的是,如果提供的地址是一个大型场所,例如购物中心,您需要增加50米默认值的容差,并且通常最终会从一条小街道看到。

是否可以从地址获取StreetView全景图,最好是与Google的网络地图服务相同的地理位置,而不是纬度,经度以及API v3的标题或音高?

1 个答案:

答案 0 :(得分:3)

使用this answer to the question: Request main road StreetView panoramas instead of back alleys from API中的代码和您的"示例"地址" 666 5th avenue,New York,NY 10019"给我与谷歌地图相同的结果。

代码段



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>
&#13;
&#13;
&#13;