Solved

how to remove markers in google maps v3

Posted on 2011-03-03
6
1,372 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
  • 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now