I have a script that loops and adds markers one at a time.
I am trying to get the current marker to have an info window and and only have 5 markers on a map at a time (4 without info windows and 1 with)
How would I add an id to each marker so that I can delete and close info windows as needed.
This is the function I am using to set the marker:
function codeAddress(address, contentString) { var infowindow = new google.maps.InfoWindow({ content: contentString }); if (geocoder) { geocoder.geocode( { 'address': address}, function(results, status) { if (status == google.maps.GeocoderStatus.OK) { map.setCenter(results[0].geometry.location); var marker = new google.maps.Marker({ map: map, position: results[0].geometry.location }); infowindow.open(map,marker); } else { alert("Geocode was not successful for the following reason: " + status); } }); }
}
A marker identifies a location on a map. By default, a marker uses a standard image. Markers can display custom images, in which case they are usually referred to as "icons." Markers and icons are objects of type Marker . You can set a custom icon within the marker's constructor, or by calling setIcon() on the marker.
Open the map styles dialog If you don't see a gray bar labeled "Configure map" above the map itself, click "Tools > Change map". If "Feature map" is not highlighted in red, click it. Click the "Change feature styles" button. Click "Points > Marker icon" if it's not already selected.
Create Custom Maps using Google MapsClick on the “Your Places” option in the menu. Click on the “Maps” Tab in the top right. Click on the “CREATE MAP” link at the bottom of the menu. Once you are on the map creation page, click the marker icon to add a marker to the page.
JavaScript is a dynamic language. You could just add it to the object itself.
var marker = new google.maps.Marker(markerOptions); marker.metadata = {type: "point", id: 1};
Also, because all v3 objects extend MVCObject()
. You can use:
marker.setValues({type: "point", id: 1}); // or marker.set("type", "point"); marker.set("id", 1); var val = marker.get("id");
Just adding another solution that works for me.. You can simply append it in the marker options:
var marker = new google.maps.Marker({ map: map, position: position, // Custom Attributes / Data / Key-Values store_id: id, store_address: address, store_type: type });
And then retrieve them with:
marker.get('store_id'); marker.get('store_address'); marker.get('store_type');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With