【问题标题】:Mapbox/Leaflet Javascript : Uncaught TypeError: Cannot read property 'lat' of undefinedMapbox/Leaflet Javascript:未捕获的类型错误:无法读取未定义的属性“纬度”
【发布时间】:2015-08-22 20:12:48
【问题描述】:

我正在尝试构建一个应用程序来构建多色路径(颜色取决于条件)并将它们显示在传单地图上。问题如下。我有 2 个功能:一个用于绘制,一个用于将视图适合绘制区域。当我只将我的 latlng 数组传递给一个时,它就完成了这项工作。当我将它传递给 draw() 和 fitboduns() 两个函数时,我收到“未捕获的类型错误:无法读取未定义的属性 'lat'”错误...关于是否:(取决于顺序)

  • draw() 函数,如果我使用“addTo”方法绑定 & draw-> 线。
  • bound() 函数,如果我使用 fitBounds 方法绘制 & bound-> 线。

我尝试了很多东西,这是我最新的代码

注意:数据是 L.latLng 的数组: data.push(new L.latLng(test[0],test[1]));

function draw(data) {

var singlePath;
singlePath = [];

for (var i = 0; i < data.length; i++) {

  singlePath.push(data[i],data[i+1]);
  alert ("singlePath=" + singlePath);
  var firstpolyline = L.polyline(singlePath, {
    //color: pathColor[i] -> Color ARRAY 
    color : 'blue',
    weight: 8,
    opacity: 0.5,
    smoothFactor: 1

  }).addTo(leafmap);
  singlePath = [];
}

function bound(data) {
var data_bound =[];

var bounds = (new L.latLngBounds(data));

data_bound.push([bounds.getNorthEast().lat,bounds.getNorthEast().lng]);
data_bound.push([bounds.getSouthWest().lat,bounds.getSouthWest().lng]);
leafmap.fitBounds(data_bound); 

}

感谢您的帮助!!!

【问题讨论】:

    标签: javascript leaflet mapbox


    【解决方案1】:
    for (var i = 0; i < data.length; i++) {
      singlePath.push(data[i],data[i+1]);
    

    这个循环在i == data.length - 1 结束,你引用data[i + 1],这将是未定义的。将循环的条件更改为i &lt; data.length - 1

    【讨论】:

      猜你喜欢
      • 2012-12-22
      • 1970-01-01
      • 2020-04-19
      • 2018-07-29
      • 2018-07-30
      • 2018-07-31
      • 2016-01-01
      • 2012-07-04
      相关资源
      最近更新 更多