how to remove markers in google maps v3

Posted on 2011-03-03
Medium Priority
Last Modified: 2012-05-11
I have spend quite some time searching for the answer to this and it seems to be a depreciated functionality for V3 and I can't find a clear exact way to deal with this so I though i  ought to run it past the experts!

Basically I have a map rebuild routine that runs when a route part is removed from a structure of route parts so I simply need to remove the corresponding marker from the map.

any help appreciated.
Question by:Lmillard
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
LVL 19

Expert Comment

by:Albert Van Halen
ID: 35025065
Use this
// method to clear all markers
// usage : clearMarkers(map);
// arguments : map - instance of the map

function clearMarkers(map) {
   var markCount = map.markers.length;
   for(var i = 0; i < markCount; i++)

Open in new window


Author Comment

ID: 35025232
Looks like it should do what I want but for some reason it wont run in my rebuild routine.
I am initializing the initial map using the following code so not sure if I am creating a named instance.
Also this removes all markers, is there a way to remove just one?

var geocoder;
	var map;
	var bounds = new google.maps.LatLngBounds();
	var totalMarkers = 0;
	function initialize() {
		geocoder = new google.maps.Geocoder();
		var latlng = new google.maps.LatLng(51.397, -1.0);
		var myOptions = {
		  zoom: 8,
		  center: latlng,
		  mapTypeId: google.maps.MapTypeId.ROADMAP
		map = new google.maps.Map(document.getElementById("gMapScreen"), myOptions);

Open in new window

LVL 19

Expert Comment

by:Albert Van Halen
ID: 35025460
You have your map variable defined globally so that's good.
Can you show your rebuild routine or provide a link to your site ?

The method indeed remove all markers by iterating through the markers array of the map.
If you know specific properties of a marker that needs to be deleted you can rewrite the routine by iterating through the markers and delete the ones that meet the criteria.
Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.


Author Comment

ID: 35025573
My site is on my localhost dev server at the moment so not possible to show that directly but here is a copy of my rebuild code.
At the moment I am running the initialize() function at the top to destroy the existing map and recreate from scratch but this is a bit heavy handed.
Could advise on how to add a unique id to a marker to allow for single deletion?
Sorry for the daft questions but I am very new to google maps.

showGmap is just a script that slides down the map screen so no real effect on the whole thing.
the ajax post returns a json array of route lat/lng from session vars in the form or GeoArray
function rebuildMapCFC(basketId,defaultLatLng) {
	var mydata = {data:[basketId]};
		   {method:"getBasketRoute",argumentCollection:$.toJSON(mydata), returnFormat:"json"}, function(geoArray) {

	var geoArray = $.evalJSON(geoArray);
	if(geoArray.length) {
	for (var i=0; i < geoArray.length; i++){
			varLoc = geoArray[i].split(',');
			myLat = varLoc[0]; myLng = varLoc[1];
			var latlng = new google.maps.LatLng(myLat,myLng)
			totalMarkers = totalMarkers +1;
			thisPos = latlng;
			marker = new google.maps.Marker({map: map, position: latlng});
		if(totalMarkers > 1) {
		   } else {
			varLoc = defaultLatLng.split(',');
			myLat = varLoc[0]; myLng = varLoc[1];
			var latlng = new google.maps.LatLng(myLat,myLng)

Open in new window

LVL 19

Accepted Solution

Albert Van Halen earned 2000 total points
ID: 35034242
I'm not quite sure if you can specify an alternative property in the marker object; I wouldn't do it -> tampering with 3rd party objects...
You could store each marker into an array of custom objects having an id and the marker.

Te delete a specific marker, loop through your array, find the specific id and delete the marker.
var markers = []; // create the array
for(var x = 0; x < 10; x++) {
   var marker = new google.maps.Marker({map: map, position: latlng}); // create a new marker
   markers.push({ "id": x, "marker": marker }); // store object (id, marker) in the array

// if you want to delete a specific marker, do it like this

// usage : deleteMarker(3)
// arguments : id - the id of the marker to delete
function deleteMarker(id) {
   var markCount = markers.length;
   for(var x = 0; x < markCount; x++) {
      if(markers[x].id == id) {
         var markerToDelete = markers.splice(x,1)[0].marker;

Open in new window


Author Closing Comment

ID: 35098191
Perfect result thank, sorry for the delay in response but I was sidelined for a few days.

Featured Post

Get MongoDB database support online, now!

At Percona’s web store you can order your MongoDB database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card. Handle your MongoDB database support now!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Originally, this post was published on Monitis Blog, you can check it here . It goes without saying that technology has transformed society and the very nature of how we live, work, and communicate in ways that would’ve been incomprehensible 5 ye…
Australian government abolished Visa 457 earlier this April and this article describes how this decision might affect Australian IT scene and IT experts.
This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…
Suggested Courses

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question