Skip to content

Instantly share code, notes, and snippets.

@daviosa
Last active December 21, 2015 20:28
Show Gist options
  • Save daviosa/6361013 to your computer and use it in GitHub Desktop.
Save daviosa/6361013 to your computer and use it in GitHub Desktop.
Javascript code to handle markers on GoogleMaps API. Keywords: GoogleMaps API, Marker, Overlay, Javascript, GIS
var markersArray = [];
function addMarkerOnClick(mapClient){
google.maps.event.addListener(mapClient, 'click', function(event) {
addMarker(event.latLng);
});
}
function addMarker(mapClient, location) {
marker = new google.maps.Marker({
position: location,
map: mapClient
});
markersArray.push(marker);
}
//Removes the overlays from the map, but keeps them in the array
function clearOverlays() {
if (markersArray) {
for (i in markersArray) {
markersArray[i].setMap(null);
}
}
}
//Shows any overlays currently in the array
function showOverlays(mapClient) {
if (markersArray) {
for (i in markersArray) {
markersArray[i].setMap(mapClient);
}
}
}
//Deletes all markers in the array by removing references to them
function deleteOverlays() {
if (markersArray) {
for (i in markersArray) {
markersArray[i].setMap(null);
}
markersArray.length = 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment