拖动标记时更改方向渲染的位置

时间:2012-10-08 13:55:01

标签: google-maps-api-3 direction

我有一个根据Marker位置在数据库上保存路标的项目,我想在这个标记上使用自定义图标,所以我压制了路线标记(绿色标记)

但是如果我将标记移动到另一个点,它将不会根据标记位置更改渲染线,我想在更改标记位置时“更新”此渲染器。

This is the link to my project with the original version 这是我的代码:(我的尝试编辑)

var map, ren, ser;
var data = {};
var data2 = {};
var marker;
var infowindow;
var directionsDisplay;

var wayA = [];
var wayB = [];
var directionResult = [];

function goma()
{
    var mapDiv = document.getElementById('mappy');   
    var mapOptions = {
    zoom: 12,       
    center: new google.maps.LatLng(-23.563594, -46.654239),
    mapTypeId : google.maps.MapTypeId.ROADMAP
    }
    map = new google.maps.Map( mapDiv, mapOptions );

    google.maps.event.addListener(map, "click", function(event) {
        if (wayA.length == wayB.length) {
        wayA.push(new google.maps.Marker({
      draggable: true,      
          position: event.latLng,
          map: map        
        }));
        } else {
        wayB.push(new google.maps.Marker({
      draggable: true,  
          position: event.latLng,
          map: map  
        }));  

ren = new google.maps.DirectionsRenderer( {'draggable':true, suppressMarkers : true}  );
    ren.setMap(map);
    ren.setPanel(document.getElementById("directionsPanel"));
    ser = new google.maps.DirectionsService();

    ser.route({ 'origin': wayA[wayA.length-1].getPosition(), 'destination':  wayB[wayB.length-1].getPosition(), 'travelMode': google.maps.DirectionsTravelMode.DRIVING},function(res,sts) {
        if(sts=='OK') {
                    directionResult.push(res);
                    ren.setDirections(res); 

                } else {
                    directionResult.push(null);
                }
        });     
    }
 });
}  

3 个答案:

答案 0 :(得分:3)

我有demo可以满足您的需求。

您需要的更改位于

directionsDisplay = new google.maps.DirectionsRenderer( {
'map': map,
'preserveViewport': true,
'draggable':true/*, 
'suppressMarkers' : true */} 
);
directionsDisplay.setMap(map);
directionsDisplay.setPanel(document.getElementById("directionsPanel"));
directionsService = new google.maps.DirectionsService();    
    }
});

注意suppressMarkers' : true已被注释掉

这意味着当显示方向标记时,原始标记仍然存在。这些将使用

删除
function clearOverlays() {
 if (wayA) {
   for (i in wayA) {
   wayA[i].setMap(null);
 }
 }
 if (wayB) {
   for (i in wayB) {
     wayB[i].setMap(null);
  }
}
}

这在

结束时调用
  function calcRoute() {

directionsService.route({ 'origin': wayA[wayA.length-1].getPosition(), 'destination': 
      wayB[wayB.length-1].getPosition(), 'travelMode': 
 google.maps.DirectionsTravelMode.DRIVING},function(response,status) {
    if(status == google.maps.DirectionsStatus.OK) {
                directionResult.push(response);
                directionsDisplay.setDirections(response);  

            } else {
                directionResult.push(null);
            }

})
clearOverlays();
}

答案 1 :(得分:1)

这张地图:

http://www.geocodezip.com/v3_directions_custom_icons_draggable.html

允许您拖动标记并根据新位置重新计算路径。它是在“可拖动方向”之前开发的,因此折线不可拖动。

答案 2 :(得分:1)

在'dragend'标记上添加一个事件监听器函数,重新计算方向。这是一个快速示例,每个都有相同的侦听器,但您可能会将此代码分解为自己的函数,该函数对wayA和wayB标记都进行操作。

google.maps.event.addListener(wayA[wayA.length-1], 'dragend', function() {
    ser.route({ 'origin': wayA[wayA.length-1].getPosition(), 'destination':  wayB[wayB.length-1].getPosition(), 'travelMode': google.maps.DirectionsTravelMode.DRIVING},function(res,sts) {
        if(sts=='OK') {
            rectionResult.push(res);
            ren.setDirections(res); 
        } else  {
            directionResult.push(null);
        }
    })
}

google.maps.event.addListener(wayB[wayB.length-1], 'dragend', function() {
    ser.route({ 'origin': wayA[wayA.length-1].getPosition(), 'destination':  wayB[wayB.length-1].getPosition(), 'travelMode': google.maps.DirectionsTravelMode.DRIVING},function(res,sts) {
        if(sts=='OK') {
            rectionResult.push(res);
            ren.setDirections(res); 
        } else  {
            directionResult.push(null);
        }
    })
}