【问题标题】:How do I add location biasing to Google Places Autocomplete: Javascript API如何向 Google Places Autocomplete 添加位置偏差:Javascript API
【发布时间】:2020-04-18 02:45:34
【问题描述】:

我有一个地方自动填充字段,我用它来让用户提供位置。自动完成表单正在工作,但它并没有像我想要的那样偏向位置。我有以下代码:

function activatePlacesSearch() {
  var input = document.getElementById('project-location-input');
  var location = {lat: 53.2910591, lng: -6.1823276};
  var radius = 1000;
  var autocomplete = new google.maps.places.Autocomplete(input, location, radius);
  google.maps.event.addListener(autocomplete, 'place_changed', function(e) {
      const place = autocomplete.getPlace();
      lat = place.geometry.location.lat();
      lng = place.geometry.location.lng();
      // draw the map
      document.getElementById('new-project-map-div').style.display = 'block'
      var map = new google.maps.Map(document.getElementById('new-project-map-div'), {
      zoom: 15,
      center: new google.maps.LatLng(lat, lng),
      mapTypeId: google.maps.MapTypeId.ROADMAP,
      mapTypeControl: false,
      streetViewControl: false,
      fullscreenControl:false,
      disableDefaultUI: true,
    });

// Set marker position
  var myMarker = new google.maps.Marker({
      position: new google.maps.LatLng(lat, lng),
      draggable: false
    });


  map.setCenter(myMarker.position);
  myMarker.setMap(map);
  });

}

我知道您可以按如下方式查询,并且有效: https://maps.googleapis.com/maps/api/place/autocomplete/xml?input=20&location=53.2910591,-6.1823276&radius=5000&key=AIzaSyC8sJYstB5U0fi8UdiHHAlG4tHVDuX_e-o

我想使用我的代码完成此操作,但我在这里做错了。

【问题讨论】:

  • 您的Autocomplete 构造函数不正确,它只需要两个参数:Autocomplete(inputField[, opts])。其中一个"opts" 是一个对象并且是可选的,根据docs

标签: javascript google-maps autocomplete


【解决方案1】:

一种选择是使用AutocompleteOption.bounds。如果您只知道位置和半径,您可以创建一个圆圈并在其上调用 getBounds(没有记录的方法可以使用位置和半径来偏置 javascript 自动完成)。

var location = {lat: 53.2910591, lng: -6.1823276};
  var radius = 1000;
  var circle = new google.maps.Circle({
    center: location,
    radius: radius
  });
  var autocomplete = new google.maps.places.Autocomplete(input, {
    bounds: circle.getBounds()
  });

proof of concept fiddle

代码 sn-p:

// This example requires the Places library. Include the libraries=places
// parameter when you first load the API. For example:
// <script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=places">

function activatePlacesSearch() {
  var input = document.getElementById('project-location-input');
  var location = {lat: 53.2910591, lng: -6.1823276};
  var radius = 1000;
  var circle = new google.maps.Circle({
    center: location,
    radius: radius
  });
  var autocomplete = new google.maps.places.Autocomplete(input, {bounds: circle.getBounds()});
  google.maps.event.addListener(autocomplete, 'place_changed', function(e) {
      const place = autocomplete.getPlace();
      console.log(place);
      lat = place.geometry.location.lat();
      lng = place.geometry.location.lng();
      // draw the map
      document.getElementById('new-project-map-div').style.display = 'block'
      var map = new google.maps.Map(document.getElementById('new-project-map-div'), {
      zoom: 15,
      center: new google.maps.LatLng(lat, lng),
      mapTypeId: google.maps.MapTypeId.ROADMAP,
      mapTypeControl: false,
      streetViewControl: false,
      fullscreenControl:false,
      disableDefaultUI: true,
    });

// Set marker position
  var myMarker = new google.maps.Marker({
      position: new google.maps.LatLng(lat, lng),
      draggable: false
    });

  map.setCenter(myMarker.position);
  myMarker.setMap(map);
  });

}
/* Always set the map height explicitly to define the size of the div
 * element that contains the map. */
#new-project-map-div {
  height: 100%;
}
/* Optional: Makes the sample page fill the window. */
html, body {
  height: 100%;
  margin: 0;
  padding: 0;
}
<div class="pac-card" id="pac-card">
  <div id="pac-container">
    <input id="project-location-input" type="text"
        placeholder="Enter a location">
  </div>
</div>
<div id="new-project-map-div"></div>
<!-- Replace the value of the key parameter with your own API key. -->
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk&libraries=places&callback=activatePlacesSearch"
        async defer></script>

【讨论】:

    【解决方案2】:

    在他们的文档中查看此部分:google places autocomplete location biasing。您也许可以利用 strictboundscomponents 参数。

    如果这不能让您更接近您想要完成的目标,请发布指向 CodeSandbox 的链接以及您的示例,我可以帮助您解决问题。

    干杯! ?

    【讨论】:

    • 谢谢拉兹。我会尽快向 CodeSandbox 发布一些内容。 strictbounds 绝对限制在您的边界内,而biasing 只是更喜欢您的边界,但会返回该边界之外的结果。
    • 另一方面,组件将您限制在特定的国家/州等。再次像严格限制一样,这是一个绝对限制,而不是 locationbias 提供的偏见/偏好。
    • 嘿 Raz,这是 CodeSandbox 的链接。您需要输入自己的 API 密钥才能使其正常工作。您需要在您的帐户上启用 Places API 才能使用它。干杯! codesandbox.io/s/holy-grass-8ew2f?file=/index.html
    【解决方案3】:

    在做了一些调查工作后,这就是我想出的。 似乎除非您在 DOM 上显示地图(如果您不想看到它,可以将其放置在视线之外),否则这将无法正常工作。 这里是an example I found on JSFiddle

    //sn-p

    var map = new google.maps.Map(document.getElementById('map'), {
        center: {lat: 39.742347, lng: -104.941121}, //Denver, CO
        zoom: 12
      });
    

    here's my CodeSandbox example 使用您的沙箱和上面示例中的一些指针。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-19
      • 2023-03-15
      • 2018-08-01
      • 2013-08-24
      • 1970-01-01
      相关资源
      最近更新 更多