Android Google Places API不返回基于位置的建议

Android Google Places API不返回基于位置的建议,android,location,google-places-api,Android,Location,Google Places Api,当我从Google Places Autocomplete搜索时,它会给出来自世界各地的建议/预测。然而,我想根据用户的位置得到建议。我试图在模拟器中手动更改我的位置,但仍然不起作用。我想我的代码中可能缺少一些东西,但我不知道是什么。请帮忙 代码: 我的依赖关系 compile 'com.google.android.gms:play-services-places:10.2.1' compile 'com.google.android.gms:play-services-location:10

当我从Google Places Autocomplete搜索时,它会给出来自世界各地的建议/预测。然而,我想根据用户的位置得到建议。我试图在模拟器中手动更改我的位置,但仍然不起作用。我想我的代码中可能缺少一些东西,但我不知道是什么。请帮忙

代码:

我的依赖关系

compile 'com.google.android.gms:play-services-places:10.2.1'
compile 'com.google.android.gms:play-services-location:10.2.1'
compile 'com.google.android.gms:play-services:10.2.1'
查找定位活动

public class FindLocation extends AppCompatActivity 
        implements GoogleApiClient.ConnectionCallbacks, 
        GoogleApiClient.OnConnectionFailedListener {

    private int PLACE_AUTOCOMPLETE_REQUEST_CODE = 1;
    private GoogleApiClient mGoogleApiClient;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_find_location);

        mGoogleApiClient = new GoogleApiClient
                .Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(Places.GEO_DATA_API)
                .addApi(Places.PLACE_DETECTION_API)
                .enableAutoManage(this, this)
                .build();

        try {
            Intent intent =
                    new PlaceAutocomplete.IntentBuilder(PlaceAutocomplete.MODE_OVERLAY)
                            .build(this);
            startActivityForResult(intent, PLACE_AUTOCOMPLETE_REQUEST_CODE);
        } catch (GooglePlayServicesRepairableException e) {
            // TODO: Handle the error.
        } catch (GooglePlayServicesNotAvailableException e) {
            // TODO: Handle the error.
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == PLACE_AUTOCOMPLETE_REQUEST_CODE) {
            if (resultCode == RESULT_OK) {
                Place place = PlaceAutocomplete.getPlace(this, data);
                //Log.i(TAG, "Place: " + place.getName());
            } else if (resultCode == PlaceAutocomplete.RESULT_ERROR) {
                Status status = PlaceAutocomplete.getStatus(this, data);
                // TODO: Handle the error.
                //Log.i(TAG, status.getStatusMessage());

            } else if (resultCode == RESULT_CANCELED) {
                // The user canceled the operation.
            }
        }
    }

    @Override
    public void onConnected(@Nullable Bundle bundle) {

        //refreshPlacesData();

    }

    @Override
    public void onConnectionSuspended(int i) {

    }

    @Override
    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {

    }
}
我的活动是XML

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.android.project_water.FindLocation">

</android.support.constraint.ConstraintLayout>

将LatLongBounds设置为PlaceAutoComplete,以显示用户当前位置的结果,如

下一行给出了当前位置的边界

    LatLngBounds bounds = mGoogleMap.getProjection().getVisibleRegion().latLngBounds;
    Log.i("curScreen", ""+bounds);
或者您可以设置静态latlongbounds,如

    LatLngBounds latLngBounds = new LatLngBounds(
                new LatLng(19.8036125,75.1932621),
                new LatLng(19.9365798,75.4171406));
然后将过滤器设置为放置自动完成

    final AutocompleteFilter typeFilter = new AutocompleteFilter.Builder()
            .setTypeFilter(AutocompleteFilter.TYPE_FILTER_ADDRESS)
            .setTypeFilter(3)
            .build();
并将
LatLongBounds
AutocompleteFilter
传递到
PlaceAutocomplete
Intent

    try
    {
        Intent intent = new PlaceAutocomplete.IntentBuilder(PlaceAutocomplete.MODE_OVERLAY).setBoundsBias(bounds).setFilter(typeFilter).build(MapActivity.this);
        startActivityForResult(intent, PLACE_AUTOCOMPLETE_REQUEST_CODE);

    }
更新代码:

LatLngBounds bounds = new LatLngBounds( new LatLng(23.8036125,72.1932621), new LatLng(23.9365798,72.4171406));
        final AutocompleteFilter typeFilter = new AutocompleteFilter.Builder()
                .setTypeFilter(AutocompleteFilter.TYPE_FILTER_ADDRESS)
                .build();
        try {

            Intent intent = new PlaceAutocomplete.IntentBuilder(PlaceAutocomplete.MODE_OVERLAY).
                    setBoundsBias(bounds).
                    setFilter(typeFilter).build(LocationPickerActivityDEMO.this);
            startActivityForResult(intent, PLACE_AUTOCOMPLETE_REQUEST_CODE);

        } catch (GooglePlayServicesRepairableException e) {
            // TODO: Handle the error.
        } catch (GooglePlayServicesNotAvailableException e) {
            // TODO: Handle the error.
        }

希望能有所帮助

我找到了解决方案,感谢马赫什·加瓦尼和帕特里克·R对我的帮助。但是为了完整地回答我的问题,我必须首先获得用户的当前位置(纬度和经度)。我按照教程获得了用户在Lat和Lng方面的位置(取决于您的应用程序,您可以根据需要定制代码)

然后,我必须将这个Lat和Lng坐标输入到一个LatLngBounds对象中,我按照教程这样做。有了LatLngBounds对象后,我将
setBoundsBias()
方法调用到Places Autocomplete中,请参见指南


现在,Places Autocomplete将根据用户位置周围的某个“有界”区域给出“偏差”建议!我所说的“偏差”是指相对于不在此“有界”区域内的位置。

Google places API指南建议,只有当我希望此片段显示为片段时,才应添加此片段。但是,我使用intent将启动自动完成作为一项活动。通过从Google控制台启用,您的意思是检索API密钥吗?如果是这样,我已经这样做了。谢谢,但这只适用于特定的位置。我需要它为用户的当前位置工作。您只需要将静态坐标替换为用户的当前位置。如果每次都手动更改坐标,这不是太麻烦了吗?是否有一种方法可以对代码进行编程,使places autocomplete根据用户的位置自动给出建议@Patrick Rattemp对空对象引用调用虚拟方法“com.google.android.gms.maps.Projection com.google.android.gms.maps.GoogleMap.getProjection()”