javascript - 使用 $.each 循环构建传单 GeoJson

标签 javascript jquery leaflet geojson

我正在尝试使用 $.each 循环构建 GeoJson LineString。不过,我似乎无法解决这个问题。

根据我读到的信息,这应该有效。

我做错了什么?

var geojson = {
  type: 'LineString',
  coordinates: []
};

        $.each(data, function(key, data) { 
    geojson.coordinates.push(data.shape_pt_lat.toString(),data.shape_pt_lon.toString());
          });
console.log(geojson.coordinates.toString());
L.geoJson(geojson).addTo(map_osm);

Console.log 输出为

-36.861461,174.750440,-36.860228,174.751980,-36.859726,174.752736,-36.858892,174.753709,-36.858594,174.754863,-36.858037,174.756671,-36.857528,174.758350,-36.857694,174.759050,-36.855159,174.758675,-36.854423,174.759160,-36.851987,174.760902,-36.851186,174.761970,-36.851037,174.762303,-36.850380,174.762700,-36.849411,174.763020,-36.848521,174.763507,-36.847178,174.764000,-36.845462,174.764750,-36.843594,174.767050

错误消息:

Error: Invalid LatLng object: (3, NaN)

如果我使用以下内容:

geojson.coordinates.push([data.shape_pt_lat,data.shape_pt_lon]);

它显示一条线,但它位于 map 的顶部中间,距离输入的坐标不远。

控制台输出是:

[["-36.857694", "174.759050"], ["-36.855159", "174.758675"], ["-36.854423", "174.759160"], ["-36.851987", "174.760902"], ["-36.851186", "174.761970"], ["-36.851037", "174.762303"], ["-36.850380", "174.762700"], ["-36.849411", "174.763020"], ["-36.848521", "174.763507"], ["-36.847178", "174.764000"], ["-36.845462", "174.764750"], ["-36.843594", "174.767050"]]

最佳答案

您必须将点表示为 [lon,lat] 而不是 [lat,lon]。 此外,将数字写为 float ,而不是字符串(删除引号)。 您可以在 http://geojsonlint.com/ 中测试有效性。 您的数据示例:

{
"type": "LineString",
"coordinates": [[-36.857694, 174.759050], [-36.855159, 174.758675], [-36.854423, 174.759160], [-36.851987, 174.760902], [-36.851186, 174.761970], [-36.851037, 174.762303], [-36.850380, 174.762700], [-36.849411, 174.763020], [-36.848521, 174.763507], [-36.847178, 174.764000], [-36.845462, 174.764750], [-36.843594, 174.767050]]

}

包含正确数据的示例

{
"type": "LineString",
"coordinates": [[174.759050, -36.857694], [174.758675, -36.855159], [174.759160, -36.854423], [174.760902, -36.851987], [174.761970, -36.851186], [174.762303, -36.851037], [174.762700, -36.850380], [174.763020, -36.849411], [174.763507, -36.848521], [174.764000, -36.847178], [174.764750, -36.845462], [174.767050, -36.843594]]

}

关于javascript - 使用 $.each 循环构建传单 GeoJson,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30500660/

相关文章:

javascript - 使用嵌套数组从数组中获取最小的字符串

javascript - 将 JSignature 输出存储到 mysql 以在单独的页面上重绘

javascript - 滚动时屏幕跳转

javascript - 捕获溢出时的滚动 :Hidden elements

javascript - 使用 jQuery 删除表中指定行之后的所有行

javascript - 在 Mapbox.js 中,如何平滑折线?

javascript - 更新定价的结帐控制面板?

css - 使用 VueJs2 和 webpack 的传单 CSS 导入问题

javascript:当鼠标悬停到集群时,会有弹出窗口,但当放大时,集群会扩大,但弹出窗口仍然存在

javascript - AngularJs。在 $http.success 中调用 jQuery 函数