javascript - Remove previous Directions Route Google Maps -
hi, trying plan route info window, unable remove previous set route. tried setmap(null) didn't work. using callback event listener need pass parameter returned api call. 1 option use new google.maps.map on each event e.g (link) on marker markers placed when page loads.
element = document.getelementbyid('map-canvas'); map = new google.maps.map(element, options); var marker = new google.maps.marker({ position: new google.maps.latlng(car.lat, car.long), map: map, title: car.make }); google.maps.event.addlistener(marker, 'click', directionscallback(car)); function directionscallback(car) { return function (e) { if( infowindow != null ) { infowindow.close(); } var directionsservice = new google.maps.directionsservice(); directionsdisplay = new google.maps.directionsrenderer(); directionsdisplay.setmap(null); console.log(directionsdisplay); calcroute = function(){ var startpoint = document.getelementbyid('clientaddress').value; var finishpoint = new google.maps.latlng(car.lat, car.long); var request = { origin: startpoint, destination: finishpoint, travelmode: google.maps.travelmode.driving }; directionsservice.route(request, function (response, status) { if (status == google.maps.directionsstatus.ok) { directionsdisplay.setmap(map); directionsdisplay.setdirections(response); console.log(directionsdisplay) console.log(response.routes.length) infowindow.close(); } else{ alert('sorry ! ' + givenaddress + ' not recognized') } }); } infowindow = new google.maps.infowindow(); infowindow.setcontent("\n" + 'directions here : <input id = \'clientaddress\' placeholder=\"postcode or city\" type=\'text\'>' + ' <input type=\'button\' onclick=calcroute() value=\'go!\'>') infowindow.open(map, this); } }
define directionsdisplay
1 time(outside of click
-handler)
Comments
Post a Comment