0
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);
  });
  });
  }

我放置了 2 个可拖动的标记,我必须计算这两个标记之间的路线。

4

1 回答 1

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
  }
});

更多信息 => https://developers.google.com/maps/documentation/javascript/directions

于 2013-05-08T08:34:04.123 回答