【发布时间】:2012-11-17 00:06:11
【问题描述】:
用户使用 Google Places API 搜索我的地图,该 API 会向我的地图添加标记。我在每个标记信息窗口内都有一个单击事件,以获取到该标记的方向。 如何将所选标记的坐标传递给我的 google 路线服务请求?
到目前为止,我将var placeLoc = null; 声明为全局变量。我在创建标记函数中定义了placeLoc=place.geometry.location;。然后我有:
function calcRoute() {
var start = myLatLng;
var end = placeLoc;
var request = {
origin: start,
destination: end,
travelMode: google.maps.DirectionsTravelMode.BICYCLING
};
directionsService.route(request, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
}
});
}
该函数将方向呈现给错误的标记。我猜它只是为搜索后返回的数组中的第一个标记提供方向,而不是我选择的实际标记。如何提供路线请求选择标记的坐标?
下面是将搜索结果放入place变量的代码:
google.maps.event.addListener(searchBox, 'places_changed', function() {
var places = searchBox.getPlaces();
// alert("getPlaces returns "+places.length+" places");
for (var i = 0; i < gmarkers.length; i++) {
gmarkers[i].setMap(null);
}
gmarkers = [];
var bounds = new google.maps.LatLngBounds();
for (var i = 0, place; place = places[i]; i++) {
var place = places[i];
createMarker(place);
bounds.extend(place.geometry.location);
}
map.fitBounds(bounds);
// if (markers.length == 1) map.setZoom(17);
});
【问题讨论】:
标签: google-maps-api-3 driving-directions