简体   繁体   English

如何将邮政编码转换为GeoJSON

[英]How to convert zip code into GeoJSON

I have an array of users containing their zip codes and i would like to display their location based on their zipcode on Google Maps but i cannot turn my zipcode into a GeoJSON format. 我有一组包含邮政编码的用户,我想根据他们在Google Maps上的邮政编码显示位置,但是我无法将其邮政编码转换为GeoJSON格式。 Help me 帮我

From what I looked up GeoJSON uses latitude and longitude right? 根据我的查找,GeoJSON使用纬度和经度对吗?

If that's the case you can just use another api that tells you latitude and longitude based on the zip code you input in its query. 如果是这种情况,您可以使用另一个api,根据您在查询中输入的邮政编码来告诉您纬度和经度。

Using google api: 使用Google API:

http://maps.googleapis.com/maps/api/geocode/json?address= {zipcode} http://maps.googleapis.com/maps/api/geocode/json?address= {zipcode}

extract the lat/lon coordinate values from the JSON above: 从上面的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);


});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM