2

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',
          });
      }
 }

1 Answer 1

5

cities 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',
    });
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks so much for answering! Unfortunately it still doesn't place a marker. You can see the whole map at websu.it/devnw/dev/contact.html The latitude and longitude should be a place in The Netherlands and it does not show up :( Any ideas on what I'm doing wrong?
Haha looking at that console all the time; and just now I forgot. Thanks. sorry haha.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.