使用带有标记的谷歌街景视图,如何将 POV 指向标记?
我有一个简单的街景视图,可以向我显示给定地址的街景视图:
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);
}
});
如您所见,我在街景视图上添加了地址标记。我的问题是,街景指向北,而标记指向南。对于非特定地址,如何指定街景视图应指向该地址的标记,而不是默认指向北?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
原因是街景 POV 默认情况下是拍摄图像时卡车所面向的方向(见图)。您需要获取卡车的位置和房屋的位置,并计算从第一个位置到第二个位置的“航向”,然后将您的街景位置设置为具有刚刚计算的航向的卡车的位置:
使用街景视图的另一个技巧/陷阱是,您需要通过不断增加距离重复调用
getPanoramaByLocation
来获取距离您的地址位置最近的街景视图,直到成功或达到某个最大值距离。我使用这段代码解决了这个问题:The reason for this is that the street view POV is, by default the direction the truck was facing when the image was shot (go figure). You need to get the location of the truck and the location of the house and calculate a "heading" from the first location to the second, then set your street-view location to that of the truck with the heading you just calculated:
Another trick/trap using street view is that you need to obtain the closest street view to your address location by repeatedly calling
getPanoramaByLocation
with an increasing distance until you are either successful or reach some maximum distance. I solve this using this code:请查看此示例。即使它是 V2,您也可以重用该代码。基本上,您需要调用computeAngle(markerLatLng, streetviewPanoLatLng),并将街景全景的偏航设置为返回值。
Check out this sample. Even though its for V2, you can reuse the code. Basically, you'll need to call computeAngle(markerLatLng, streetviewPanoLatLng), and set the Street View pano's yaw to the returned value.
基于您的代码的简单示例:
工作小提琴
工作代码片段:
Simple example based off your code:
working fiddle
working code snippet: