谷歌地图V3对齐最近的街道 [英] Google Maps V3 snap to nearest Street
问题描述
当我点击地图时,我想让标记贴近最近的街道。这里有一个很好的例子:
http://econym.org.uk/gmap/ example_snappath.htm (主页: http://econym.org.uk/gmap/ snap.htm )。
但是 - 这个例子适用于谷歌地图版本2.有没有办法在v3中做到这一点?
When I click into a map I want the marker to get snaped to the nearest street. There is a good exmaple here: http://econym.org.uk/gmap/example_snappath.htm (Main Page:http://econym.org.uk/gmap/snap.htm). However - this example is for google maps version 2. Is there a way to do this in v3?
推荐答案
您提供的示例通过执行方向查找获得结果,然后从中获取第一个位置。在api v3中,这是通过以下代码完成的,其中 map 是您的地图名称
The example that you have given gets the result by doing a direction lookup and then getting the first location from it. In api v3 this is accomplished with the following code where "map" is the name of your map
var directionsService = new google.maps.DirectionsService();
google.maps.event.addListener(map, 'click', function(event) {
var request = {
origin:event.latLng,
destination:event.latLng,
travelMode: google.maps.DirectionsTravelMode.DRIVING
};
directionsService.route(request, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
var marker = new google.maps.Marker({
position: response.routes[0].legs[0].start_location,
map: map,
title:"Hello World!"
});
}
});
});
这篇关于谷歌地图V3对齐最近的街道的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!