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

Building leaflet GeoJson with $.each loop

我正在尝试使用 $.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]);

它显​​示一条线,但它位于地图的中上部,离输入的坐标不远。

控制台输出为:

[["-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]。 此外,将数字写为浮点数,而不是字符串(删除引号)。 您可以在 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]]

}