简体   繁体   English

将Google Maps Geocoder与国家/地区的地址和组件限制一起使用

[英]Using Google maps geocoder with address and component restrictions for country

I have a field on my form that is using the google autocomplete feature 我的表单上有一个使用Google自动填充功能的字段

new window.google.maps.places.Autocomplete(location, { types: ['geocode'] });

but if the user doesn't select a dropdown from the autocomplete I need google maps geocoder to search when the form is submitted. 但是,如果用户未从自动完成功能中选择下拉菜单,则我需要Google Maps Geocoder在提交表单时进行搜索。 I want my user to select either a street, town, city, neighborhood, etc anywhere in the world! 我希望用户选择世界上任何地方的街道,城镇,城市,邻里等!

 var navbarGeocoder = new google.maps.Geocoder(); navbarGeocoder.geocode({ 'address': location.value }, function(results, status) { 

But now I want to be able to limit the geocoder to the US (street, neighborhood, city, town, etc.) with the componentRestriction of the country (US), but when I try this below it just hangs! 但是现在,我希望能够使用对国家/地区(US)的componentRestriction将地理编码器限制到美国(街道,邻里,城市,城镇等),但是当我在下面尝试使用它时,它就挂了!

 var navbarGeocoder = new google.maps.Geocoder(); navbarGeocoder.geocode({ 'address': location.value }, { componentRestrictions: { country: ' US' } }, function(results, status) { if (status === google.maps.GeocoderStatus.OK) { // do something } else { // do something } }); 

Is there a proper way to add the component restriction and still have the address field? 有没有适当的方法来添加组件限制并仍然具有地址字段? Any help would be appreciated! 任何帮助,将不胜感激!

I'm using this link to the google site for doc, is there a field in the component restrictions that acts like the address field? 我正在使用指向 Google文档网站的链接,组件限制中是否有一个类似于地址字段的字段?

From looking over the documentation you supplied I think your problem might actually be pretty simple - though I have yet to test it myself. 通过查看您提供的文档,我认为您的问题实际上可能非常简单-尽管我尚未亲自对其进行测试。 Anyway, if you scroll up to the "Geocoding requests" section you'll see the following code. 无论如何,如果您向上滚动到“地理编码请求”部分,则会看到以下代码。

{
 address: string,
 location: LatLng,
 placeId: string,
 bounds: LatLngBounds,
 componentRestrictions: GeocoderComponentRestrictions,
 region: string
}

So, I think you just need to put the componentRestrictions field in the same array as the actual address. 因此,我认为您只需要将componentRestrictions字段与实际地址放在同一数组中即可。 Let me know if it helps :) 让我知道是否有帮助:)

So instead of what you posted, try this... 因此,请尝试以下操作,而不是您发布的内容:

var navbarGeocoder = new google.maps.Geocoder(); 
navbarGeocoder.geocode({ 
    'address': location.value, 
    componentRestrictions: { 
        country: ' US' 
    } 
}, 

I figured it out. 我想到了。

below is the code snippet! 下面是代码片段!

 navbarGeocoder.geocode({ address: location.value, componentRestrictions: { country: ' US' } }, function(results, status) { 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM