问题描述
我使用Google地理位置API和地理编码。
我想要获取有关用户在自动填充中以此格式输入的地址的信息:
街道号码/街道/城市/省份/国家。
如果用户自动完成街道12,SomeCity,SomeProvince,SomeCountry,我希望返回所有这些信息。但是当用户输入someProvince,SomeCountry时,我只想要省和国家的地址类型。
这是我的代码:
google.maps.event.addListener(autocomplete,'place_changed',function(){
var place = autocomplete.getPlace();
'alert('0:'+ place.address_components [0] .long_name);
alert('1:'+ place.address_components [1] .long_name);
alert('2:'+ ('4:'+ place.address_components [4] .long_name);
alert('3:'+ place.address_components [3] .long_name);
alert('4:'+ place.address_components [4] .long_name );
alert('5:'+ place.address_components [5] .long_name);
alert('6:'+ place.address_components [6] .long_name);
alert( '7:'+ place.address_components [7] .long_name);
)};
问题是,当用户自动完成完整地址时,它会正确显示所有这些警报。但是,只有部分信息(仅限国家)自动填充时,它将显示打印国家的7倍。
我想拥有,当没有街道和城市时,它会显示警报(街道是空的等)。如何做到这一点?
为项目制作了这个功能。它分析
- 街道
- / li>
- 邮编
- 城市
function parseGoogleResponse(components){
_.each(components,function(component){
_ .each(component.types,function(type){
if(type ==='route'){
$(input [name = street])。val(component.long_name)
}
if(type ==='street_number'){
$(input [name = nr]).val(component.long_name)
}
if(type ==='locality'){
$(input [name = city]).val(component.long_name)
}
if(type ==='country '){
$(input [name = country]).val(component.long_name)
}
if(type ==='postal_code'){
$ (input [name = zip]).val(component.long_name)
}
})
})
}
I'm using google places API with geocoding. I have a problem with address components type.
I want to get information about address which user typed in autocomplete in this format:
Street number/ street / City/ Province/ Country.
If user autocompleted "Street 12, SomeCity, SomeProvince, SomeCountry", I want to return in alert all of this information. But when user type only "someProvince, SomeCountry", I want to have only province and country address type.
Here is my code:
google.maps.event.addListener(autocomplete, 'place_changed', function () {
var place = autocomplete.getPlace();
alert('0: ' + place.address_components[0].long_name);
alert('1: ' + place.address_components[1].long_name);
alert('2: ' + place.address_components[2].long_name);
alert('3: ' + place.address_components[3].long_name);
alert('4: ' + place.address_components[4].long_name);
alert('5: ' + place.address_components[5].long_name);
alert('6: ' + place.address_components[6].long_name);
alert('7: ' + place.address_components[7].long_name);
)};
Problem is that when user autocomplete full address, it show properly all of this alerts. But when autocomplete only part of information - only country - it will show 7 times what country is typed.
http://gmaps-samples-v3.googlecode.com/svn/trunk/places/autocomplete-addressform.html
I want to have, that when street and city is not given, it will show alert ("street is null" etc). How to do it?
Made this function for a project. It parses
- street
- number
- country
- zip
- city
from a google georesponse
function parseGoogleResponse(components) {
_.each(components, function(component) {
_.each(component.types, function(type) {
if (type === 'route') {
$("input[name=street]").val(component.long_name)
}
if (type === 'street_number') {
$("input[name=nr]").val(component.long_name)
}
if (type === 'locality') {
$("input[name=city]").val(component.long_name)
}
if (type === 'country') {
$("input[name=country]").val(component.long_name)
}
if (type === 'postal_code') {
$("input[name=zip]").val(component.long_name)
}
})
})
}
这篇关于获取address_components的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!