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

时间:2015-05-28 08:05:32

标签: 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

错误讯息:

  

错误:无效的LatLng对象:(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"]]

1 个答案:

答案 0 :(得分:1)

你必须将这些点设为[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]]

}