这是我尝试过的代码,但是当用户没有选择任何内容而只是返回 Activity 时,不会调用 onActivityResult()
方法
我将 PlacePIcker 称为
googleMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
@Override
public void onMapClick(LatLng latLng) {
try {
googleMap.clear();
PlacePicker.IntentBuilder intentBuilder =
new PlacePicker.IntentBuilder();
Intent intent = intentBuilder.build(getApplicationContext());
startActivityForResult(intent, 5);
} catch (GooglePlayServicesRepairableException e) {
e.printStackTrace();
} catch (GooglePlayServicesNotAvailableException e) {
e.printStackTrace();
}
}
});
onActivityResult()
方法
if (requestCode == 5 && resultCode == RESULT_OK) {
Toast.makeText(this,"here",Toast.LENGTH_SHORT).show();
if (data != null) {
Place place = PlacePicker.getPlace(data, this);
if (place != null) {
lat = place.getLatLng().latitude;
lng = place.getLatLng().longitude;
MarkerOptions marker = new MarkerOptions().position(
place.getLatLng()).title("Hello Maps"); marker.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_RED));
googleMap.addMarker(marker);
} else{
Toast.makeText(this,"place",Toast.LENGTH_SHORT).show();
MarkerOptions marker = new MarkerOptions().position(
new LatLng(latitude, longitude)).title("Hello Maps");
lat = latitude;
lng = longitude;
// Changing marker icon
marker.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_ROSE));
// adding marker
googleMap.addMarker(marker);
}
} else{
Toast.makeText(this,"data",Toast.LENGTH_SHORT).show();
MarkerOptions marker = new MarkerOptions().position(
new LatLng(latitude, longitude)).title("Hello Maps");
lat = latitude;
lng = longitude;
// Changing marker icon
marker.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_ROSE));
// adding marker
googleMap.addMarker(marker);
}
当我单击 placepicker
Activity 中的默认后退按钮时,我没有看到任何 toast 被调用
根据文档,它应该将 place.getplace()
返回为 null
When the user selects a place, you can retrieve the place by calling PlacePicker.getPlace(). If the user has not selected a place, the method will return null.
这意味着它应该转到 onActivityResult()
,但在我的例子中,在后面并且没有选择任何位置,它不会转到 onActivityResult()
。
谁能帮我解决这个问题吗?
最佳答案
我认为你错过了 else 语句
if (requestCode == 5 && resultCode == RESULT_OK) {
Toast.makeText(this,"here",Toast.LENGTH_SHORT).show();
if (data != null) {
Place place = PlacePicker.getPlace(data, this);
if (place != null) {
lat = place.getLatLng().latitude;
lng = place.getLatLng().longitude;
MarkerOptions marker = new MarkerOptions().position(
place.getLatLng()).title("Hello Maps"); marker.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_RED));
googleMap.addMarker(marker);
} else{
Toast.makeText(this,"place",Toast.LENGTH_SHORT).show();
MarkerOptions marker = new MarkerOptions().position(
new LatLng(latitude, longitude)).title("Hello Maps");
lat = latitude;
lng = longitude;
// Changing marker icon
marker.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_ROSE));
// adding marker
googleMap.addMarker(marker);
}
} else{
Toast.makeText(this,"data",Toast.LENGTH_SHORT).show();
MarkerOptions marker = new MarkerOptions().position(
new LatLng(latitude, longitude)).title("Hello Maps");
lat = latitude;
lng = longitude;
// Changing marker icon
marker.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_ROSE));
// adding marker
googleMap.addMarker(marker);
}
}else if(requestCode == 5 && resultCode == RESULT_CANCELED){
// handle no selection here
}
关于java - 当未选择任何内容时,PlacePicker Activity 不会调用 `onActivityResult`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30413748/