gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 08:25:28 25 4
gpt4 key购买 nike

当我从 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'

查找位置 Activity

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) {

}
}

我的 Activity 的 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>

最佳答案

更新代码:

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

希望对你有帮助

关于android - Google Places API 不返回基于位置的建议,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45726759/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com