Google Maps API: Post-Created Access Tokens
I am wondering if there is a way to access markers that have been added to the Map using JavaScript after creation.
I mean, is it possible on the page to click a button that, on each click, centers the map on another marker that was added earlier?
Any help would be really appreciated!
+1
source to share
1 answer
Here's one way.
Create an array of marker locations and add them to your array when adding markers to the map. Associate an array index with a button and when the button is clicked you can get your location from your array and focus on it.
Here's an example (very simple). 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)" />
+5
source to share