Jam*_*eit 4 google-maps-api-3 google-street-view
当您访问 Google 地图网络地图服务并搜索地址(例如“666 5th avenue, New York, NY 10019”)时,您将看到一张地图,其中带有显示位置的标记,以及左上角的各种控件和链接地图的。其中包括街景链接。单击它,您将获得显示建筑物正面的全景图(在本例中为第五大道)。
但是,如果我使用 JavaScript API 并想要显示街景,全景图将不会显示建筑物的正面。我可以对地址进行地理编码,但当我获得全景图时,它不会看到建筑物的正面(在本例中,它将是距最近的十字路口 52 街的建筑物侧面)。更糟糕的是,如果提供的地址是大型场所,例如购物中心,则需要增加默认值 50 米的容差,并且通常最终会看到来自小巷的视图。
是否可以从一个地址(最好是与 Google 网络地图服务提供的地址相同)获取 StreetView 全景图,而不是使用 API v3 获取纬度、经度以及可选的航向或俯仰角?
使用此问题答案中的代码:从 API 请求主要道路 StreetView 全景图而不是后巷,并使用“示例”地址“666 5th avenue, New York, NY 10019”,得到的结果与在 Google 地图上得到的结果相同。
代码片段:
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);
}
}Run Code Online (Sandbox Code Playgroud)
<script src="https://maps.googleapis.com/maps/api/js?libraries=geometry&key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk"></script>
<div id="pano" style="width: 425px; height: 400px;float:left"></div>Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4217 次 |
| 最近记录: |