Skip to content

Instantly share code, notes, and snippets.

@getsudocode
Last active July 18, 2018 09:27
Show Gist options
  • Save getsudocode/42002fab282fab75c58681caec7d43e7 to your computer and use it in GitHub Desktop.
Save getsudocode/42002fab282fab75c58681caec7d43e7 to your computer and use it in GitHub Desktop.
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