有没有我可以ping的服务或API,并传入lat / long作为参数,它返回lat / long对所在的邮政编码?这只是美国的,所以我不必担心国际代码等。
我觉得谷歌地图的反向地理编码对我来说太沉重了。如果可能的话,我更喜欢更轻的东西。这将在javascript中完成。
它被称为Reverse Geocoding (Address Lookup)。要获取lat的地址:40.714224,lng:-73.961452使用参数http://maps.googleapis.com/maps/api/geocode/json
(latlng=40.714224,-73.961452&sensor=true
)查询example并返回JSON对象或使用http://maps.googleapis.com/maps/api/geocode/xml
返回XML响应(example)。这是来自谷歌,它是免费的。
对于Google API,您需要在Google地图中根据其网站使用它:
注意:地理编码API只能与Google地图结合使用;禁止在地图上显示地理编码结果。
Google Maps API可以获取与地理位置相关联的邮政编码。但是,如果你在丛林中间的某个地方,那么这个API将不会返回任何内容,因为邮政编码被映射到邮政地址。在这种情况下,您需要获取附近城市的邮政编码。
您可以使用此方法执行此操作
findNearbyPostalCodesJSON?lat=47&lng=9&username=demo
工作实例
{
"postalCodes": [{
"adminCode3": "1631",
"distance": "2.2072",
"postalCode": "8775",
"countryCode": "CH",
"lng": 8.998679778165283,
"placeName": "Luchsingen",
"lat": 46.980169648620375
}]
}
//Reverse GeoCode position into Address and ZipCOde
function getZipCodeFromPosition(geocoder, map,latlng,userLocationInfoWindow) {
geocoder.geocode({'location': latlng}, function(result, status) {
if (status === 'OK') {
if (result[0]) {
console.log("GeoCode Results Found:"+JSON.stringify(result));
//Display Address
document.getElementById("address").textContent = "Address: " +result[0].formatted_address;
//Update Info Window on Server Map
userLocationInfoWindow.setPosition(latlng);
userLocationInfoWindow.setContent('<IMG BORDER="0" ALIGN="Left" SRC="https://media3.giphy.com/media/IkwH4WZWgdfpu/giphy.gif" style ="width:50px; height:50px"><h6 class ="pink-text">You Are Here</h4> <p class = "purple-text" style ="margin-left:30px;">'+result[0].formatted_address+'</p>');
userLocationInfoWindow.open(map);
map.setCenter(latlng);
//Try to Get Postal Code
var postal = null;
var city = null;
var state = null;
var country = null;
for(var i=0;i<result.length;++i){
if(result[i].types[0]=="postal_code"){
postal = result[i].long_name;
}
if(result[i].types[0]=="administrative_area_level_1"){
state = result[i].long_name;
}
if(result[i].types[0]=="locality"){
city = result[i].long_name;
}
if(result[i].types[0]=="country"){
country = result[i].long_name;
}
}
if (!postal) {
geocoder.geocode({ 'location': result[0].geometry.location }, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
//Postal Code Not found, Try to get Postal code for City
var result=results[0].address_components;
for(var i=0;i<result.length;++i){
if(result[i].types[0]=="postal_code"){
postal = result[i].long_name;
}
}
if (!postal) {
//Postal Code Not found
document.getElementById("postal").textContent = "No Postal Code Found for this location";
}else
{
//Postal Code found
document.getElementById("postal").textContent = "Zip Code: "+postal;
}
}
});
} else
{
//Postal Code found
document.getElementById("postal").textContent = "Zip Code: "+postal;
}
console.log("STATE: " + state);
console.log("CITY: " + city);
console.log("COUNTRY: " + country);
} else {
window.alert('No results found');
}
} else {
window.alert('Geocoder failed due to: ' + status);
}
});
}
</script>
来自kubetz的答案很棒,但由于我们必须使用https来使地理定位工作(https://codepen.io/hiteshsahu/pen/gxQyQE?editors=1010),因此调用失败,因为它没有超过https。从geonames.org获取的本地php脚本(通过https调用)解决了这个问题。
Geolocation API Removed from Unsecured Origins in Chrome 50