简体   繁体   中英

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. Help me

From what I looked up GeoJSON uses latitude and longitude right?

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.

Using google api:

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

extract the lat/lon coordinate values from the JSON above:

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);


});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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