纬度和经度可以找到邮政编码?

问题描述 投票:12回答:5

我使用Google地理编码器进行lat和lon,我的问题是,有没有办法可以找到纬度和经度的邮政编码?

google-maps geolocation
5个回答
7
投票

我认为你要找的是address_components[]阵列中的results。也许这样的东西会起作用,只需输入下面的内容就可以了,但是我觉得你会得到这个想法。

http://code.google.com/apis/maps/documentation/geocoding/#Results

function (request, response) {
  geocoder.geocode({ 'address': request.term, 'latLng': centLatLng, 'region': 'US' }, function (results, status) {
    response($.map(results, function (item) {
      return {
       item.address_components.postal_code;//This is what you want to look at
      }
}

8
投票

值得注意的是,自从提交此决议以来,Google地图有了新版本。

参考:https://developers.google.com/maps/documentation/geocoding/?csw=1#ReverseGeocoding

这是Google Maps v3的更新示例。它使用了JIssak上面提到的地址组件。我应该注意到没有后退。如果找不到邮政编码,它什么都不做。这可能对您的脚本很重要,也可能不重要。

var latlng = new google.maps.LatLng(p.coords.latitude, p.coords.longitude);
geocoder = new google.maps.Geocoder();

    geocoder.geocode({'latLng': latlng}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            if (results[0]) {
                for (j = 0; j < results[0].address_components.length; j++) {
                    if (results[0].address_components[j].types[0] == 'postal_code')
                        alert("Zip Code: " + results[0].address_components[j].short_name);
                }
            }
        } else {
            alert("Geocoder failed due to: " + status);
        }
    });

8
投票

[为谷歌删除了非工作解决方案 - 请参阅@ hblackorby的解决方案。]

这是一个使用openstreetmap.org的版本,比谷歌的api - coffeescript简单得多,然后javascript:

getZip = (cb) ->
  # try to populate zip from geolocation/google geocode api
  if document.location.protocol == 'http:' && navigator.geolocation?
    navigator.geolocation.getCurrentPosition (pos) ->
      coords = pos.coords
      url = "http://nominatim.openstreetmap.org/reverse?format=json&lat=#{ coords.latitude }&lon=#{ coords.longitude }&addressdetails=1"
      $.ajax({
        url: url,
        dataType: 'jsonp',
        jsonp: 'json_callback',
        cache: true,
      }).success (data) ->
        cb(data.address.postcode)

这是编译的javascript:

getZip = function(cb) {
  if (document.location.protocol === 'http:' && (navigator.geolocation != null)) {
    return navigator.geolocation.getCurrentPosition(function(pos) {
      var coords, url;
      coords = pos.coords;
      url = "http://nominatim.openstreetmap.org/reverse?format=json&lat=" + coords.latitude + "&lon=" + coords.longitude + "&addressdetails=1";
      return $.ajax({
        url: url,
        dataType: 'jsonp',
        jsonp: 'json_callback',
        cache: true
      }).success(function(data) {
        return cb(data.address.postcode);
      });
    });
  }
};

像这样使用它:

getZip(function(zipcode){ console.log("zip code found:" + zipcode); });

1
投票

Yahoo PlaceFinder API提供了一种通过lat / lng查找位置数据的好方法:

http://developer.yahoo.com/geo/placefinder/

这是他们使用的示例网址:

http://where.yahooapis.com/geocode?q=38.898717,+-77.035974&gflags=R


1
投票

看起来似乎是这样的:

资料来源:Google Maps API Service

地理编码是将地址(如“1600 Amphitheatre Parkway,Mountain View,CA”)转换为地理坐标(如纬度37.423021和经度-122.083739)的过程,您可以使用它来放置标记或定位地图。 Google Geocoding API提供了通过HTTP请求访问地理编码器的直接方式。此外,该服务允许您执行逆向操作(将坐标转换为地址);这个过程被称为“反向地理编码”。

您还应该查看此文档,其中包含一些示例代码:Reverse Geocoding

© www.soinside.com 2019 - 2024. All rights reserved.