在 Google Places Apis 中搜索特定城市内的位置

2024-04-16

我正在使用 Google Places Apis 来过滤特定城市内的结果。我能够过滤结果。但它也会显示该城市之外的结果。 例如,如果我设置德里市的 LatLngBounds 并搜索纽约市的位置。它还给了我纽约市的结果(但纽约的 LatLng 不在德里境内)。

如何将结果限制在特定城市?

这是我的 PlaceAutoCompleteAdapter 类

import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.PendingResult;
import com.google.android.gms.common.api.Status;
import com.google.android.gms.common.data.DataBufferUtils;
import com.google.android.gms.location.places.AutocompleteFilter;
import com.google.android.gms.location.places.AutocompletePrediction;
import com.google.android.gms.location.places.AutocompletePredictionBuffer;
import com.google.android.gms.location.places.Places;
import com.google.android.gms.maps.model.LatLngBounds;


import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.concurrent.TimeUnit;

public class PlaceAutocompleteAdapter
        extends ArrayAdapter<AutocompletePrediction> implements Filterable {


    private ArrayList<AutocompletePrediction> mResultList;
    private GoogleApiClient mGoogleApiClient;
    private LatLngBounds mBounds;
    private AutocompleteFilter mPlaceFilter;

    public PlaceAutocompleteAdapter(Context context, GoogleApiClient googleApiClient,
            LatLngBounds bounds, AutocompleteFilter filter) {
        super(context,R.layout.simple_expandble_text_view_item, R.id.text1);
        mGoogleApiClient = googleApiClient;
        mBounds = bounds;
        mPlaceFilter = filter;
    }

    public void setBounds(LatLngBounds bounds) {
        mBounds = bounds;
    }

    @Override
    public int getCount() {
        return mResultList.size();
    }

    @Override
    public AutocompletePrediction getItem(int position) {
        return mResultList.get(position);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = super.getView(position, convertView, parent);
        AutocompletePrediction item = getItem(position);

        TextView textView1 = (TextView) row.findViewById(R.id.text1);
        textView1.setText(item.getDescription());
        return row;
    }

    @Override
    public Filter getFilter() {
        return new Filter() {
            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
                FilterResults results = new FilterResults();
                if (constraint != null) {
                    mResultList = getAutocomplete(constraint);
                    if (mResultList != null) {
                        results.values = mResultList;
                        results.count = mResultList.size();
                    }
                }
                return results;
            }

            @Override
            protected void publishResults(CharSequence constraint, FilterResults results) {
                if (results != null && results.count > 0) {
                    notifyDataSetChanged();
                } else {
                    notifyDataSetInvalidated();
                }
            }

            @Override
            public CharSequence convertResultToString(Object resultValue) {
                if (resultValue instanceof AutocompletePrediction) {
                    return ((AutocompletePrediction) resultValue).getDescription();
                } else {
                    return super.convertResultToString(resultValue);
                }
            }
        };
    }

    private ArrayList<AutocompletePrediction> getAutocomplete(CharSequence constraint) {
        if (mGoogleApiClient.isConnected()) {
            PendingResult<AutocompletePredictionBuffer> results =
                    Places.GeoDataApi
                            .getAutocompletePredictions(mGoogleApiClient, constraint.toString(),
                                    mBounds, mPlaceFilter);
            AutocompletePredictionBuffer autocompletePredictions = results
                    .await(60, TimeUnit.SECONDS);

            final Status status = autocompletePredictions.getStatus();
            if (!status.isSuccess()) {
                Toast.makeText(getContext(), "Error contacting API: " + status.toString(),
                        Toast.LENGTH_SHORT).show();
                autocompletePredictions.release();
                return null;
            }
            return DataBufferUtils.freezeAndClose(autocompletePredictions);
        }
        return null;
    }
}

查看文档:

公共抽象 PendingResult getAutocompletePredictions(GoogleApiClient 客户端、字符串查询、LatLngBounds 边界、AutocompleteFilter 过滤器)根据地点的名称和地址返回查询的自动完成预测。有关更多信息,请参阅开发人员指南。

该方法执行网络查找。

访问此方法受到配额限制。有关更多详细信息,请参阅使用限制。

参数

query要获取自动完成预测的查询字符串。

bounds 地理界限。如果存在,返回的预测将偏向于该区域的地点。

filter用于限制返回的预测的过滤器。如果为 null,则将使用没有约束的过滤器。

“bounds”的描述是返回biased预测。所以这些没有被过滤,而是基于这个界限有偏差。这意味着界限内的结果应该具有更高的优先级,但这些并不是唯一的结果。

UPDATE

自 2018 年 4 月起,Google 增加了指定如何处理自动完成预测中的边界的可能性。现在您可以使用getAutocompletePredictions()的方法GeoDataClient与 一起上课boundsMode范围。

See https://stackoverflow.com/a/50134855/5140781 https://stackoverflow.com/a/50134855/5140781更多细节。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 Google Places Apis 中搜索特定城市内的位置 的相关文章

随机推荐

  • Unity使用Invoke在另一个脚本上调用方法

    我有两个脚本 其中一个重新启动场景 另一个是倒计时器 而不是在第一个脚本中调用重新启动场景方法 但是 它没有重新启动 即使没有错误 我也不明白为什么 重新启动场景的第一个脚本 using UnityEngine using UnityEng
  • 在编译时通过 constexpr 或模板函数获取多维 std::array 的大小

    我用的是三维std array 因为大小在编译时已知 但是 我注意到 size 函数不是静态的 因此 constexpr 模板函数无法访问 我已经找到了下面的演示示例 它估计一维的大小std array 然而 这不适用于二维或更多维度 有没
  • C# 中的并发集合

    我正在寻找一种方法来获得并发收集 in C 或者至少是一个支持的集合并发枚举器 现在我得到了InvalidOperationException当我正在迭代的集合发生变化时 我可以深度复制该集合并使用私人副本 但我想知道是否有更好的方法 代码
  • DisplayMemberPath 在 WPF 中不起作用

    我要显示CustomerList CustomerName财产项目ListBox using ItemsSource DisplayMemberPath仅限财产 但它不起作用 我不想使用DataContext或我的问题中的任何其他绑定 请帮
  • 实体框架 6 和集合

    我正在开发我的第一个实体框架应用程序 我正在使用 EF 版本 6 来自 Nuget 和 net 4 0 然而 我在一些对我来说似乎应该非常简单的事情上遇到了一些困难 我在互联网上发现了很多相互矛盾的建议和解决方案 但是在花了几天时间尝试解决
  • Firebase - Firestore - 使用 collection.add() 获取密钥

    我在使用 Firebase 的新 Firestore 时遇到问题 情况 我有一个collection room 我创建房间collection room add room 我正在尝试做的事情 我需要更新一个房间 为此 我使用 collect
  • 示例 urllib3 和 python 中的线程

    我正在尝试在简单线程中使用 urllib3 来获取多个 wiki 页面 该脚本将 为每个线程创建 1 个连接 我不明白为什么 并永远挂起 urllib3 和线程的任何提示 建议或简单示例 import threadpool from url
  • 将多行转换为一行,并以逗号作为分隔符[重复]

    这个问题在这里已经有答案了 如果我发出SELECT username FROM Users我得到这个结果 username Paul John Mary 但我真正需要的是one所有值均以逗号分隔的行 如下所示 Paul John Mary
  • 计算网格上两点之间恰好有“n”个节点的最短路径

    我在网格上定义了以下 3D 表面 pylab inline def muller potential x y use numpy False Muller potential Parameters x float np ndarray or
  • 实体框架 Fluent API 映射简单的一对多关系

    我有两张桌子 文档 Id DocumentTypeId 标题 详细信息 文档类型 ID 名称 描述 DocumentTypeId 是引用 DocumentTypes 表的外键 IE 所有文件都可以应该 有一个分配给它们的类型 我有两节课 p
  • 多人台球游戏物理模拟[关闭]

    就目前情况而言 这个问题不太适合我们的问答形式 我们希望答案得到事实 参考资料或专业知识的支持 但这个问题可能会引发辩论 争论 民意调查或扩展讨论 如果您觉得这个问题可以改进并可能重新开放 访问帮助中心 help reopen questi
  • 将邻接矩阵转换为 Cytoscape 的 Edgelist(csv 文件)

    我的 csv 文件中有一个大的 200 列 行 邻接矩阵 这详细说明了个体之间的互动 我想将此文件转换为边缘列表 可以手动完成 但需要大量时间 下面显示了一小部分数据 第一个单元格是空格 A B C A 0 0 1 B 0 0 1 C 1
  • (0, _reactI18next.translate) 不是函数

    实际上 我是本地反应新手 在这里我尝试使用 react i18next 将语言更改为阿拉伯语 但在执行时出现以下错误 ReactNativeJS 0 reactI18next translate 不是一个函数 在 0 reactI18nex
  • AppDelegate 或 AppController

    在阅读可可教程时 我注意到一些教程使用AppDelegate还有一些AppController用于定义IBActions打开使用子类的各种窗口NSWindowController 这有某种经验法则吗 我创建一个仅是我的应用程序委托的类 并实
  • 如何使用 Visual Studio 2008 对 C# Web 服务进行单元测试

    您应该如何使用 Visual Studio 2008 对 C 中的 Web 服务进行单元测试 当我生成单元测试时 它会添加对 Web 服务类的实际引用 而不是 Web 引用 它设置以下中指定的属性 http msdn microsoft c
  • 检查 netcdf linux 库版本

    如何确定我的系统中安装了哪个版本的 netcdf 库 有命令行吗 我尝试搜索 netcdf 发现了一堆文件 但无法确定版本号 有没有命令可以检查已安装的任何版本 我在ubuntu上 netCDF 提供nc config用于此目的的命令行工具
  • 在 Rust 中应该如何进行指针算术?

    我知道答案是 你不应该 但为了争论 如何should你做吧 例如 如果您想编写一个替代方案Vec
  • Apache 服务器上的 React、js

    我正在一个react js项目中工作 我有一个安装了apache服务器的云服务器 我的问题是我可以在 apache 服务器上设置我的反应项目吗 正如达文 泰伦所说 react是一种浏览器技术 除了客户端浏览器从服务器下载应用程序之外 一切都
  • 用户模型中带有 uuid 列的 Laravel Sanctum 不保存 tokenable_id

    我尝试使用Laravel 8 x and Laravel sanctum 2 14 2验证我的 API 和 UUID 作为我的主密钥User model 我的定制PersonalAccessToken model use Illuminat
  • 在 Google Places Apis 中搜索特定城市内的位置

    我正在使用 Google Places Apis 来过滤特定城市内的结果 我能够过滤结果 但它也会显示该城市之外的结果 例如 如果我设置德里市的 LatLngBounds 并搜索纽约市的位置 它还给了我纽约市的结果 但纽约的 LatLng