You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Google地址自动补全报错:autocomplete.setBounds is not a function

Google地址自动补全setBounds报错解决方案

问题原因

Google Maps JavaScript API的Autocomplete接口进行了更新,原有的setBounds()方法已被废弃并移除,直接调用该方法会触发TypeError: autocomplete.setBounds is not a function错误。旧版本API中Autocomplete实例支持此方法,但API迭代后该方法被移除,改用setOptions()来配置边界参数。

修复代码

将原代码中的autocomplete.setBounds(circle.getBounds());替换为通过setOptions()设置bounds的写法:

function geolocate() {
  if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(function(position) {
      var geolocation = new google.maps.LatLng(
        position.coords.latitude, position.coords.longitude);
      var circle = new google.maps.Circle({
        center: geolocation,
        radius: position.coords.accuracy
      });
      // 替换原setBounds调用为setOptions
      autocomplete.setOptions({ bounds: circle.getBounds() });
    });
  }
}

说明

修改后,通过setOptions({ bounds: 边界对象 })的方式,依然可以实现原功能——将地址自动补全的结果限制在当前定位的地理范围内,和之前的setBounds()效果一致。

内容的提问来源于stack exchange,提问作者David Brierton

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.24 14:24:55