Search code examples
google-maps-api-3direction

Change position of Direction Render when drag Marker


I have a project that save waypoints on the database according with the Marker position, I want to use custom icons on this markers so I supressed the Route Markers (the green markers)

But if I move the marker to another point it will not change the render line according with the marker position, I want to "update" this renderer when I change my marker position.

This is the link to my project with the original version Here is my code: (Edited with my try)

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

Solution

  • I have a demo which does what you are looking for.

    The changes you require are in

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

    Notice suppressMarkers' : true has been commented out

    This means that the origonal markers are still there when the direction markers are displayed. These are removed using

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

    This is called at the end of

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