Get lat & long based zip code?

Is there a service or API that I can ping and pass to lat / long as parameters, where it returns the zip code where the lat / long pair is located? This is for the US only, so I don’t have to worry about international codes, etc.

I feel that reverse geocoding Google Maps is too hard for me. I would prefer something lighter if possible. This will be done in javascript.

+6
source share
5 answers

It is called Reverse Geocoding (Address Search) . To get the address for lat: 40.714224, lng: -73.961452 request http://maps.googleapis.com/maps/api/geocode/json with parameters latlng=40.714224,-73.961452&sensor=true ( example ) and it returns a JSON object or uses http://maps.googleapis.com/maps/api/geocode/xml to return an XML Response ( example ). It is from Google and is free.

+17
source

For the Google API, you need to use it on a Google map according to their website:

Note. The geocoding API can only be used in conjunction with the Google map; geocoding results without displaying them on the map is prohibited.

+6
source

Please see http://geonames.org . There is a webservice findNearbyPostalCodes (international).

Example : findNearbyPostalCodesJSON? Lat = 47 & lng = 9 & username = demo

Abbreviated output :

 { "postalCodes": [{ "adminCode3": "1631", "distance": "2.2072", "postalCode": "8775", "countryCode": "CH", "lng": 8.998679778165283, "placeName": "Luchsingen", "lat": 46.980169648620375 }] } 

The demo account limit is 2000 requests per hour.

+5
source

The answer from kubetz is great, but with us we need to use https for geolocation ( the geolocation API Removed from Unsecured Origins in Chrome 50 ), the call to http://api.geonames.org fails because it does not exceed https. A local PHP script called https, which is received from geonames.org, solves the problem.

 //javascript function function getZipFromLatLong() { var url = "getzip.php"; var formData = new FormData(); formData.append("lat", current_lat); formData.append("long", current_long); var r = new XMLHttpRequest(); r.open("POST", url, true); r.onreadystatechange = function () { if (r.readyState != 4 || r.status != 200) { return; } else { data = r.responseText; var jdata = JSON.parse(data); document.getElementById("zip").value = jdata.postalCodes[0].postalCode; } } r.send(formData); } //getzip.PHP  <?php $lat = $_POST["lat"]; $long = $_POST["long"]; $url = "http://api.geonames.org/findNearbyPostalCodesJSON?username=demo&lat=" . $lat . "&lng=" . $long; $content = file_get_contents($url); //the output is json, so just return it as-is die($content); ?> 
0
source

The Google Maps API can retrieve a zip code associated with a geographic location. However, if you are somewhere in the middle of the jungle, then this API will not return anything, since the postal code is mapped to the mailing addresses. In this case, you need to get the zip code of a nearby city.

You can use this method for this.

  //Reverse GeoCode position into Address and ZipCOde function getZipCodeFromPosition(geocoder, map,latlng,userLocationInfoWindow) { geocoder.geocode({'location': latlng}, function(result, status) { if (status === 'OK') { if (result[0]) { console.log("GeoCode Results Found:"+JSON.stringify(result)); //Display Address document.getElementById("address").textContent = "Address: " +result[0].formatted_address; //Update Info Window on Server Map userLocationInfoWindow.setPosition(latlng); userLocationInfoWindow.setContent('<IMG BORDER="0" ALIGN="Left" SRC="https://media3.giphy.com/media/IkwH4WZWgdfpu/giphy.gif" style ="width:50px; height:50px"><h6 class ="pink-text">You Are Here</h4> <p class = "purple-text" style ="margin-left:30px;">'+result[0].formatted_address+'</p>'); userLocationInfoWindow.open(map); map.setCenter(latlng); //Try to Get Postal Code var postal = null; var city = null; var state = null; var country = null; for(var i=0;i<result.length;++i){ if(result[i].types[0]=="postal_code"){ postal = result[i].long_name; } if(result[i].types[0]=="administrative_area_level_1"){ state = result[i].long_name; } if(result[i].types[0]=="locality"){ city = result[i].long_name; } if(result[i].types[0]=="country"){ country = result[i].long_name; } } if (!postal) { geocoder.geocode({ 'location': result[0].geometry.location }, function (results, status) { if (status == google.maps.GeocoderStatus.OK) { //Postal Code Not found, Try to get Postal code for City var result=results[0].address_components; for(var i=0;i<result.length;++i){ if(result[i].types[0]=="postal_code"){ postal = result[i].long_name; } } if (!postal) { //Postal Code Not found document.getElementById("postal").textContent = "No Postal Code Found for this location"; }else { //Postal Code found document.getElementById("postal").textContent = "Zip Code: "+postal; } } }); } else { //Postal Code found document.getElementById("postal").textContent = "Zip Code: "+postal; } console.log("STATE: " + state); console.log("CITY: " + city); console.log("COUNTRY: " + country); } else { window.alert('No results found'); } } else { window.alert('Geocoder failed due to: ' + status); } }); } </script> 

Working example

https://codepen.io/hiteshsahu/pen/gxQyQE?editors=1010

enter image description here

0
source

Source: https://habr.com/ru/post/903028/


All Articles