views:

913

answers:

2

I'm wondering if there is a way to access the Markers that were added to a Map using JavaScript after creation.

I mean, is it possible to make a button somewhere on the page that, on each click, centers the map on another marker that was added previously?

Any help would really be appreciated!

+5  A: 

Here's one way.

Create an array of marker locations and add those to your array as you add the markers to the map. Associate the array index with your button and when the button is clicked, you can get your location from your array and centre on that.

Here's a (very simple) example. The Javascript looks something like this:

var googleMarkerPoints = [];
var googleMap;

function CreateMap() {
     googleMap = new GMap2(yourMapDiv);
     AddMarker(0, 1.2, 1.3);
     AddMarker(1, -1.2, -1.3);
}

function AddMarker(index, latitude, longitude) {
    googleMarkerPoints[index] = new GLatLng(latitude, longitude);
    var marker = new GMarker(googleMarkerPoints[index]);
    googleMap.addOverlay(marker);
}

function SelectMarker(index) {
    googleMap.panTo(googleMarkerPoints[index]);
}

and your HTML looks something like this:

<input type="button" value="Marker0" onclick="SelectMarker(0)" />
<input type="button" value="Marker1" onclick="SelectMarker(1)" />
Robin M
A: 

legend, thanks for this you have solved my hours of internet searches! hoorah

ali