Forum Moderators: open

Message Too Old, No Replies

Google Maps API Help

GEvent.addListener

         

Birdman

12:21 pm on Mar 28, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hello,

I could use some advice here. I have a map with multiple markers. On each marker I am setting an event listener, which should pan the map to that point and zoom.

The problem I am having is that it will only zoom to the last "point" that was set during the for loop.

I've highlighted the problem areas below. What I need to do is pass the current point value to each listener. Really appreciate any advice givin.

function load() {
if (GBrowserIsCompatible()) {
var map = new GMap2(document.getElementById("map"));
map.addControl(new GSmallMapControl());
map.setCenter(new GLatLng(39.021667, -105.505278), 7);

GDownloadUrl("data.php", function(data, responseCode) {
var xml = GXml.parse(data);
var markers = xml.documentElement.getElementsByTagName("marker");

var mgr = new GMarkerManager(map);
var points = [];

for (var i = 0; i < markers.length; i++) {
var point = new GLatLng(parseFloat(markers[i].getAttribute("lat")),
parseFloat(markers[i].getAttribute("lng")));

var title = markers[i].getAttribute("title");
var m = new GMarker(point, { title: title });
GEvent.addListener(m, "click", function() {
map.setCenter(point, 7);
});

points.push( m );

}
mgr.addMarkers(points, 0, 9);
mgr.refresh();
});

}
}

phranque

12:34 pm on Mar 28, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



try replacing the "m" variable with "m$i":
var m$i = new GMarker(point, { title: title });
GEvent.addListener(m$i, "click", function() {
map.setCenter(point, 7);
});
points.push( m$i );

Birdman

12:45 pm on Mar 28, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks Phranque,

I tried that and got the same result. Every marker pans to the last marker's point. I also tried changing the point var to point$i.

I thought that was going to work too :(