Andrej Andrej - 2 years ago 225
Javascript Question

Javascript - Google Maps v3 - Get positions of markers

Thanks to this site and your help, I have almost finished my Googlemaps script. There is one more thing, I need your help with:

I have multiple markers on my map.

I would like to:

-save each marker position in a variable

-make external links below the map which link to the markers

-change mapcenter on click of these links (described above)

Here is my script so far:

<script type="text/javascript">
var offender_locations = [
["10001", "Title 2", "icon.png"],
["10002", "Title 3", "icon.png"],
["10010", "home", "icon.png"]

var myOptions = {
zoom: 12,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
var map = new google.maps.Map(document.getElementById("results_map"), myOptions);
var latlng = new google.maps.LatLng(0, 0);

for (i = 0; i < offender_locations.length; i++) {
var infowindow = new google.maps.InfoWindow(),
geocoder_map = new google.maps.Geocoder(),
address = offender_locations[i][0],
icon_img = offender_locations[i][2];
(function(addr, img, i) {
'address': addr
}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var marker = new google.maps.Marker({
map: map,
position: map.getCenter(),
icon: img
google.maps.event.addListener(marker, 'click', (function(marker, i) {
return function() {
infowindow.setContent(offender_locations[i][1]);, marker);
})(marker, i));

} else {
}(address, icon_img, i));

function zoomin() {

function zoomout() {

The last two small functions are for the click even of the external links. at this moment they only change the zoom level and do not change the map center.

Answer Source

Try pushing your markers to an array so you can look at them later.

At the beginning do var markers = [];

Then after each marker declaration markers.push(marker);.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download