Javascript 从邮政编码获取 LatLng - Google Maps API
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5585957/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get LatLng from Zip Code - Google Maps API
提问by Scott
All I want is some simple example code that shows me how to obtain a latlng element from an inputted zip code OR a city/state.
我想要的只是一些简单的示例代码,向我展示如何从输入的邮政编码或城市/州获取 latlng 元素。
回答by Mark
Couldn't you just call the following replaceing the {zipcode} with the zip code or city and state http://maps.googleapis.com/maps/api/geocode/json?address={zipcode}
难道你不能只是调用以下替换 {zipcode} 与邮政编码或城市和州 http://maps.googleapis.com/maps/api/geocode/json?address={zipcode}
Here is a link with a How To Geocode using JavaScript: Geocode walk-thru. If you need the specific lat/lng numbers call geometry.location.lat() or geometry.location.lng() (API for google.maps.LatLng class)
这是一个包含如何使用 JavaScript 进行地理编码的链接:Geocode walk-thru。如果您需要特定的经纬度数字,请调用 geometry.location.lat() 或 geometry.location.lng() (google.maps.LatLng 类的 API)
EXAMPLE to get lat/lng:
获取纬度/经度的示例:
var lat = '';
var lng = '';
var address = {zipcode} or {city and state};
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
lat = results[0].geometry.location.lat();
lng = results[0].geometry.location.lng();
});
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
alert('Latitude: ' + lat + ' Logitude: ' + lng);
回答by andilabs
Just a hint: zip codes are not worldwide uniqueso this is worth to provide country ISO code in the request (https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2).
只是一个提示:邮政编码不是全球唯一的,因此值得在请求中提供国家/地区 ISO 代码(https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2)。
e.g looking for coordinates of polish (iso code PL
) zipcode 01-210
:
例如寻找波兰(iso 代码PL
)邮政编码的坐标01-210
:
https://maps.googleapis.com/maps/api/geocode/json?address=01210,PL
https://maps.googleapis.com/maps/api/geocode/json?address=01210,PL
how to obtain user country code?
如何获取用户国家代码?
if you would like to get your user country info based on IP address there are services for it, e.g you can do GET request on: http://ip-api.com/json
如果您想根据 IP 地址获取您的用户国家/地区信息,则可以为其提供服务,例如您可以在以下位置执行 GET 请求:http: //ip-api.com/json
回答by Rambutan
Here is the most reliable way to get the lat/long from zip code (i.e. postal code):
这是从邮政编码(即邮政编码)获取经纬度的最可靠方法:
https://maps.googleapis.com/maps/api/geocode/json?key=YOUR_API_KEY&components=postal_code:97403
回答by Geovanny Buitrago
This is just an improvement to the previous answers because it didn't work for me with some zipcodes even when in https://www.google.com/mapsit does, I fixed just adding the word "zipcode " before to put the zipcode, like this:
这只是对先前答案的改进,因为即使在https://www.google.com/maps中,它对某些邮政编码也不起作用,我修复了在放置之前只添加了“zipcode”一词邮政编码,像这样:
function getLatLngByZipcode(zipcode)
{
var geocoder = new google.maps.Geocoder();
var address = zipcode;
geocoder.geocode({ 'address': 'zipcode '+address }, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var latitude = results[0].geometry.location.lat();
var longitude = results[0].geometry.location.lng();
alert("Latitude: " + latitude + "\nLongitude: " + longitude);
} else {
alert("Request failed.")
}
});
return [latitude, longitude];
}
回答by Amir Md Amiruzzaman
Here is the function I am using for my work
这是我用于工作的功能
function getLatLngByZipcode(zipcode)
{
var geocoder = new google.maps.Geocoder();
var address = zipcode;
geocoder.geocode({ 'address': address }, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var latitude = results[0].geometry.location.lat();
var longitude = results[0].geometry.location.lng();
alert("Latitude: " + latitude + "\nLongitude: " + longitude);
} else {
alert("Request failed.")
}
});
return [latitude, longitude];
}
回答by Paramjeet
<script src="https://maps.googleapis.com/maps/api/js?key=API_KEY"></script>
<script>
var latitude = '';
var longitude = '';
var geocoder = new google.maps.Geocoder();
geocoder.geocode(
{
componentRestrictions: {
country: 'IN',
postalCode: '744102'
}
}, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
latitude = results[0].geometry.location.lat();
longitude = results[0].geometry.location.lng();
console.log(latitude + ", " + longitude);
} else {
alert("Request failed.")
}
});
</script>
https://developers.google.com/maps/documentation/javascript/geocoding#ComponentFiltering
https://developers.google.com/maps/documentation/javascript/geocoding#ComponentFiltering