How can I get zip code based on lat & long?
Asked Answered
D

6

17

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

I feel like Google Maps' reverse-geocoding is too heavy for me. I'd prefer something lighter, if possible. This will be done in javascript.

Dinothere answered 4/12, 2011 at 0:23 Comment(1)
possible duplicate of Zip Code DatabasePhonograph
B
24

It is called Reverse Geocoding (Address Lookup). To get address for lat: 40.714224, lng: -73.961452 query http://maps.googleapis.com/maps/api/geocode/json with parameters latlng=40.714224,-73.961452&sensor=true (example) and it returns JSON object or use http://maps.googleapis.com/maps/api/geocode/xml to return XML response (example). It's from Google and it's free.

Bottrop answered 4/12, 2011 at 0:30 Comment(3)
It should be noted that Google explicitly forbids using Geocoding without putting it on a map, per their TOSServility
Above link returns 404..so heres the latest link of Geocoding API - developers.google.com/maps/documentation/geocoding/…Invert
Currently, It is not free. Check here developers.google.com/maps/documentation/geocoding/…Charleton
L
8

For the Google API, you need to use it within a Google map, according to their site:

Note: the Geocoding API may only be used in conjunction with a Google map; geocoding results without displaying them on a map is prohibited.

Lowery answered 10/9, 2012 at 19:22 Comment(2)
Not sure why this was downvoted to begin with, but it brings up a good point about the TOS on the Google APIDreary
THIS IS NOT TRUE. "You can display Geocoding API results on a Google Map, or without a map. If you want to display Geocoding API results on a map, then these results must be displayed on a Google Map. It is prohibited to use Geocoding API data on a map that is not a Google map." developers.google.com/maps/documentation/geocoding/policiesSuperfluous
H
7

Please have a look on http://geonames.org. There is a webservice findNearbyPostalCodes (international).

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

Shortened output:

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

Limit of the demo account is 2000 queries per hour.

Hydromancy answered 4/12, 2011 at 0:30 Comment(2)
Works beautiful for me. Haven't implimented it yet but I pasted your jsfiddle code into my view and it told me my zipcode on load.Sackett
Hey kubetz, how would I get nearest city, and what state? Is that possible? Your jsfiddle code works like a charm in my project.Sackett
F
1

Google Maps API can get zip code associated with a geolocation. However if you are somewhere in middle of jungle then this API will not return anything as postal code are mapped to postal addresses. In that case you need to get zip code of nearby city.

You can use this method to do so

  //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

Flight answered 30/8, 2017 at 11:50 Comment(0)
P
0

The answer from kubetz is great, but since we have to use https to get geolocation to work (Geolocation API Removed from Unsecured Origins in Chrome 50), the call to http://api.geonames.org fails because it is not over https. A local php script, called over https, that gets 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 script
<?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);
?>
Pseudaxis answered 9/3, 2017 at 16:24 Comment(0)
E
0

For Swift do the following:

import CoreLocation

class ViewController: UIViewController, CLLocationManagerDelegate {

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
    self.locationManager.delegate = self
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
    
}


let locationManager = CLLocationManager()
let geoCoder = CLGeocoder()

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        let longitude = manager.location?.coordinate.longitude
        let latitude = manager.location?.coordinate.latitude
        let currentLocation = CLLocation(latitude: latitude!, longitude: longitude!)
        
        self.geoCoder.reverseGeocodeLocation(currentLocation, completionHandler: { (placemarks, error) -> Void in
            
            // Place details
            var placeMark: CLPlacemark?
            placeMark = placemarks?[0]
            
            if let zipCodeFound = placeMark?.postalCode {
                print(zipCodeFound)
            })
}
Expeditionary answered 25/6, 2024 at 2:19 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.