输入5个字符后,Google Place自动填充地址表单 [英] Google Place Autocomplete Address Form after typing 5 characters

查看:265
本文介绍了输入5个字符后,Google Place自动填充地址表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是Google开发者网站提供的代码( https://developers.google.com/maps/documentation/javascript/examples/places-autocomplete-addressform ),如下所示。使用此代码,在输入第一个字符后,google自动完成地址列表将出现在地址文本框下方。我希望能够在Google列表下拉之前键入5个字符。我期待着你们中的任何人都能帮助我实现我的目标。先谢谢你!

  //此示例使用Google Places API的自动填充功能
//来显示地址表单用户填写信息。

var placeSearch,autocomplete;
var componentForm = {
street_number:'short_name',
route:'long_name',
locality:'long_name',
administrative_area_level_1:'short_name',
国家:'long_name',
postal_code:'short_name'
};

函数initAutocomplete(){
//创建自动填充对象,将搜索限制为地理区域
//位置类型。
autocomplete = new google.maps.places.Autocomplete(
/ ** @type {!HTMLInputElement} * /(document.getElementById('autocomplete')),
{types:['地理编码']});

//当用户从下拉列表中选择一个地址时,填写表单中的地址
//字段。
autocomplete.addListener('place_changed',fillInAddress);
}

函数fillInAddress(){
//从自动填充对象中获取地点详细信息。
var place = autocomplete.getPlace();

(componentForm中的var组件){
document.getElementById(component).value ='';
document.getElementById(component).disabled = false;
}

//从地点详细信息
//获取地址的每个组成部分,然后填写表单中相应的字段。
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]];
document.getElementById(addressType).value = val;
}
}
}

//将自动完成对象偏移到用户的地理位置
//,由浏览器的'navigator.geolocation '对象。
函数geolocate(){
if(navigator.geolocation){
navigator.geolocation.getCurrentPosition(function(position){
var geolocation = {
lat:position .coords.latitude,
lng:position.coords.longitude
};
var circle = new google.maps.Circle({
center:geolocation,
radius :position.coords.accuracy
});
autocomplete.setBounds(circle.getBounds());
});
}
}


解决方案

A客户希望仅在输入超过2个字符时触发自动完成功能。
我找不到开箱即用解决方案(功能请求已在 https://code.google.com/p/gmaps-api-issues/issues/detail?id = 10367 on Google Code)。



Thx to Zme ... @ gmail.com( https://code.google.com/p/gmaps-api-issues/issues/detail?id=3429 ),我可以制定出满足如果(1< query.length){//如果输入2个字符或更多字符
if(($ <$ p



$> undefined == self.googleAutocomplete){// ...和Google Autocomplete尚未设置
console.debug('trigger autocomplete!');
self.googleAutocomplete = new google.maps.places.Autocomplete(self.HtmlElement,self.settings.google_options);
}
//!注意:2个字符=>我们只是设置自动完成:实际的建议将从第三个字符开始...
google.maps.event.addListener(self.googleAutocomplete,'place_changed',function(){
//做任何事建议选择...
}
} else if(undefined!= self.googleAutocomplete){//如果小于2个字符
console.debug('unbind autocomplete!');
//移除自动完成:Thx至Zme ... @ gmail.com
// ...在https://code.google.com/p/gmaps-api-issues/issues/detail?id = 3429
$(。pac-container)。remove();
google.maps.event.clearListeners(self.HtmlElement);
self.googleAutocomplete = undefined;
}

虽然它对我有用,但我仍然认为通过这样重的物体是一种耻辱实例/删除只是为了activa te /取消自动完成。
请不要犹豫,您可以投票支持在Google Autocompletion API中实现此功能 https://code.google.com/p/gmaps-api-issues/issues/detail?id=10367 ,如果您认为它有用。



祝你有美好的一天

I am using the code taken from the Google Developer website (https://developers.google.com/maps/documentation/javascript/examples/places-autocomplete-addressform), which is indicated below. With this code, the google autocomplete address list appears below the address text-box just after typing the first character. I would like to be able to type 5 characters before the google list drops down. I look forward anyone of you could help me to get my aim. Thank you in advance!

// This example displays an address form, using the autocomplete feature
// of the Google Places API to help users fill in the information.

var placeSearch, autocomplete;
var componentForm = {
  street_number: 'short_name',
  route: 'long_name',
  locality: 'long_name',
  administrative_area_level_1: 'short_name',
  country: 'long_name',
  postal_code: 'short_name'
};

function initAutocomplete() {
  // Create the autocomplete object, restricting the search to geographical
  // location types.
  autocomplete = new google.maps.places.Autocomplete(
      /** @type {!HTMLInputElement} */(document.getElementById('autocomplete')),
      {types: ['geocode']});

  // When the user selects an address from the dropdown, populate the address
  // fields in the form.
  autocomplete.addListener('place_changed', fillInAddress);
}

function fillInAddress() {
  // Get the place details from the autocomplete object.
  var place = autocomplete.getPlace();

  for (var component in componentForm) {
    document.getElementById(component).value = '';
    document.getElementById(component).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]];
      document.getElementById(addressType).value = val;
    }
  }
}

// Bias the autocomplete object to the user's geographical location,
// as supplied by the browser's 'navigator.geolocation' object.
function geolocate() {
  if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(function(position) {
      var geolocation = {
        lat: position.coords.latitude,
        lng: position.coords.longitude
      };
      var circle = new google.maps.Circle({
        center: geolocation,
        radius: position.coords.accuracy
      });
      autocomplete.setBounds(circle.getBounds());
    });
  }
}

解决方案

A customer wanted to have autocompletion triggered only if more than 2 chars were entered. I could not find an "out-of-the-box" solution (feature request opened at https://code.google.com/p/gmaps-api-issues/issues/detail?id=10367 on Google Code).

Thx to Zme...@gmail.com (https://code.google.com/p/gmaps-api-issues/issues/detail?id=3429), I could work out following solution that fulfills the need.

if (1 < query.length) { // if 2 chars or more entered
                if (undefined == self.googleAutocomplete) { //... and Google Autocomplete not set yet
                    console.debug('trigger autocomplete !');
                    self.googleAutocomplete = new google.maps.places.Autocomplete(self.HtmlElement, self.settings.google_options);
                }   
                // ! Beware : with 2 chars => we are only SETTING the autocompletion : actual suggestions will start from 3rd char...
                google.maps.event.addListener(self.googleAutocomplete, 'place_changed', function () {
                    // do whatever with suggestion selected...
                }
            } else if (undefined != self.googleAutocomplete) { // if less than 2 chars
                    console.debug('unbind autocomplete !');
                    // removing autocompletion : Thx to Zme...@gmail.com 
                    //... on https://code.google.com/p/gmaps-api-issues/issues/detail?id=3429
                    $(".pac-container").remove();
                    google.maps.event.clearListeners(self.HtmlElement);
                    self.googleAutocomplete = undefined;
            }

Although it worked for me, I still think it is a shame to go through such heavy object instanciation/deletion just to activate/disactivate autocompletion. Do not hesitate to vote in favor of having such a feature implemented in Google Autocompletion Api at https://code.google.com/p/gmaps-api-issues/issues/detail?id=10367, if you deem it usefull.

Have a good day

这篇关于输入5个字符后,Google Place自动填充地址表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆