2016-12-08 17 views
2

我有一個包含他們的郵政編碼的用戶數組,我想根據他們的谷歌地圖上的郵政編碼顯示他們的位置,但我無法將我的郵政編碼轉換爲GeoJSON格式。幫我如何將郵政編碼轉換爲GeoJSON

+0

你做了什麼?向我們展示一些代碼。 –

回答

0

從我擡頭看GeoJSON使用的是經緯度吧?

如果是這種情況,您可以使用另一個API,根據您在其查詢中輸入的郵政編碼告訴您經緯度。

使用谷歌API:

http://maps.googleapis.com/maps/api/geocode/json?address= {}郵政編碼

從JSON上述提取緯度/經度座標值:

var zipCode; //from your array of users 

$.getJSON("http://maps.googleapis.com/maps/api/geocode/json?address=" + zipCode, function (json){ 
    var latitude = json.results[0].geometry.location.lat; 
    var longitude = json.results[0].geometry.location.lng; 

    //Handle your geoJSON here, I'm just guessing on this part I don't know geoJSON 
    var geojsonFeature = 
    { 
     "type": "Feature", 
     "properties": 
     { 
      "name": "", 
      "amenity": "", 
      "popupContent": "" 
     }, 
     "geometry": 
     { 
      "type": "Point", 
      "coordinates": [latitude, longitude] 
     } 
    }; 

    L.geoJSON(geojsonFeature).addTo(map); 


}); 
相關問題