Last active
July 18, 2018 09:27
-
-
Save getsudocode/42002fab282fab75c58681caec7d43e7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let map; | |
let markersArray = []; | |
// define global variable to store polyline | |
let polyline = null; | |
function initMap() { | |
... | |
map.addListener('click', function(e) { | |
//console.log(e); | |
addMarker(e.latLng); | |
drawPolyline(); | |
}); | |
} | |
function addMarker(latLng) { | |
... | |
} | |
function drawPolyline() { | |
let markersPositionArray = []; | |
// obtain latlng of all markers added on map | |
markersArray.forEach(function(e) { | |
markersPositionArray.push(e.getPosition()); | |
}); | |
// check if there is already polyline drawn on map | |
// remove the polyline from map before we draw new one | |
if (polyline !== null) { | |
polyline.setMap(null); | |
} | |
// draw new polyline at markers' position | |
polyline = new google.maps.Polyline({ | |
map: map, | |
path: markersPositionArray, | |
strokeOpacity: 0.4 | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment