【问题标题】:Using Google Places searchBox to retrieve only address results使用 Google Places searchBox 仅检索地址结果
【发布时间】:2016-06-22 18:40:08
【问题描述】:

我正在使用自动完成进行地图搜索,一旦选择了新地址,当前标记将被删除并替换为用户搜索的标记。为此,我使用了搜索框,但是自动完成中显示的一些结果不起作用,例如“购物”或“奥迪”,它们会为整个地图上的每个地点结果添加一个标记。我试图使这些结果仅过滤到地址,但似乎无法使其工作。我一直在文档中看到 types: address 属性,但似乎无法弄清楚在这种情况下会去哪里。任何帮助都非常感谢!这是我的代码:

function initAutocomplete() {
        var map = new google.maps.Map(document.getElementById('gmap_canvas'), {
            zoom: 14,
            center: new google.maps.LatLng(39.711383, -105.018230),
            mapTypeId: google.maps.MapTypeId.ROADMAP,
            zoomControl: true,
            streetViewControl: true,
            mapTypeControl: false
        });

        var myLatLng = {lat: 39.711383, lng: -105.018230};

        var icon = {
          url: "../../../images/gold-delivery-128.png",
          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(64, 64)
        };

        var marker = new google.maps.Marker({
            position: myLatLng,
            map: map,
            icon: icon
        });

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

        // Bias the SearchBox results towards current map's viewport.
        map.addListener('bounds_changed', function() {
          searchBox.setBounds(map.getBounds());
        });

        var markers = [];
        // 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;
          }

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

          marker.setVisible(false);

          // For each place, get the icon, name and location.
          var bounds = new google.maps.LatLngBounds();
          places.forEach(function(place) {
            var icon = {
              url: "../../../images/gold-delivery-128.png",
              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(64, 64)
            };

            // 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);

        });
      }

【问题讨论】:

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


    【解决方案1】:

    SearchBox doesn't allow any filtering (its only option is a google.maps.LatLngBounds)。另一个选项是Places Autocomplete,它接受type option。您可以查看其中一个是否支持您的用例(也许geocodeestablishment 会起作用)。

    【讨论】:

    • 谢谢!!使用自动完成而不是搜索框可以解决问题,并允许我使用地理编码过滤结果。
    【解决方案2】:

    一种简单的方法是过滤结果,如下所示:

     // Clear out the old markers.
     markers.forEach(function(marker) {
       marker.setMap(null);
     });
     markers = [];
    
     marker.setVisible(false);
    
     <!---- Filtering the results : start ------>
     places = places.filter(function(place) {
       return place.types.indexOf('locality') > -1 ||
              place.types.indexOf('political') > -1;
     });
     <!---- Filtering the results : end ------>
    
     // For each place, get the icon, name and location.
     var bounds = new google.maps.LatLngBounds();
     places.forEach(function(place) {
       ...
    

    支持的类型在这里: https://developers.google.com/maps/documentation/geocoding/intro#Types

    【讨论】:

      猜你喜欢
      • 2018-01-14
      • 1970-01-01
      • 1970-01-01
      • 2018-11-02
      • 2015-11-13
      • 2019-11-14
      • 1970-01-01
      • 2014-01-14
      • 1970-01-01
      相关资源
      最近更新 更多