【问题标题】:How to reverse geocode coordinates to get postal pincode using google map? [closed]如何使用谷歌地图反转地理编码坐标以获取邮政密码? [关闭]
【发布时间】:2013-05-22 19:25:16
【问题描述】:

我正在制作一个应用程序,它将获取用户的当前位置或他的自定义地图标记来找出纬度和经度,然后使用这些值我想知道该区域的密码(邮政编码),以便我可以告诉用户是否商品是否可以在该区域交付。

我试过这个:http://www.geonames.org/export/ws-overview.html 但它没有完整的数据,而且它所拥有的任何东西都不是很准确。是否有任何其他 API 可用于获取此类数据?

【问题讨论】:

    标签: javascript api google-maps geocoding


    【解决方案1】:

    如果您有位置(和 Google Maps API v3 地图),请reverse geocode 该位置。处理返回的 postal_code 记录(请参阅this SO post for an example)。

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

    Working example (displays a postcode from a geocoded address, reverse geocoded coordinates, or a click on the map)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-07
      相关资源
      最近更新 更多