views:

201

answers:

1

Here is what I am trying to do. I have about 160 places of interest. The user enters their address (postcode, full address, whatever) which I use Google to geo-encode. I then create a Google map centred about this point and I add a marker for each of my points of interest to the map (using a MarkerManager). So far so good.

I want to display a list of items beside the map that correspond to the markers being displayed. When the user drags the window, or zooms in or out, or whatever, I want to update this list. If too many items are shown at once, I want to display a message to the user.

What is the best way to do this? I tried adding a listener to the MarkerManager so that when it changed I could work out which markers were still shown. However, the event doesn't seem to fire as I expected, i.e. when the markers being displayed change. Also, I doubt that looping over 160+ markers like this every time is going to be efficient.

             GEvent.addListener(mgr, "changed", 
                function(bounds, markerCount) 
                {
                    var visibleBounds = map.getBounds();

                    for (var i = 0; i < gmarkers.length; i++) 
                    {
                        //alert(gmarkers[i].getPoint());
                        if (visibleBounds.containsLatLng(gmarkers[i].getLatLng())) {

                            // this will need to be replaced with an ajax call
                            // to get the details from the server
                            strHtml += "<div>Another item</div>";
                            count ++;
                        }
                    }
                    alert(count);
                });

What is the best way to accomplish this?

UPDATE: This code works, but the event only seems to fire when the map is moved by a certain minimum distance. So if the user drags the map a short distance, the event doesn't seem to fire.

Thanks!

David

+1  A: 

The 'changed' event only fires if markers have changed, so there would certainly be instances when small movements don't change anything. MarkerManager expands its working bounds quite a bit, to make things more smooth when moving around (it shows markers that are off the map, within a certain distance).

Chris B