Я не уверен, что вы хотите получить, но, возможно, вы могли бы использовать DirectionsService.
Глобальные переменные:
var directionDisplay;
var directionsService = new google.maps.DirectionsService();
var startLocation;
var markerStart;
В функции initialize ():
directionsDisplay = new google.maps.DirectionsRenderer();
directionsDisplay.setMap(map);
google.maps.event.addListener(map, 'click', function(event) {
mapZoom = map.getZoom();
startLocation = event.latLng;
setTimeout("placeMarkerStart()", 600);
});
И еще глобальный персонал:
function placeMarkerStart() {
if(mapZoom == map.getZoom()){
markerStart = new google.maps.Marker({
position: startLocation,
map: map,
title: "start"
});
}
}
function calcRoute(num) {
var request = {
origin: markerStart.getPosition(),
destination: markers[num].getPosition(),
travelMode: google.maps.TravelMode.DRIVING
};
directionsService.route(request, function(result, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(result);
}
});
}
Если вы хотите проверить это, вы можете добавить:
<div id="locationField">
...
<input type="button" onclick="calcRoute(0);" id="start" value="0"/>
<input type="button" onclick="calcRoute(1);" id="start" value="1"/>
<input type="button" onclick="calcRoute(2);" id="start" value="2"/>
</div>
... и изменить положение карты:
#map_canvas {
position: absolute;
width: 399px;
height: 399px;
top: 55px;
left: 0px;
border: 1px solid grey;
}
Это только идея, и она не работает идеально, но вы можете восстановить ее, если вам это нравится.