Solved

how to remove markers in google maps v3

Posted on 2011-03-03
6
1,382 Views
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.
0
Comment
Question by:Lmillard
[X]
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
6 Comments
 
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++)
      map.markers[i].setMap(null); 
}

Open in new window

0
 

Author Comment

by:Lmillard
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;
// 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

0
 
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.
0
Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

 

Author Comment

by:Lmillard
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
// 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

0
 
LVL 19

Accepted Solution

by:
Albert Van Halen earned 500 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;
         markerToDelete.setMap(null);
         break;
      }
   }
}

Open in new window

0
 

Author Closing Comment

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

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

When it comes to write a Context Sensitive Help (an online help that is obtained from a specific point in state of software to provide help with that state) ,  first we need to make the file that contains all topics, which are given exclusive IDs. …
Learn by example how to specify CSS selectors for Selenium WebDriver test automation software.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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…

740 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