2011-07-20 40 views
9

我使用谷歌地理编码为经纬度和我的问题是,有没有办法可以找到经纬度的邮政编码?纬度和经度可以找到邮政编码?

+0

请注意,此问题的接受答案已经过时。有一个解决方案可以解决当前版本的Google Maps API,它自2013年起显然没有改变,在这里:http://stackoverflow.com/a/17933106/4526479 –

回答

8

我想你正在寻找的是results数组中的address_components[]。也许这样的事情会起作用,只需要输入下面的内容,这样可能会有错误,但我认为你会明白。

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 
     } 
} 
+0

请注意下面的答案以获得最新的答案(显然API自这个答案以来没有改变2013):http://stackoverflow.com/a/17933106/4526479 –

1

看起来是这样:

来源:Google Maps API Service

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

你也应该看看这个文档其中有一些示例代码: Reverse Geocoding

+0

yoda:我将地址转换为你张贴在上面,但我的问题是更多“是否有一种方法,我可以得到基于经纬度的邮政编码 –

8

这里是我的解决方案,它跳过谷歌js和得到的结果JSON(jQuery的&的CoffeeScript):

navigator.geolocation.getCurrentPosition (pos) -> 
    coords = pos.coords 
    url = "http://maps.googleapis.com/maps/api/geocode/json?latlng=#{ coords.latitude },#{ coords.longitude }&sensor=true&callback=zipmap" 
    $.ajax({ 
     url: url, 
     dataType: 'json', 
     cache: true, 
    }).success (data) -> 
     for c in data.results[0].address_components 
     if c.types[0] == 'postal_code' 
      alert c.short_name 

https://developers.google.com/maps/documentation/geocoding/

我想这没有工作很长一段时间 - 尽管我发誓一点。我必须错过一些秘密握手,这是Google .js的一部分。我切换到使用OpenStreetMap.org:

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

本来不会喜欢coffeescript – Zone6

+0

谢谢你的微动@ Zone6,这里是JS版本。 – Julian

+0

感谢这帮了我,开放的街道地图waaayyy更容易获取邮政编码,Google的版本 – garek007

6

这是很好的注意,谷歌地图有新版本时,因为介绍了这一解决方案。

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

下面是谷歌地图3.0版更新的例子。它使用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); 
     } 
    }); 
相关问题