Javascript 如何使用谷歌地图反转地理编码坐标以获取邮政pincode?

Javascript 如何使用谷歌地图反转地理编码坐标以获取邮政pincode?,javascript,api,google-maps,geocoding,Javascript,Api,Google Maps,Geocoding,我正在制作一个应用程序,它将使用用户的当前位置或他定制的地图标记来查找lat&long,然后使用这些值,我想知道该区域的pincode(zipcode),这样我就可以告诉用户该区域是否可以交付货物 我试过这样做:但它没有完整的数据,而且它所拥有的数据也不是很准确。是否有其他API可用于获取此类数据?如果您有位置(以及Google Maps API v3地图),则位置。处理返回的邮政编码记录(请参阅) // assumes comma separated coordinates in a inp

我正在制作一个应用程序,它将使用用户的当前位置或他定制的地图标记来查找lat&long,然后使用这些值,我想知道该区域的pincode(zipcode),这样我就可以告诉用户该区域是否可以交付货物

我试过这样做:但它没有完整的数据,而且它所拥有的数据也不是很准确。是否有其他API可用于获取此类数据?

如果您有位置(以及Google Maps API v3地图),则位置。处理返回的邮政编码记录(请参阅)

// assumes comma separated coordinates in a input element 
function codeLatLng() {
  var input = document.getElementById('latlng').value;
  var latlngStr = input.split(',', 2);
  var lat = parseFloat(latlngStr[0]);
  var lng = parseFloat(latlngStr[1]);
  var latlng = new google.maps.LatLng(lat, lng);
  geocoder.geocode({'latLng': latlng}, processRevGeocode);
}

// process the results
function processRevGeocode(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
       var result;
       if (results.length > 1)
          result = results[1];
       else
          result = results[0];
       if (result.geometry.viewport)
          map.fitBounds(result.geometry.viewport);
       else if (result.geometry.bounds)
          map.fitBounds(result.geometry.bounds);  
       else { 
          map.setCenter(result.geometry.location);
          map.setZoom(11);
       }
       if (marker && marker.setMap) marker.setMap(null);
       marker = new google.maps.Marker({
           position: result.geometry.location,
           map: map
       });
       infowindow.setContent(results[1].formatted_address);
       infowindow.open(map, marker);
       displayPostcode(results[0].address_components);

    } else {
      alert('Geocoder failed due to: ' + status);
    }
}

// displays the resulting post code in a div
function displayPostcode(address) {
  for (p = address.length-1; p >= 0; p--) {
    if (address[p].types.indexOf("postal_code") != -1) {
       document.getElementById('postcode').innerHTML= address[p].long_name;
    }
  }
}