Remove previous route from Google Maps

Asked

Viewed 572 times

2

In an application with multiple locations, I have a call button for a route between two points using the function setDirection, as inserted below.

I would like a support to be able to remove a previously traced route after clicking on another marker.

// Funções de roteamento

var directionsDisplay;
var directionsService = new google.maps.DirectionsService();

function setDirection(destLat, destLng) {
directionsDisplay = new google.maps.DirectionsRenderer({ polylineOptions:{strokeColor:"#4a4a4a",strokeWeight:5}, suppressMarkers:true });
directionsDisplay.setMap(map);

var request = {
    origin: pos.lat + ',' + pos.lng,
    destination: destLat + ', ' + destLng,
    travelMode: google.maps.TravelMode.DRIVING
};

directionsService.route(request, function (result, status) {
    console.log(request);
    if (status == google.maps.DirectionsStatus.OK) {
        directionsDisplay.setDirections(result);
    }
});

return false;
}

Below I inserted a function that shows the map, including the section where you have the button to "Plot the Route"

 // Recarrega marcadores
 function reloadMarkers() {
 var center = map.getCenter();
 setMapOnAll(null);
 map.setCenter(center);


downloadUrl(findUrl, pos, function (data) {
    var xml = data;
    var markers = xml.documentElement.getElementsByTagName("marker");
    for (var i = 0; i < markers.length; i++) {

        var name = markers[i].getElementsByTagName("name")[0].firstChild.nodeValue;
        var address = markers[i].getElementsByTagName("address")[0].firstChild.nodeValue;
        var type = markers[i].getElementsByTagName("type")[0].firstChild.nodeValue;
        var point = new google.maps.LatLng(
                parseFloat(markers[i].getElementsByTagName("lat")[0].firstChild.nodeValue),
                parseFloat(markers[i].getElementsByTagName("lng")[0].firstChild.nodeValue));
        var html = "<b>" + name + "</b> <br/>" + address +
                '<br><a href="#" onclick="return setDirection(' + markers[i].getElementsByTagName("lat")[0].firstChild.nodeValue + ',' + markers[i].getElementsByTagName("lng")[0].firstChild.nodeValue + ');">Traçar Rota</a>';
        var icon = customIcons[type] || {};
        var marker = new google.maps.Marker({
            map: map,
            position: point,
            icon: icon.icon,
            shadow: icon.shadow
        });
        bindInfoWindow(marker, map, infoWindow, html);
    }
});

}

This is the button where I return the 'setDirection' function()':

 <a href="#" onclick="return setDirection(' + markers[i].getElementsByTagName("lat")[0].firstChild.nodeValue + ',' + markers[i].getElementsByTagName("lng")[0].firstChild.nodeValue + ');">Traçar Rota</a>
  • Is there any way to pass the entire code to be tested on Jsfiddle or some other genre site? I want to take the instantiation of the map including.

  • Hello @Giancarloabelgiulian. Yes. I put in Jsfiddle: jsfiddle.net/lucastaglia/82e4qLv8. You can see online how the application works on (http://ondedescarto.com.br). I do not know if it will be possible to see the collection sites depending on their location, as they were determined at a distance of 100km from the user’s location.

2 answers

0

The setMap(null) @Dian Carlos passed is correct, maybe you didn’t put it in the right place.
Try this way:

var directionsDisplay = null;
var directionsService = new google.maps.DirectionsService();

function setDirection(destLat, destLng) {
    if(directionsDisplay != null){
        directionsDisplay.setMap(null);
    }  

I just changed the part of your code that I think is relevant, the rest stays the same

0

Use the function:

directionsDisplay.setMap(null);

  • Dian, thanks for your help, but I’ve already used this function and still the routes aren’t reset. I edited my question better above, putting the code snippet where I try to restart the bookmarks, but without success. If you could give me a scan, that would be helpful. Thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.