function initialize()
{
var mapOptions = {
zoom: 7,
center: new google.maps.LatLng(21.7679, 78.8718),
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
var count=0;
var isCreateHeadPoint = true;
var headMarker, tailMarker;
google.maps.event.addListener(map, 'click', function(e) {
if(count<2)
{
if(isCreateHeadPoint)
{
var headmarker = new google.maps.Marker({
position: e.latLng,
draggable:true,
map: map
});
isCreateHeadPoint=false;
count++;
}
else
{
var tailmarker = new google.maps.Marker({
position: e.latLng,
draggable:true,
map: map
});
count++;
}
}
google.maps.event.addListener(headmarker,'click', function(event)
{
infowindow = new google.maps.InfoWindow({
map: map,
content: "coordinates:"+event.latLng.toUrlValue(),
position: event.latLng,
});
infowindow.open(map, headmarker);
});
google.maps.event.addListener(tailmarker,'click', function(event)
{
infowindow = new google.maps.InfoWindow({
map: map,
content: "coordinates:"+event.latLng.toUrlValue(),
position: event.latLng,
});
infowindow.open(map, tailmarker);
});
});
}
我已经放置了两个可拖动的标记,我必须计算这两个标记之间的路径。
答案 0 :(得分:1)
您可以执行以下操作:
var directionsService = new google.maps.DirectionsService()
, directionsDisplay = new google.maps.DirectionsRenderer({
/* options */
})
, origin = new google.maps.LatLng(origin.latitude, origin.longitude)
, destination = new google.maps.LatLng(destination.latitude, destination.longitude)
, // request options
request = {
origin: origin,
destination: destination,
travelMode: google.maps.TravelMode.DRIVING
};
// set your map to the directions Display
directionsDisplay.setMap(/* your map */);
// route it (passing request options as 1st parameter)
directionsService.route(request, function(response, status) {
if (status === google.maps.DirectionsStatus.OK) { // if it can route it
directionsDisplay.setDirections(response); // show route
}
});
更多信息=&gt; https://developers.google.com/maps/documentation/javascript/directions