我所需的只是一些简单的示例代码,以显示如何从输入的邮政编码或城市/州获取latlng元素。
我所需的只是一些简单的示例代码,以显示如何从输入的邮政编码或城市/州获取latlng元素。
你能否直接调用以下代码,将 {zipcode} 替换为邮政编码或城市和州的名称? http://maps.googleapis.com/maps/api/geocode/json?address={zipcode}
这里有一个使用 JavaScript 进行地理编码的操作步骤链接:地理编码演示。如果你需要特定的纬度和经度,请调用 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://thezipcodes.com/api/v1/search?zipCode={zipCode}&countryCode={2digitCountryCode}&apiKey={apiKey}
我在这里找到了大部分数据。
这只是以前答案的改进,因为它在某些邮政编码上对我不起作用,即使在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];
}
这是我在工作中使用的函数:
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
import pgeocode
nomi = pgeocode.Nominatim('us')
query = nomi.query_postal_code("90001")
data = {
"lat": query["latitude"],
"lon": query["longitude"]
}
print(data)