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

Android Places Autocomplete API出现Unknown LocationRestriction type崩溃

问题:Places Autocomplete设置圆形范围限制导致崩溃

我尝试将Places Autocomplete的预测结果限制在指定半径范围内,使用了以下代码:

List<Place.Field> fields = Arrays.asList(Place.Field.NAME, Place.Field.LAT_LNG, Place.Field.ADDRESS, Place.Field.ADDRESS_COMPONENTS);

List<String> countryList = new ArrayList<>();
countryList.add(companyInfo.getCountry());

CircularBounds bounds = CircularBounds.newInstance(new LatLng(businessLocation.getLatitude(), businessLocation.getLongitude()), companyInfo.getRadius() * 1000);

Intent intent = new Autocomplete.IntentBuilder(
        AutocompleteActivityMode.OVERLAY, fields)
        .setCountries(countryList)
        .setLocationRestriction(bounds)
        .setHint(context.getString(R.string.start_typing_an_address))
        .build(context);
startActivityForResult(intent, requestCode);

但应用持续崩溃,报错信息如下:

java.lang.AssertionError: Unknown LocationRestriction type

问题原因与解决办法

  • 问题根源:Places Autocomplete的setLocationRestriction()方法仅支持RectangleBounds类型的范围限制,不直接兼容CircularBounds,这就是触发该报错的核心原因。
  • 解决步骤:
    1. 替换CircularBounds为RectangleBounds,基于中心点和半径计算出矩形的西南、东北端点来构建范围边界。
    2. 使用Google Maps的SphericalUtil工具类计算偏移坐标(需确保项目依赖Google Maps Android API):
      // 确定中心点与半径
      LatLng center = new LatLng(businessLocation.getLatitude(), businessLocation.getLongitude());
      double radius = companyInfo.getRadius() * 1000;
      // 计算矩形的西南、东北端点
      LatLng southwest = SphericalUtil.computeOffset(center, radius, 225);
      LatLng northeast = SphericalUtil.computeOffset(center, radius, 45);
      RectangleBounds bounds = RectangleBounds.newInstance(southwest, northeast);
      
    3. 修改后的完整代码:
      List<Place.Field> fields = Arrays.asList(Place.Field.NAME, Place.Field.LAT_LNG, Place.Field.ADDRESS, Place.Field.ADDRESS_COMPONENTS);
      
      List<String> countryList = new ArrayList<>();
      countryList.add(companyInfo.getCountry());
      
      LatLng center = new LatLng(businessLocation.getLatitude(), businessLocation.getLongitude());
      double radius = companyInfo.getRadius() * 1000;
      LatLng southwest = SphericalUtil.computeOffset(center, radius, 225);
      LatLng northeast = SphericalUtil.computeOffset(center, radius, 45);
      RectangleBounds bounds = RectangleBounds.newInstance(southwest, northeast);
      
      Intent intent = new Autocomplete.IntentBuilder(
              AutocompleteActivityMode.OVERLAY, fields)
              .setCountries(countryList)
              .setLocationRestriction(bounds)
              .setHint(context.getString(R.string.start_typing_an_address))
              .build(context);
      startActivityForResult(intent, requestCode);
      
  • 补充说明:如果需要严格的圆形范围过滤,官方Intent方式无法支持,只能通过调用Places Autocomplete Web API,自行在本地计算地点与中心点的距离并过滤结果。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.26 10:20:02