最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Google map autocomplete - maximum 3 character type - Stack Overflow

programmeradmin3浏览0评论

I have implemented Google map with search box in it which allows user to select address by searching it.

In that search box, even if I am typing 1 character it searches.. I want user to type at least 3 characters else it should not go for search.

My JS Fiddle is: /

How can I do this? Is Google provides that restriction?

I have implemented Google map with search box in it which allows user to select address by searching it.

In that search box, even if I am typing 1 character it searches.. I want user to type at least 3 characters else it should not go for search.

My JS Fiddle is: http://jsfiddle/upsidown/2NhsE/

How can I do this? Is Google provides that restriction?

Share Improve this question edited Feb 2, 2017 at 11:26 Briskstar Technologies asked Feb 2, 2017 at 8:17 Briskstar TechnologiesBriskstar Technologies 2,24312 gold badges40 silver badges75 bronze badges 1
  • 1 Thanks whoever downvotes - it is not valid question!!!??? – Briskstar Technologies Commented Feb 2, 2017 at 9:24
Add a ment  | 

4 Answers 4

Reset to default 2

Google doesn't provide this option for places autoplete element at the moment.

There is a feature requests in the public issue tracker. Have a look at issue 5831. The issue's status is Accepted, however there is no any ETA exposed. You can star this issue to add your vote and receive further updates.

Also there is a ment #15 with remendation how to implement this using autoplete service.


Update: 04 June 2018

This feature was marked as Won't Fix (Obsolete)

This feature request is obsoleted by the new session-based billing for Places autoplete. Please refer to the "Place Autoplete" section of https://cloud.google./maps-platform/user-guide/pricing-changes/ for more details.

You can validate the string and rebuild the AutoComplete like this

in this case im using jQuery this API stopImmediatePropagation also about val

input.keyup(function(e){
if(input.val().length<2){
    e.stopImmediatePropagation()
}
else{
   createAutoComplete(input)
} 

You can initialize the autoplete after user has entered at least minimum characters. Bellow code listens to keyup events on input with jQuery. When the user types 3rd character it will initialize the autoplete.

I think your intension is to avoid additional API requests. This should work.

window.autopleteobj =false;

//initialize map after 2 chars are typed
$('#autopleteinput').keyup(function(e) {
    if ($('#autopleteinput').val().length < 3) {
        e.stopImmediatePropagation()
    } else {
        if (window.autopleteobj == false) {
            window.autopleteobj = new google.maps.places.Autoplete(autopleteinput);
            google.maps.event.addListener(autopleteobj, 'place_changed', function() {
               //your stuff here
            });
        }
    }
});

Check the length of text input in pac-input textbox. if the length is greater than 3, then forward that query to google maps.

发布评论

评论列表(0)

  1. 暂无评论