【发布时间】:2011-07-31 22:53:09
【问题描述】:
我想要的只是一些简单的示例代码,向我展示如何从输入的邮政编码或城市/州获取 latlng 元素。
【问题讨论】:
-
您在寻找您的示例时是否有特定的语言?
标签: javascript google-maps-api-3
我想要的只是一些简单的示例代码,向我展示如何从输入的邮政编码或城市/州获取 latlng 元素。
【问题讨论】:
标签: javascript google-maps-api-3
您不能直接调用以下代码,将 {zipcode} 替换为邮政编码或城市和州 http://maps.googleapis.com/maps/api/geocode/json?address={邮编}
这是一个如何使用 JavaScript 进行地理编码的链接:Geocode walk-thru。如果您需要特定的纬度/经度数字,请调用 geometry.location.lat() 或 geometry.location.lng() (API for google.maps.LatLng class)
获取 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);
【讨论】:
提示:邮政编码并非全球唯一,因此值得在请求中提供国家/地区 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 中,它对某些邮政编码也不起作用,我修复了在放置邮政编码之前添加单词“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];
}
【讨论】:
在从事我的实习项目时,我找到了一个网站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
【讨论】: