在Google地图Apis中搜索特定城市内的地点 [英] Search locations inside a particular city in Google Places Apis

查看:380
本文介绍了在Google地图Apis中搜索特定城市内的地点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Google Places Apis过滤某个城市内的结果。我可以过滤搜索结果,但它也显示了该城市的结果。
例如,如果我设置了DELHI城市的LatLngBounds并在城市NEWYORK中搜索某个位置。它也给了我NEWYORK城市的成果(但NEWYORK的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;


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

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

public class PlaceAutocompleteAdapter
extends ArrayAdapter< AutocompletePrediction>实现可过滤{


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

public PlaceAutocompleteAdapter(Context context,GoogleApiClient googleApiClient,
LatLngBounds界限,AutocompleteFilter过滤器){
super(context,R.layout.simple_expandble_text_view_item,R.id.text1);
mGoogleApiClient = googleApiClient;
mBounds =边界;
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());
返回行;

$ b @Override
public Filter getFilter(){
return new Filter(){
@Override
Filter FilterResults performFiltering(CharSequence约束){
FilterResults results = new FilterResults();
if(constraint!= null){
mResultList = getAutocomplete(constraint);
if(mResultList!= null){
results.values = mResultList;
results.count = mResultList.size();
}
}
返回结果;
}
$ b $ @覆盖
protected void publishResults(CharSequence约束,FilterResults结果){
if(results!= null&& results.count> 0 ){
notifyDataSetChanged();
} else {
notifyDataSetInvalidated();

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

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

final状态= autocompletePredictions.getStatus();
if(!status.isSuccess()){
Toast.makeText(getContext(),Error contact API:+ status.toString(),
Toast.LENGTH_SHORT).show( );
autocompletePredictions.release();
返回null;
}
返回DataBufferUtils.freezeAndClose(autocompletePredictions);
}
返回null;
}
}


解决方案

在文档中:
$ b



公共抽象PendingResult getAutocompletePredictions(GoogleApiClient客户端,字符串查询,LatLngBounds边界, AutocompleteFilter过滤器)
根据地点的名称和地址返回查询的自动完成预测。

此方法执行网络查找。



访问此方法受限于配额。请参阅使用限制以获取更多详细信息。



参数



查询自动完成预测将被提取。



边界 地理边界。如果存在,返回的预测将偏向该地区。

过滤器用于限制返回预测的过滤器。


bounds的描述是返回偏向的强大的预测。所以这些不是过滤的,但是基于这个界限是有偏见的。这意味着绑定内部的结果应该具有更高的优先级,但这不会是唯一的结果。



更新



截至2018年4月,Google增加了指定如何处理自动填充预测范围的可能性。现在,您可以使用 boundsMode getAutocompletePredictions()方法 GeoDataClient $ c>参数。



请参阅 https://stackoverflow.com/a / 50134855/5140781 了解更多详情。

I am using Google Places Apis to filter results inside a particular city.I am able to filter results.but it also shows results out side of that city. For example if I set LatLngBounds of DELHI city and searching for a location in city NEWYORK. It also gives me result of NEWYORK city(but NEWYORK's LatLng is not lies inside DELHI).

How can I restrict my results to a particular city?

This is my PlaceAutoCompleteAdapter class

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;
    }
}

解决方案

Looking at the documentation:

public abstract PendingResult getAutocompletePredictions (GoogleApiClient client, String query, LatLngBounds bounds, AutocompleteFilter filter) Returns autocomplete predictions for a query based on the names and addresses of places. For more information, see the developer's guide.

This method performs a network lookup.

Access to this method is subject to quota restrictions. See Usage Limits for more details.

Parameters

query The query string for which the autocomplete predictions are to be fetched.

bounds A geographic bounds. If present, returned predictions will be biased towards places in this area.

filter A filter to use for restricting the returned predictions. If null, a filter with no constraints will be used.

The description for "bounds" is to return biased predictions. So those are not filtered, but biased basing on this bounds. This means that the results inside the bound should have higher priority, but those will not be the only results.

UPDATE

As of April 2018 Google added possibility to specify how to treat the bounds in autocomplete predictions. Now you can use the getAutocompletePredictions() method of GeoDataClient class with boundsMode parameter.

See https://stackoverflow.com/a/50134855/5140781 for more details.

这篇关于在Google地图Apis中搜索特定城市内的地点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆