0
用戶使用Google地圖API搜索我的地圖,Google地圖API將標記添加到我的地圖。我在每個標記infowindow中都有一個單擊事件來獲取該標記的方向。 如何將所選標記的座標傳遞給我的Google路線服務請求?Google Maps API V3:將標記座標傳遞給谷歌路線服務請求
到目前爲止,我宣稱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);
}
});
}
該函數將方向顯示爲錯誤的標記。我猜測它只是指示搜索後返回的數組中的第一個標記,而不是我選擇的實際標記。如何提供指示請求所選標記的座標?
下面是一個地將搜索結果到地方變量的代碼:
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);
});
我試過這個,它沒有給我指示我選擇的標記。它仍然返回數組中另一個標記的方向。但我認爲marker.getPosition()就是我在代碼中某處丟失的東西。我只需要找出在哪裏, – Rick
我發現這個例子:http://econym.org.uk/gmap/example_map4c.htm我beleive通過這個代碼通過標記位置''';此代碼使用google maps api v2。在V3中會有類似的工作,或者是我應該關注的getPosition()函數? – Rick
終於搞明白了。它與你的答案類似,關鍵是將marker.getPosition放入具有打開infowindow的事件偵聽器的函數中。我認爲,作爲你的功能代碼,它應該足以幫助人們解決這個問題。謝謝 – Rick