首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Google地图使用边界按国家/地区自动完成过滤

Google地图使用边界按国家/地区自动完成过滤
EN

Stack Overflow用户
提问于 2016-09-14 13:28:59
回答 1查看 2.3K关注 0票数 1

如何限制自动补全建议仅适用于美国(国家/地区)。到目前为止,我的代码如下:

代码语言:javascript
复制
<div class="search-result">
                    <form action="" method="post" class="clearfix">
                        <input type="text" placeholder="Enter City or Zip Code" class="searchBox" id="search-result" autocomplete="on">
                        <button type="submit" class="btn btn-brown"></button>
                    </form>
                    <div class="marker-list">
                        <ul>
                        </ul>
                    </div>
                </div>

下面是Javascript代码。请注意,有更多的代码,这不需要过滤US的结果,

代码语言:javascript
复制
inputResult = document.getElementById("search-result"), 
    searchBoxResult = new google.maps.places.SearchBox(inputResult), 

searchBoxResult.addListener("places_changed", function () {

        // Clear old markers
        markers.forEach(function(place) {
            place.setMap(null);
        });
        markers = [];

        var 
            places = searchBoxResult.getPlaces(), 
            bounds = new google.maps.LatLngBounds(), 
            searchPlace = new google.maps.LatLng({lat: places[0].geometry.location.lat(), lng: places[0].geometry.location.lng()});

        $(".search-result form").submit();

        getPlacesList(searchPlace, customPlaces, searchRadius);

        var placesFilter = function (element) {
            return distance(element.LatLng, searchPlace) < searchRadius;
        };

        var j=0;
        customPlaces.forEach(function (place) {
            if(place.distance<=20){
                createMarker(place,j++);
            }
        });

        customPlacesFiltered = customPlaces.filter(placesFilter);

        customPlacesFiltered.forEach(function(place) {
            bounds.extend(place.LatLng);
        });

        map.fitBounds(bounds);
    });  

我的api请求是,

代码语言:javascript
复制
<script src="https://maps.googleapis.com/maps/api/js?key=xxxx&libraries=places,geometry&callback=initMap&sensor=false&libraries=geometry,places&components=country:us&ext=.js" async defer></script>

如何使用此方法限制resluts仅适用于US。请帮帮忙。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-09-14 14:36:37

Autocomplete相比,SearchBox提供的限制搜索的选项更少。在前者中,您可以将搜索偏向给定的LatLngBounds。在后者中,您可以将搜索限制在特定的国家和特定的地点类型,以及设置边界。

代码语言:javascript
复制
var defaultBounds = new google.maps.LatLngBounds(
   new google.maps.LatLng(-33.8902, 151.1759), // LatLng of the north-east corner of US
   new google.maps.LatLng(-33.8474, 151.2631)  // LatLng of the south-west corner of US
);  

var input = document.getElementById('search-result');

var searchBox = new google.maps.places.SearchBox(input, { bounds: defaultBounds });

Autocomplete中我们有componentRestrictions

代码语言:javascript
复制
var options = { componentRestrictions: { country: 'us' } };
var input = document.getElementById('search-result');

var autoComplete = new google.maps.places.Autocomplete(input, options);
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39483051

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档