first of all thanks for considering to answer this :) Much appreciated!
I've created a map using the following code, and this works perfectly.
function initialize() {
var mapOptions = {
zoom: 5,
center: new google.maps.LatLng(48.160, -6.832),
disableDefaultUI: true,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById('map_canvas'), mapOptions);
setMarkers(map, cities);
}
But then I want markers at each of the cities in this array (please don't suggest changing this as this exact piece of code solves another problem I had, unless of course absolutely neccesary):
var cities = {
'Groningen': [ 53.216723950863425, 6.560211181640625, 7],
'San Francisco': [ 34.01131647557699, -118.25599389648437, 5],
'New York City': [ 40.7143528, -74.0059731, 3]
};
And I'm using this code to place the actual markers (which is the part that doesn't work):
function setMarkers(map, locations) {
// Add markers to the map
for (var i = 0; i < cities.length; i++) {
var data = cities [i]
var marker = new google.maps.Marker({
position: new google.maps.LatLng (data[0], data[1]),
map: map,
icon: image,
title: 'test',
});
}
}
first of all thanks for considering to answer this :) Much appreciated!
I've created a map using the following code, and this works perfectly.
function initialize() {
var mapOptions = {
zoom: 5,
center: new google.maps.LatLng(48.160, -6.832),
disableDefaultUI: true,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById('map_canvas'), mapOptions);
setMarkers(map, cities);
}
But then I want markers at each of the cities in this array (please don't suggest changing this as this exact piece of code solves another problem I had, unless of course absolutely neccesary):
var cities = {
'Groningen': [ 53.216723950863425, 6.560211181640625, 7],
'San Francisco': [ 34.01131647557699, -118.25599389648437, 5],
'New York City': [ 40.7143528, -74.0059731, 3]
};
And I'm using this code to place the actual markers (which is the part that doesn't work):
function setMarkers(map, locations) {
// Add markers to the map
for (var i = 0; i < cities.length; i++) {
var data = cities [i]
var marker = new google.maps.Marker({
position: new google.maps.LatLng (data[0], data[1]),
map: map,
icon: image,
title: 'test',
});
}
}
Share
Improve this question
edited Aug 27, 2013 at 11:30
j0k
22.8k28 gold badges81 silver badges90 bronze badges
asked Jan 13, 2013 at 22:23
Robin PapaRobin Papa
1801 gold badge2 silver badges11 bronze badges
1 Answer
Reset to default 5cities
isn't an array but an object so you can't use
for (var i = 0; i < cities.length; i++) {
//...
}
Use this instead
for (var key in cities) {
var data = cities[key];
var marker = new google.maps.Marker({
position: new google.maps.LatLng (data[0], data[1]),
map: map,
icon: image,
title: 'test',
});
}