使用Google Maps API绘制许多行

时间:2013-12-13 12:59:09

标签: javascript google-maps google-maps-api-3

我正在尝试使用Google API的JavaScript API绘制一堆行,但我不断收到语法错误。我的数据存储如下:

var line_map = {};
line_map['l1'] = {
  path: [new google.maps.LatLng(42.3581, -71.0636), new google.maps.LatLng(42.351821, -71.045461)],
  weight: 2
};

有许多其他行条目。然后我尝试使用以下内容:

for (var entry in line_map) {
    var line = new google.maps.Polyline({
      path: entry.path,
      geodesic: true,
      strokeColor: '#FF0000',
      strokeOpacity: 1.0,
      strokeWeight: entry.weight
    });

    // Add the line to map
    line.setMap(map);
  }

但是我一直收到Invalid value for constructor parameter 0: undefined

的错误消息

我知道它是在说entry.path未定义但我不明白为什么因为我在l1的条目中明确定义了

1 个答案:

答案 0 :(得分:3)

你应该改变这个:

path: entry.path

path: line_map[entry].path

weight做同样的事情。查看this working fiddle - 我更改了一些坐标以查看该行。

编辑:关于for ... in循环和对象,this post也有一个很好的解释。