将Google地图地址查找限制为特定的邮政编码

将Google地图地址查找限制为特定的邮政编码

本文介绍了将Google地图地址查找限制为特定的邮政编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将Google地图地址查询服务限制为特定邮政编码或城市/城镇中的街道名称。它目前在英国给我所有的地址,但是我想限制它只能在英国伯明翰(B邮编)的地址。

代码到目前为止



JS

  addressLookup(); 
});

函数addressLookup(){
var options = {
componentRestrictions:{
country:'uk'
}
};
var address = document.getElementsByClassName('form-control booking address');


(var i = 0; i< address.length; i ++){
google.maps.places.Autocomplete(address [i],options);
}
// new google.maps.places.Autocomplete(address,options);
}

HTML $ b

 < input type =textclass =form-control booking addressid =pickupplaceholder =From> 
< input type =textclass =form-control booking addressid =destinationplaceholder =To>

解决方案

如果伯明翰邮编的定义以B开头,我假定你正在循环的地址数组中的每个值都是一个邮编(是吗?):

 函数isBirmingham(postcode){
return postcode.substr(0,1)==='B';


函数addressLookup(){
var options = {
componentRestrictions:{
country:'uk'
}
};
var address = document.getElementsByClassName('form-control booking address');

for(var i = 0; i< address.length; i ++){
if(isBirmingham(address [i])){
new google.maps.places .Autocomplete(address [i],options);





更新:
$ / strong>



如何在自动填充选项中设置 componentRestrictions ?请参阅 - 该文件只提到你可以指定一个国家。然而演示使用邮政编码。


How do I limit Google Maps address lookup service to street names in a particular postcode or city/town. It is currently giving me all the addresses in the UK, but I would like to limit it to addresses only in Birmingham, UK (B postcode).

Code so far

JS

$(document).ready(function () {
    addressLookup();
});

function addressLookup() {
      var options = {
        componentRestrictions: {
            country: 'uk'
        }
    };
    var address = document.getElementsByClassName('form-control booking address');


    for(var i=0; i< address.length; i++){
    new google.maps.places.Autocomplete(address[i], options);
    }
        //new google.maps.places.Autocomplete(address, options);
}

HTML

<input type="text" class="form-control booking address" id="pickup" placeholder="From">
<input type="text" class="form-control booking address" id="destination" placeholder="To">

Fiddle here

解决方案

If the definition of a Birmingham postcode is starts with a B for instance, and I'm assuming each value in the address array you're looping over is a postcode (is it?):

function isBirmingham(postcode) {
    return postcode.substr(0, 1) === 'B';
}

function addressLookup() {
    var options = {
        componentRestrictions: {
            country: 'uk'
        }
    };
    var address = document.getElementsByClassName('form-control booking address');

    for (var i = 0; i < address.length; i++) {
        if (isBirmingham(address[i])) {
            new google.maps.places.Autocomplete(address[i], options);
        }
    }
}

Update:

What about setting the componentRestrictions in the Autocomplete options? See https://developers.google.com/maps/documentation/javascript/reference#AutocompleteOptions - the documentation only mentions you can specify a country. However this article demonstrates using a postcode.

这篇关于将Google地图地址查找限制为特定的邮政编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 20:32