最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Google Maps API v3: Adding markers from an array doesn't work - Stack Overflow

programmeradmin10浏览0评论

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
Add a ment  | 

1 Answer 1

Reset to default 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',
    });
}
发布评论

评论列表(0)

  1. 暂无评论