【问题标题】:Google maps suggestions. filter suggestions based on first input谷歌地图建议。根据第一个输入过滤建议
【发布时间】:2015-09-15 10:05:58
【问题描述】:

我正忙于谷歌地图。一切正常。 在首页上有一张地图,该地图绘制了一条来自 2 个用户输入的路线。

现在我想根据第一个输入的位置过滤来自第二个输入的建议。

目前,如果在第一次输入中输入了荷兰的地址,如果您没有指定完整的地址,建议开始提供美国的地址。

在谷歌发回建议之前,是否可以为第二个输入提供半径或其他内容? 我发现所有的限制都是 50KM,而大多数游乐设施都超过 50KM。

基于国家/地区是行不通的,因为它是国际性的。

【问题讨论】:

  • 所以您想将第二个输入中的结果限制为第一个输入中选择的国家/地区?
  • 不是国家,更像是一个半径。它是一个国际网站。因此,例如,如果客户从荷兰旅行到比利时,我想要来自荷兰和比利时的结果。现在结果在美国开始。

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


【解决方案1】:

radius 不是一个可能的选项,但是您可以通过@987654325 的bounds-选项在结果应该首选的位置周围定义一个区域(LatLngBounds) @.(限制仅适用于国家/地区)

这样的面积可以通过google.maps.geometry.spherical.computeOffset来计算

例子:

      function init() {

        var map = new google.maps.Map(document.getElementById('map_canvas'), {
            zoom: 1,
            center: new google.maps.LatLng(0, 0),
            noClear: true,
            disableDefaultUI: true
          }),
          diagonal = 250, //length of the diagonal in km
          inputs = map.getDiv().querySelectorAll('input[id^="pac"]'),
          acs = [],
          area = new google.maps.Rectangle(),
          marker = new google.maps.Marker({
            animation: google.maps.Animation.DROP
          });

        for (var i = 0; i < inputs.length, i < 2; ++i) {
          map.controls[google.maps.ControlPosition.TOP_CENTER].push(inputs[i]);
          acs.push(new google.maps.places.Autocomplete(inputs[i]));
          if (i === 1) {
            //first input
            google.maps.event.addListener(acs[0], 'place_changed', function() {
              //when there is a valid place
              if (this.getPlace().geometry) {

                var center = this.getPlace().geometry.location,
                  bounds = new google.maps.LatLngBounds(center);
                //create a area around the  place
                bounds.extend(google.maps.geometry.spherical.computeOffset(center, diagonal / 2 * 1000, 135));
                bounds.extend(google.maps.geometry.spherical.computeOffset(center, diagonal / 2 * 1000, 315));

                //just a rectangle to visualize the used area
                area.setOptions({
                  map: map,
                  bounds: bounds
                });

                map.fitBounds(bounds);
                //set the prefered search-area for the 2nd autocomplete
                acs[1].setBounds(bounds);
              } else {
                acs[1].setBounds(null);
                area.setMap(null);
              }
            });
            //2nd input
            google.maps.event.addListener(acs[1], 'place_changed', function() {
              //when there is a valid place
              if (this.getPlace().geometry) {
                //draw a marker and set the center of the map
                var center = this.getPlace().geometry.location;
                map.setCenter(center)
                marker.setOptions({
                  map: map,
                  position: center
                })
              } else {
                marker.setMap(null);
              }
            });
          }
        }
      }
 html,
 body,
 #map_canvas {
   height: 100%;
   margin: 0;
   padding: 0;
 }
<div id="map_canvas">
  <input id="pac1">
  <input id="pac2">
</div>
<script src="https://maps.googleapis.com/maps/api/js?v=3&libraries=places,geometry&callback=init"></script>

【讨论】:

  • 非常感谢,这正是我想要的!接受为答案!
猜你喜欢
  • 2011-10-22
  • 2011-04-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多