使用移动Google地图移动标记和重绘路径

时间:2017-04-16 03:17:31

标签: javascript google-maps google-maps-api-3

以下代码显示地图中的两个点,并绘制这些点之间的行车路径。我想要实现的是当我移动一个点时,它应该移动标记并重新绘制路径。我怎样才能做到这一点?我很欣赏任何提示。感谢。

function mapLocation() {
  var directionsDisplay;
  var directionsService = new google.maps.DirectionsService();
  var map;

  function initialize() {
    directionsDisplay = new google.maps.DirectionsRenderer();
    var istanbul = new google.maps.LatLng(41.015137, 28.979530);
    var mapOptions = {
      zoom: 7,
      center: istanbul
    };
    map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
    directionsDisplay.setMap(map);
    calcRoute();
  }

  function calcRoute() {
    var start = new google.maps.LatLng(41.01524, 28.975994);
    //var end = new google.maps.LatLng(38.334818, -181.884886);
    var end = new google.maps.LatLng(41.013232, 28.978676);
    var request = {
      origin: start,
      destination: end,
      travelMode: google.maps.TravelMode.DRIVING
    };
    directionsService.route(request, function(response, status) {
      if (status == google.maps.DirectionsStatus.OK) {
        directionsDisplay.setDirections(response);
        directionsDisplay.setMap(map);
      } else {
        alert("Directions Request from " + start.toUrlValue(6) + " to " + end.toUrlValue(6) + " failed: " + status);
      }
    });
  }

  google.maps.event.addDomListener(window, 'load', initialize);
}
mapLocation();

1 个答案:

答案 0 :(得分:1)

您可以将draggable的{​​{1}}选项设置为DirectionsRendererOptions并将其传递给true,以允许用户编辑呈现的路线。

DirectionsRenderer

请参阅documentation