我正在使用Google Map Place API。第一次单击片段时,使用Google的内置UI(AutoCompleteFragment),但当我第二次单击该片段时,出现错误
自动完成活动只有在启用片段后才能启动。
其次,当我单击搜索的地方时,下面的注意是代码。我称它为onCreate方法
private void placeAutoComplete(){
if (!Places.isInitialized()) {
Places.initialize(getApplicationContext(), getString(R.string.google_api_key));
}
// Initialize the AutocompleteSupportFragment.
AutocompleteSupportFragment autocompleteFragment = (AutocompleteSupportFragment)
getSupportFragmentManager().findFragmentById(R.id.autocomplete_fragment);
ImageView searchIcon = (ImageView)((LinearLayout)autocompleteFragment.getView()).getChildAt(0);
searchIcon.setVisibility(View.GONE);
autocompleteFragment.setHint(getString(R.string.whereToToday));
// Specify the types of place data to return.
autocompleteFragment.setPlaceFields(Arrays.asList(Place.Field.ID, Place.Field.NAME , Place.Field.LAT_LNG));
Log.e(TAG, "Before entering into things: " );
// Set up a PlaceSelectionListener to handle the response.
autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
@Override
public void onPlaceSelected(Place place) {
// TODO: Get info about the selected place.
Log.e(TAG, "Place: " + place.getName() + ", " + place.getId());
Toast.makeText(MainActivity.this, "we here baby", Toast.LENGTH_SHORT).show();
LatLng coordinate = place.getLatLng();
CameraUpdate location = CameraUpdateFactory.newLatLngZoom(coordinate, 16);
mMap.animateCamera(location , 1500 , null);
Marker mMarker = mMap.addMarker(new MarkerOptions()
.position(coordinate));
}
@Override
public void onError(Status status) {
// TODO: Handle the error.
Log.e(TAG, "An error occurred: " + status);
Toast.makeText(MainActivity.this, "error", Toast.LENGTH_SHORT).show();
}
});
}
选择位置后,这部分代码根本无法使用
autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
@Override
public void onPlaceSelected(Place place) {
// TODO: Get info about the selected place.
Log.e(TAG, "Place: " + place.getName() + ", " + place.getId());
Toast.makeText(MainActivity.this, "we here baby", Toast.LENGTH_SHORT).show();
LatLng coordinate = place.getLatLng();
CameraUpdate location = CameraUpdateFactory.newLatLngZoom(coordinate, 16);
mMap.animateCamera(location , 1500 , null);
Marker mMarker = mMap.addMarker(new MarkerOptions()
.position(coordinate));
}
@Override
public void onError(Status status) {
// TODO: Handle the error.
Log.e(TAG, "An error occurred: " + status);
Toast.makeText(MainActivity.this, "error", Toast.LENGTH_SHORT).show();
}
});
我的xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".MainActivity">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="50dp"
android:elevation="@dimen/elevation"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
app:cardCornerRadius="@dimen/roundness"
android:layout_marginTop="8dp"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@drawable/nav_light_black"
android:padding="14dp"
android:id="@+id/navigationView"
>
</ImageView>
<!-- <TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_toRightOf="@id/navigationView"
android:background="@android:color/transparent"
android:hint="Where to today?"
android:gravity="center_vertical"
android:id="@+id/whereTo"
android:maxLines="1"
android:layout_marginEnd="50dp"
android:textColor="@color/colorLighterBlack"
android:textSize="@dimen/fontForWhereTo"
>
</TextView>-->
<fragment android:id="@+id/autocomplete_fragment"
android:layout_toRightOf="@id/navigationView"
android:layout_width="match_parent"
android:layout_height="50dp"
android:name="com.google.android.libraries.places.widget.AutocompleteSupportFragment"
/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MapsActivity" />
<Button
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_margin="10dp"
android:background="@android:color/white"
android:id="@+id/backToCurrentLocation"
></Button>
</RelativeLayout>
删除onActivityResult方法解决了我的问题,如果您想在自己的活动中使用onActivityResult,请根据Google文档
如果您正在使用自动完成片段,并且需要重写onActivityResult,则必须调用super.onActivityResult,否则该片段将无法正常运行。
https://developers.google.com/places/android-sdk/autocomplete