当我从Google商家信息自动填充中搜索时,它会提供来自世界各地的建议/预测。但是我希望根据用户的位置获得建议。我试图在模拟器中手动更改我的位置,但它仍然无法正常工作。我想我的代码中可能缺少某些东西,但我不知道是什么。请帮忙。
代码:
我的依赖
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'
FindLocation活动
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>
答案 0 :(得分:2)
更新的代码:
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.
}
希望有所帮助
答案 1 :(得分:1)
将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
意图
try
{
Intent intent = new PlaceAutocomplete.IntentBuilder(PlaceAutocomplete.MODE_OVERLAY).setBoundsBias(bounds).setFilter(typeFilter).build(MapActivity.this);
startActivityForResult(intent, PLACE_AUTOCOMPLETE_REQUEST_CODE);
}
答案 2 :(得分:0)
我找到了解决方案,感谢Mahesh Gawhane和Patrick R帮助我。但为了完全回答我的问题,我必须首先获得用户在纬度和经度方面的当前位置。我按照this教程获取了用户在Lat和Lng方面的位置(根据您的应用程序,您可以根据需要自定义代码)。
然后,我必须将这个Lat和Lng坐标输入到LatLngBounds对象中,其中,我按照this教程进行了操作。获得LatLngBounds对象后,我将{{1}}方法调入了地方自动填充功能,请参阅指南here。
现在,地方自动填充功能会给出偏见&#39;建议基于某个有限的&#39;用户所在地区域!通过偏见&#39;我的意思是相对于不在这个范围内的地方而言。区域。