5

GeoCodingの呼び出しに都市のみを渡して、郵便番号を返さないと述べた場合。なぜですか?

これが私の電話です:

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

}
4

1 に答える 1

3

郵便番号が本当に必要な場合は、リクエストをリバース ジオコーダーに返すことができます。これにより、都市をある程度代表する郵便番号が得られます。

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

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>

于 2012-12-21T20:02:07.917 に答える