[英]Get LatLng from Zip Code - Google Maps API
我想要的只是一些简单的示例代码,它向我展示了如何从输入的邮政编码或城市/州中获取 latlng 元素。
难道你不能只调用以下将 {zipcode} 替换为邮政编码或城市和州http://maps.googleapis.com/maps/api/geocode/json?address= {zipcode}
这是一个链接,其中包含如何使用 JavaScript 进行地理编码: Geocode walk-thru 。 如果您需要特定的纬度/经度数字,请调用 geometry.location.lat() 或 geometry.location.lng()( google.maps.LatLng 类的 API )
获取纬度/经度的示例:
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);
提示:邮政编码不是全球唯一的,因此值得在请求中提供国家/地区 ISO 代码 ( https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2 )。
例如寻找波兰坐标(iso 代码PL
)邮政编码01-210
:
https://maps.googleapis.com/maps/api/geocode/json?address=01210,PL
如何获取用户国家代码?
如果您想根据 IP 地址获取用户所在国家/地区的信息,可以使用相应的服务,例如,您可以在以下位置执行 GET 请求:http: //ip-api.com/json
这是从邮政编码(即邮政编码)获取纬度/经度的最可靠方法:
https://maps.googleapis.com/maps/api/geocode/json?key=YOUR_API_KEY&components=postal_code:97403
这只是对先前答案的改进,因为即使在https://www.google.com/maps中它确实适用于某些邮政编码,它对我也不起作用,我修复了在放置之前仅添加“邮政编码”一词邮编,像这样:
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]; }
在进行我的实习项目时,我找到了一个网站https://thezipcodes.com/创建一个免费帐户并从帐户部分获取 API 密钥。
https://thezipcodes.com/api/v1/search?zipCode={zipCode}&countryCode={2digitCountryCode}&apiKey={apiKey}
我在这里找到了大部分数据。
这是我在工作中使用的功能
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];
}
<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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.