2013-03-30 23 views
0

我是Javascript新手,因此我有點迷路。我能夠從GeoJSON文件讀取;但是,我不明白如何遍歷文件以獲取點的Lat-Long,然後在Leaflet中將這些點顯示爲標記。我希望能也使用該插件真棒標記(基於字體真棒,對於單張)從GeoJSON文件中讀取緯度和經度,然後用小冊子顯示每個lat-long作爲標記

這是我GeoJSON的文件的樣本:

{ "type": "FeatureCollection", 
     "features": [ 
     { "type": "Feature", "properties": { "Street Nam": "Aljunied Avenue 2", " Block": "118 Aljunied Avenue 2", " Postal Co": "380118", " Latitude": 1.320440, "Longitude": 103.887575 }, 
      "geometry": { "type": "Point", "coordinates": [ 103.887575, 1.320440 ] } } 
     , 
     { "type": "Feature", "properties": { "Street Nam": "Aljunied Crescent", " Block": "97A Aljunied Crescent", " Postal Co": "381097", " Latitude": 1.321107, "Longitude": 103.886127 }, 
     "geometry": { "type": "Point", "coordinates": [ 103.886127, 1.321107 ] } } 
    ] 
    } 

感謝您的關注和時間=)

回答

1

the leaflet documentation中所述處理geojson。指定一個pointToLayer函數,該函數用一個真棒圖標創建一個標記:

L.geoJson(geoJson, { 
    pointToLayer: function (feature, latlng) { 
     return L.marker(latlng, 
      {icon: L.AwesomeMarkers.icon( 
       << options based on feature.properties >> 
      )}); 
    } 
}).addTo(map); 
0

讀取文件後,你應該有一個JavaScript對象,表示文件中的所有數據:

var geoData = JSON.parse(fileContents); 
var features = geoData.features; 

等。解析後的數據將被轉換爲對象或數組,取決於它們是鍵/值字典還是列表。所以從上面的

var feature = geoData.features[0]; 

會讓你引用列表中的第一個功能對象。如果你寫

console.log(geoData); 

,並與最近的任何瀏覽器中運行,你應該能夠看到這些數據的可擴展的說明,以幫助使這一切的感覺。