Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
-1
rated 0 times [  6] [ 7]  / answers: 1 / hits: 20127  / 13 Years ago, thu, april 28, 2011, 12:00:00

I started to list my markers individually for my Google Map, but quickly realised this was
inefficient. I have tried to create an array to put the markers onto my map, but it is not showing anything, can you see where I have gone wrong?



function initialize() {
var latlng = new google.maps.LatLng(52.474, -1.868);

var myOptions = {
zoom: 11,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};

var map = new google.maps.Map(document.getElementById(map_canvas), myOptions);
var image = 'i/hotel-map-pin.png';

var hotels = [
['ibis Birmingham Airport', 52.452656, -1.730548, 4],
['ETAP Birmingham Airport', 52.452527, -1.731644, 3],
['ibis Birmingham City Centre', 52.475162, -1.897208, 2]
];

for (var i = 0; i < hotels.length; i++) {
var marker = new google.maps.Marker({
position: hotels[1],
map: map,
icon: image,
title: hotels[0],
zIndex: hotels[2]
});
}
}


Thanks.


More From » google-maps

 Answers
36

In your loop you are accessing the hotels array at fixed indexes (1, 0 & 2) rather than the array elements at index i:



  for (var i = 0; i < hotels.length; i++) {
var hotel = hotels [i]
var marker = new google.maps.Marker({
position: new google.maps.LatLng (hotel[1], hotel[2]),
map: map,
icon: image,
title: hotel[0],
zIndex: hotel[3]
});
}


Here is a working example with the fix.


[#92502] Wednesday, April 27, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
raymondd

Total Points: 620
Total Questions: 112
Total Answers: 94

Location: Namibia
Member since Mon, Feb 21, 2022
2 Years ago
raymondd questions
Thu, Apr 22, 21, 00:00, 3 Years ago
Thu, Jul 9, 20, 00:00, 4 Years ago
Thu, Apr 9, 20, 00:00, 4 Years ago
Thu, Jul 25, 19, 00:00, 5 Years ago
;