Google maps 经纬度能找到邮政编码吗?
我正在使用Google geocoder来查找lat和lon,我的问题是,有没有一种方法可以找到带纬度和经度的zipcode?雅虎的PlaceFinder API提供了一个很好的wat来查找lat/lng的位置数据: 以下是他们使用的url示例: 看起来是这样的: 资料来源: 地理编码是转换地址的过程(如“1600”) 圆形剧场公园道,加利福尼亚州山景城“)转换为地理坐标 (如纬度37.423021和经度-122.083739),您可以使用 放置标记或定位地图。谷歌地理编码API 提供通过HTTP请求访问地理编码器的直接方式。 此外,该服务允许您执行反向操作 (将坐标转换为地址);这个过程被称为 “反向地理编码。” 您还应该查看此文档,其中包含一些示例代码:Google maps 经纬度能找到邮政编码吗?,google-maps,geolocation,Google Maps,Geolocation,我正在使用Google geocoder来查找lat和lon,我的问题是,有没有一种方法可以找到带纬度和经度的zipcode?雅虎的PlaceFinder API提供了一个很好的wat来查找lat/lng的位置数据: 以下是他们使用的url示例: 看起来是这样的: 资料来源: 地理编码是转换地址的过程(如“1600”) 圆形剧场公园道,加利福尼亚州山景城“)转换为地理坐标 (如纬度37.423021和经度-122.083739),您可以使用 放置标记或定位地图。谷歌地理编码API 提供通过HT
我想您要查找的是
结果数组中的地址组件[]
。也许像这样的东西可以工作,只需键入下面的内容,这样可能会有错误,但我想你会明白的
[删除了谷歌的非工作解决方案-请参阅@hblackorby的解决方案。]
这是一个使用openstreetmap.org的版本,比google的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)
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);
});
});
}
};
以下是已编译的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)
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); });
值得注意的是,自从这个解决方案提出以来,谷歌地图有了一个新版本
参考:
下面是Google Maps v3的更新示例。它使用了上面提到的地址组件。我应该指出,没有退路。如果找不到邮政编码,它什么也不做。这可能对脚本很重要,也可能不重要
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);
}
});
var latlng=new google.maps.latlng(p.coords.lation,p.coords.longitude);
geocoder=新的google.maps.geocoder();
geocoder.geocode({'latLng':latLng},函数(结果,状态){
if(status==google.maps.GeocoderStatus.OK){
如果(结果[0]){
对于(j=0;j
我创建了一个泛型函数来查找您想要的类型。address_组件并不总是有zipcode、country等,如果它们不总是在同一索引中。有时候你的数组长度是8,6或者其他什么。我在Typescript中完成了它,只是做了一些更改,使其成为香草JS
getPlaceTypeValue(addressComponents: Places[], type: string): string {
let value = null;
for (const [i] of addressComponents.entries()) {
if (addressComponents[i].types.includes(type)) {
value = addressComponents[i].long_name;
break;
}
}
return value;
}
或
getPlaceTypeValue(addressComponents: any[], type: string): string {
return (addressComponents.find(({ types }) => types.includes(type)) || {}).long_name || null;
}
用法示例:
this.placesService.getPlaceTypeValue(address.address_components, 'postal_code');
this.placesService.getPlaceTypeValue(address.address_components, 'country');
尤达:我正在把地址转换成你上面贴的地址,但我的问题更多“有没有一种方法可以根据lat和LON获取zipcode呢?我更希望不在咖啡脚本中。谢谢你的轻推@Zone6,这是JS版本。谢谢你的帮助,open street map让我更容易获取邮政编码,谷歌的版本带来的好处似乎不是所有地方都有邮政编码。在某些情况下,结果是未定义的,对吗?当然,有时无法确定邮政编码。所有美国街道地址都有邮政编码,但某些位置无法连接到街道地址。所有geoip数据库都是尽力而为的。请注意以下最新答案的答案(显然,自2013年以来,API在这个答案方面没有改变):请注意,这个问题的公认答案已经过时。有一个解决方案解决了Google Maps API的当前版本,该版本显然自2013年以来一直没有改变:截至2018年9月,这仍然是最好的答案(我刚刚测试过)