如何使用谷歌地图 API 获取阿联酋国家/地区的邮政编码区域?
Posted
技术标签:
【中文标题】如何使用谷歌地图 API 获取阿联酋国家/地区的邮政编码区域?【英文标题】:How to get region in place of zipcode for UAE countries using google maps API? 【发布时间】:2017-05-11 20:30:03 【问题描述】:当我得到结果时,我没有得到所选阿联酋地区的区域,因为在阿联酋没有邮政编码/邮政编码。 如果有任何方法可以获取阿联酋地区的地区,请建议我。
下面是我的代码:
var componentForm =
street_number: 'short_name',
route: 'long_name',
administrative_area_level_1: 'short_name',
locality: 'long_name',
postal_code: 'short_name'
;
var formComponentIds =
street_number: 'user_add_street',
route: 'user_add_street',
administrative_area_level_1: 'user_add_state',
locality: 'user_add_city',
postal_code: 'user_add_zipcode'
;
function initAutocomplete ()
autocomplete = new google.maps.places.Autocomplete((document.getElementById('user_add_street')),types: ['geocode']);
autocomplete.addListener('place_changed', fillInAddress);
$(document).on('focusout', '#pickupStreet', function()
var firstResult = $(".pac-container .pac-item:first").text();
var geocoder = new google.maps.Geocoder();
geocoder.geocode("address":firstResult , function(results, status)
if (status == google.maps.GeocoderStatus.OK)
var lat = results[0].geometry.location.lat(),
lng = results[0].geometry.location.lng(),
placeName = results[0].address_components[0].long_name,
latlng = new google.maps.LatLng(lat, lng);
$('#pickup_latitude').val(lat);
$('#pickup_longitude').val(lng);
debugger
for (var i = 0; i < results[0].address_components.length; i++)
if( results[0].address_components[i].types[0] == 'route')
$('#pickupStreet').val(results[0].address_components[i-1].long_name + " " + results[0].address_components[i].long_name);
else if (results[0].address_components[i].types[0] == 'locality')
$('#pickupCity').val(results[0].address_components[i].long_name);
else if (results[0].address_components[i].types[0] == 'administrative_area_level_1')
$('#pickupState').val(results[0].address_components[i].long_name);
// else if (results[0].address_components[i].types[0] == 'postal_code')
// $('#pickupZipcode').val(results[0].address_components[i].long_name);
//
else
console.log();
$('#user_customer_detail_attributes_zipcode').val($('#pickupZipcode').val());
);
);
function fillInAddress ()
// Get the place details from the autocomplete object.
var place = autocomplete.getPlace();
$('#pickup_latitude').val(place.geometry.location.lat());
$('#pickup_longitude').val(place.geometry.location.lng());
if (place.address_components)
for (var component in componentForm)
componentId = formComponentIds[component];
document.getElementById(componentId).value = '';
document.getElementById(componentId).disabled = false;
// Get each component of the address from the place details
// and fill the corresponding field on the form.
for (var i = 0; i < place.address_components.length; i++)
var addressType = place.address_components[i].types[0];
if (componentForm[addressType])
var val = place.address_components[i][componentForm[addressType]];
if (formComponentIds[addressType] == 'user_add_street')
var street = document.getElementById(formComponentIds[addressType]).value;
document.getElementById(formComponentIds[addressType]).value = street + ' ' + val;
else
document.getElementById(formComponentIds[addressType]).value = val;
$('#user_add_street').val(place.name.split(','));
document.getElementById('user_add_apartment_number') = '';
else
splitted_address = place.name.split(',');
street = splitted_address[0];
city = splitted_address[1];
state = splitted_address[2];
$('#pickupStreet').val(street);
$('#pickupCity').val(city);
$('#pickupState').val(state);
$('#pickupZipcode').val('');
【问题讨论】:
【参考方案1】:嘿,只是看看你的问题,它没有发生在我身上,但我发现了一些可以帮助你的东西
您需要进一步了解区域代码偏差。
获取region list的列表
要了解更多请点击链接
https://developers.google.com/maps/documentation/javascript/geocoding#GeocodingRegionCodes
【讨论】:
以上是关于如何使用谷歌地图 API 获取阿联酋国家/地区的邮政编码区域?的主要内容,如果未能解决你的问题,请参考以下文章
在谷歌地图 api v3 中将地区、城市、州、国家/地区转换为纬度和经度?
如何获取某个国家/地区的位置点取决于 Android 手机配置