how to get all markers on google-maps-v3

Google MapsGoogle Maps-Api-3

Google Maps Problem Overview


Is there a way to get all the markers on Google Maps?

Google Maps Solutions


Solution 1 - Google Maps

If you mean "how can I get a reference to all markers on a given map" - then I think the answer is "Sorry, you have to do it yourself". I don't think there is any handy "maps.getMarkers()" type function: you have to keep your own references as the points are created:

var allMarkers = [];
....
// Create some markers
for(var i = 0; i < 10; i++) {
    var marker = new google.maps.Marker({...});
    allMarkers.push(marker);
}
...

Then you can loop over the allMarkers array to and do whatever you need to do.

Solution 2 - Google Maps

The one way found is to use the geoXML3 library which is suitable for usage along with KML processor Version 3 of the Google Maps JavaScript API.

Solution 3 - Google Maps

I'm assuming you have multiple markers that you wish to display on a google map.

The solution is two parts, one to create and populate an array containing all the details of the markers, then a second to loop through all entries in the array to create each marker.

Not know what environment you're using, it's a little difficult to provide specific help.

My best advice is to take a look at this article & accepted answer to understand the principals of creating a map with multiple markers: https://stackoverflow.com/questions/2870670/multiple-infowindows-tearing-my-hair-out

Solution 4 - Google Maps

If you are using JQuery Google map plug-in then below code will work for you -

var markers = $('#map_canvas').gmap('get','markers');

Solution 5 - Google Maps

For an specific cluster use: getMarkers() Gets the array of markers in the clusterer.

For all the markers in the map use: getTotalMarkers() Gets the array of markers in the clusterer.

Solution 6 - Google Maps

Google Maps API v3:

I initialized Google Map and added markers to it. Later, I wanted to retrieve all markers and did it simply by accessing the map property "markers".

var map = new GMaps({
    div: '#map',
    lat: 40.730610,
    lng: -73.935242,
});

var myMarkers = map.markers;

You can loop over it and access all Marker methods listed at [Google Maps Reference][1].

[1]: https://developers.google.com/maps/documentation/javascript/reference#Marker "Google Maps Reference"

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionzjm1126View Question on Stackoverflow
Solution 1 - Google MapsMr SpeakerView Answer on Stackoverflow
Solution 2 - Google MapsIgorView Answer on Stackoverflow
Solution 3 - Google MapsthewinchesterView Answer on Stackoverflow
Solution 4 - Google MapsRajView Answer on Stackoverflow
Solution 5 - Google MapsAaron AcevesView Answer on Stackoverflow
Solution 6 - Google MapsTommyZGView Answer on Stackoverflow