【问题标题】:Google Map API limit auto complete in this codeGoogle Map API 限制在此代码中自动完成
【发布时间】:2016-07-11 16:43:19
【问题描述】:

我正在使用谷歌地图 API,但我想将搜索限制在特定国家/地区

我可以使用这个代码来做到这一点

componentRestrictions: countryRestrict

但是我把它放在哪里我的自动完成搜索框代码在下面

var input = document.getElementById('pac-input');
        var searchBox = new google.maps.places.SearchBox(input);
        map.controls[google.maps.ControlPosition.TOP_LEFT].push(input);

下面的任何我的搜索框事件监听器

   searchBox.addListener('places_changed', function() {
          var places = searchBox.getPlaces();

         if (places.length == 0) {
            return;
          }

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

          // For each place, get the icon, name and location.
          var bounds = new google.maps.LatLngBounds();
          places.forEach(function(place) {
            var icon = {
              url: place.icon,
              size: new google.maps.Size(71, 71),
              origin: new google.maps.Point(0, 0),
              anchor: new google.maps.Point(17, 34),
              scaledSize: new google.maps.Size(25, 25)
            };

            // Create a marker for each place.
            markers.push(new google.maps.Marker({
              map: map,
              icon: icon,
              title: place.name,
              position: place.geometry.location
            }));

            if (place.geometry.viewport) {
              // Only geocodes have viewport.
              bounds.union(place.geometry.viewport);
            } else {
              bounds.extend(place.geometry.location);
            }
          });
          map.fitBounds(bounds);
        });/* Search box result */

我把代码放在哪里

componentRestrictions: countryRestrict

【问题讨论】:

    标签: javascript jquery api google-maps google-maps-api-3


    【解决方案1】:

    你必须在你定义它的地方限制你的自动完成,所以:,

    var options = {
    types: ['(cities)'],
    componentRestrictions: {country: 'de'}/
    };
    
    autocomplete = new google.maps.places.Autocomplete(input, options);
    

    【讨论】:

    • 我已经把它放在我工作的底部,但是这段代码并不高效,它在编写长关键字后停止工作
    • 在地图上搜索后,它的工作原理是最后一件事,我将如何获得它的坐标
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-11
    • 1970-01-01
    • 1970-01-01
    • 2015-11-17
    相关资源
    最近更新 更多