javascript – 基于谷歌地图中的特定国家限制搜索放置api

前端之家收集整理的这篇文章主要介绍了javascript – 基于谷歌地图中的特定国家限制搜索放置api前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用谷歌地方api自动完成位置作为用户类型.但是,我如何将建议限制在特定国家/地区?

这是我的javascript

function initAutocomplete() {

    // Create the search Box and link it to the UI element.
    var input = document.getElementById('autocomplete-input');
    var searchBox = new google.maps.places.SearchBox(input);

    // [START region_getplaces]
    // Listen for the event fired when the user selects a prediction and retrieve
    // more details for that place.
    searchBox.addListener('places_changed',function() {
        var places = searchBox.getPlaces();
        if (places.length == 0) {
            return;
        }
    });
}

解决方法

通过将SearchBox更改为自动完成并传递带有要求的选项来修复它.如果有人需要,这是代码.
function initAutocomplete() {

    // Create the search Box and link it to the UI element.
    var input = document.getElementById('autocomplete-input');
    var options = {
        componentRestrictions: {country: 'fr'}
    };

    var searchBox = new google.maps.places.Autocomplete(input,options);

    // [START region_getplaces]
    // Listen for the event fired when the user selects a prediction and retrieve
    // more details for that place.
    searchBox.addListener('places_changed',function() {
        var places = searchBox.getPlaces();
        if (places.length == 0) {
            return;
        }
    });
}
原文链接:https://www.f2er.com/js/159710.html

猜你在找的JavaScript相关文章