2015-05-28 23 views
0

我正在嘗試使用$ .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"]]

回答

1

你必須做出點爲[經度,緯度]不是[緯度,經度]。 此外,將數字寫爲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]] 

}

+0

好笑的,我看着一切,除了像交換經緯度一樣簡單。感謝您的幫助,這很好, – Yonkee