How to restrict google places to specific city

后端 未结 3 1572
时光说笑
时光说笑 2020-12-10 16:37

I am currently able to restrict places to only one country, but I also want to restrict on a specific city also. Any ideas how to achieve that? This is my current code:

3条回答
  •  不知归路
    2020-12-10 16:42

    As Luke said the places autocomplete allows you to use componentRestrictions to filter by country only.

    But you can use a little trick with a search request string. Just add prefix with city name in request.

    var prefix = 'Kyiv, ';
    
    $(input).on('input',function(){
        var str = input.value;
        if(str.indexOf(prefix) == 0) {
            // string already started with prefix
            return;
        } else {
            if (prefix.indexOf(str) >= 0) {
                // string is part of prefix
                input.value = prefix;
            } else {
                input.value = prefix+str;
            }
        }
    });
    

    http://jsfiddle.net/24p1et98/

提交回复
热议问题