Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Google GeoCoding only sometimes return the postal code?

If I pass my GeoCoding call only a city and state it fails to return the postal code. Any idea why?

Here is my call:

var test = 'New York, NY';

var geocoder = new google.maps.Geocoder();         
geocoder.geocode({ 'address': test }, function (results, status) {          

if (status == google.maps.GeocoderStatus.OK) {  

    var lat = results[0].geometry.location.lat();
    var lng = results[0].geometry.location.lng();

    var result=results[0].address_components;

    //console.log(result);

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

    alert("POSTAL: " + postal);
    alert("STATE: " + state);
    alert("CITY: " + city);
    alert("COUNTRY: " + country);

}
like image 536
Paul Avatar asked Oct 21 '22 21:10

Paul


1 Answers

If you really need a zip code, you can throw the request back to the reverse geocoder, that should give you a zip code somewhat representative of the city:

if (!postal) {
  geocoder.geocode({ 'location': results[0].geometry.location }, function (results, status) {          
    if (status == google.maps.GeocoderStatus.OK) {  

      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;
       }
      }
      alert("POSTAL: " + postal);
    }
});
} else alert("POSTAL: " + postal);

example

var map;
var geocoder = new google.maps.Geocoder();

function initialize() {
  var mapOptions = {
    zoom: 8,
    center: new google.maps.LatLng(-34.397, 150.644),
    mapTypeId: google.maps.MapTypeId.ROADMAP
  };
  map = new google.maps.Map(document.getElementById('map_canvas'),
    mapOptions);
  var test = 'New York, NY';

  geocoder.geocode({
    'address': test
  }, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {

      var lat = results[0].geometry.location.lat();
      var lng = results[0].geometry.location.lng();
      if (results[0].geometry.viewport) map.fitBounds(results[0].geometry.viewport);
      if (results[0].geometry.viewport) map.fitBounds(results[0].geometry.viewport);
      else if (results[0].geometry.bounds) map.fitBounds(results[0].geometry.bounds);
      else map.setCenter(results[0].geometry.location);

      var result = results[0].address_components;

      //console.log(result);

      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': results[0].geometry.location
        }, function(results, status) {
          if (status == google.maps.GeocoderStatus.OK) {

            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;
              }
            }
            document.getElementById('info').innerHTML += ("POSTAL: " + postal + "<br>");
          }
        });
      } else alert("POSTAL: " + postal);
      document.getElementById('info').innerHTML += ("STATE: " + state + "<br>");
      document.getElementById('info').innerHTML += ("CITY: " + city + "<br>");
      document.getElementById('info').innerHTML += ("COUNTRY: " + country + "<br>");
    }

  });
}
google.maps.event.addDomListener(window, 'load', initialize);
html,
body {
  margin: 0;
  padding: 0;
  height: 100%;
}

#map_canvas {
  height: 80%;
}
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk"></script>
<div id="info"></div>
<div id="map_canvas"></div>
like image 176
geocodezip Avatar answered Nov 15 '22 14:11

geocodezip