我有一组包含邮政编码的用户,我想根据 Google map 上的邮政编码显示他们的位置,但我无法将我的邮政编码转换为 GeoJSON 格式。帮帮我
最佳答案
据我查找,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);
});
关于javascript - 如何将邮政编码转换为 GeoJSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41048630/