我有一个简单的街景工作,可以显示给定地址的街景:
var geocoder = new google.maps.Geocoder();
var address = "344 Laguna Dr, Milpitas, CA 95035";
geocoder.geocode( { 'address': address},
function(results, status) {
//alert (results);
if (status == google.maps.GeocoderStatus.OK) {
//alert(results[0].geometry.location);
myStreetView = new google.maps.StreetViewPanorama(document.getElementById("map_canvas"));
myStreetView.setPosition(results[0].geometry.location);
var marker = new google.maps.Marker({
position: results[0].geometry.location,
map: myStreetView,
title:address
});
//alert ("yay");
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
如您所见,我在街道视图上添加了地址标记。我的问题是,街景朝北,而标记朝南。对于非特定地址,我如何指定街景视图应指向该地址的标记,而不是默认指向北?
最佳答案
原因是街景POV默认为拍摄图像时卡车所面对的方向(如图)。您需要获取卡车的位置和房屋的位置,并计算从第一个位置到第二个位置的“航向”,然后将街景位置设置为带有刚刚计算出的航向的卡车位置:
// adrloc=target address
// svwloc=street-view truck location
svwService.getPanoramaByLocation(adrloc,svwdst,function(dta,sts) {
if(sts==google.maps.StreetViewStatus.OK) {
var svwloc=dta.location.latLng;
var svwhdg=google.maps.geometry.spherical.computeHeading(svwloc,adrloc);
var svwmap=avwMap.getStreetView();
svwmap.setPosition(svwloc);
svwmap.setPov({ heading: svwhdg, pitch: 0 });
svwMarker=new google.maps.Marker({ map:svwmap, position: adrloc });
svwmap.setVisible(true);
}
else {
...
}
使用街景的另一种技巧/陷阱是,您需要通过以越来越远的距离反复调用
getPanoramaByLocation
直到获得成功或达到某个最大距离,来获得与地址位置最接近的街景。我使用以下代码解决此问题:var SVW_MAX=100; // maximum street-view distance in meters
var SVW_INC=10; // increment street-view distance in meters
var svwService=new google.maps.StreetViewService(); // street view service
var svwMarker=null; // street view marker
// NOTE: avwMap is the aerial view map, code not shown
...
resolveStreetView(avwMap.getCenter(),SVW_INC);
...
var resolveStreetView=function(adrloc,svwdst) {
svwService.getPanoramaByLocation(adrloc,svwdst,function(dta,sts) {
if(sts==google.maps.StreetViewStatus.OK) {
var svwloc=dta.location.latLng;
var svwhdg=google.maps.geometry.spherical.computeHeading(svwloc,adrloc);
var svwmap=avwMap.getStreetView();
svwmap.setPosition(svwloc);
svwmap.setPov({ heading: svwhdg, pitch: 0 });
svwMarker=new google.maps.Marker({ map:svwmap, position: adrloc });
svwmap.setVisible(true);
}
else if(svwdst<SVW_MAX) {
resolveStreetView(adrloc,svwdst+SVW_INC);
}
});
}
关于google-maps - 使用带有标记的Google街景 View ,如何将POV指向标记?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/3198415/