Link to home
Start Free TrialLog in
Avatar of Lmillard
Lmillard

asked on

how to remove markers in google maps v3

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.
Avatar of Albert Van Halen
Albert Van Halen
Flag of Netherlands image

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++)
      map.markers[i].setMap(null); 
}

Open in new window

Avatar of Lmillard
Lmillard

ASKER

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;
// START THE WHOLE MAP BUSINESS GOING
	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

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.
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
// REBUILD THE MAP - CAN BE FIRED FROM ANY ACTION SUCH AS ADD ROUTE, DELETE ROUTE, LOADING PAGE
function rebuildMapCFC(basketId,defaultLatLng) {
	initialize();
	showGmap();
	var mydata = {data:[basketId]};
	$.post(httpPath+"assets/publicComps/basket.cfc", 
		   {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)
	        map.setCenter(latlng);
			totalMarkers = totalMarkers +1;
			thisPos = latlng;
	        map.setCenter(latlng);
			marker = new google.maps.Marker({map: map, position: latlng});
			bounds.extend(latlng);
		}
		if(totalMarkers > 1) {
			map.fitBounds(bounds);
		}
		   } else {
			varLoc = defaultLatLng.split(',');
			myLat = varLoc[0]; myLng = varLoc[1];
			var latlng = new google.maps.LatLng(myLat,myLng)
	        map.setCenter(latlng);
		   }
		});
	}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Albert Van Halen
Albert Van Halen
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Perfect result thank, sorry for the delay in response but I was sidelined for a few days.