Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
137
rated 0 times [  144] [ 7]  / answers: 1 / hits: 112532  / 14 Years ago, thu, april 1, 2010, 12:00:00

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);
}
});
}


}


More From » google-maps

 Answers
33

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);

[#97180] Tuesday, March 30, 2010, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
lonnier

Total Points: 621
Total Questions: 113
Total Answers: 98

Location: Nepal
Member since Mon, Jan 4, 2021
3 Years ago
;