我正在尝试根据用户指定的查询类型列出所有位置。该应用程序将显示带有API调用返回的位置标记的地图。
private GoogleMap mMap;
private GoogleApiClient mGoogleApiClient;
static String itemSelected;
PendingResult<PlaceLikelihoodBuffer> result;
static int clicked;
CharSequence[] items = {"accounting", "airport", "amusement_park", "aquarium", "art_gallery","atm","bakery","bank","bar","beauty_salon","bicycle_store","book_store","bowling_alley","bus_station","cafe"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
final SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
mGoogleApiClient = new GoogleApiClient
.Builder(this)
.addApi(Places.GEO_DATA_API)
.addApi(Places.PLACE_DETECTION_API)
.enableAutoManage(this,this)
.build();
final AlertDialog.Builder ad = new AlertDialog.Builder(this);
ad.setTitle("Pick a place")
.setSingleChoiceItems(items, 0, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Log.d("Demo", "Selected :" + items[which]);
clicked = which;
}
})
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@TargetApi(Build.VERSION_CODES.M)
@Override
public void onClick(DialogInterface dialog, int which) {
itemSelected = String.valueOf(items[clicked]);
Log.d("demo",itemSelected);
mapFragment.getMapAsync(MapsActivity.this);
}
})
.setCancelable(false);
AlertDialog alert = ad.create();
ad.show();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
@Override
protected void onResume() {
super.onResume();
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode)
{
case 100:
{
if(grantResults[0]== PackageManager.PERMISSION_GRANTED)
{
result.setResultCallback(new ResultCallback<PlaceLikelihoodBuffer>() {
@Override
public void onResult(@NonNull PlaceLikelihoodBuffer placeLikelihoods) {
Log.d("demo",placeLikelihoods.getStatus()+"");
}
});
}
else
{
Toast.makeText(this,"Permission denied by the user", Toast.LENGTH_LONG).show();
}
}
}
}
/**
* Manipulates the map once available.
* This callback is triggered when the map is ready to be used.
* This is where we can add markers or lines, add listeners or move the camera. In this case,
* we just add a marker near Sydney, Australia.
* If Google Play services is not installed on the device, the user will be prompted to install
* it inside the SupportMapFragment. This method will only be triggered once the user has
* installed Google Play services and returned to the app.
*/
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED)
{
// TODO: Consider calling
MapsActivity.this.requestPermissions(new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION},100);
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
result= Places.PlaceDetectionApi.getCurrentPlace(mGoogleApiClient,null);
final PendingResult<PlaceLikelihoodBuffer> pbuff = Places.PlaceDetectionApi.getCurrentPlace(mGoogleApiClient,null);
pbuff.setResultCallback(new ResultCallback<PlaceLikelihoodBuffer>() {
@Override
public void onResult(@NonNull PlaceLikelihoodBuffer placeLikelihoods) {
LatLngBounds.Builder builder = new LatLngBounds.Builder();
for(PlaceLikelihood placeLikelihood : placeLikelihoods){
if (placeLikelihood.getPlace().getPlaceTypes().contains(itemSelected)){
mMap.addMarker(new MarkerOptions().position(placeLikelihood.getPlace().getLatLng()).title(placeLikelihood.getPlace().getName().toString()).icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE)));
builder.include(placeLikelihood.getPlace().getLatLng());
}
else{
mMap.addMarker(new MarkerOptions().position(placeLikelihood.getPlace().getLatLng()).title(placeLikelihood.getPlace().getName().toString()));
builder.include(placeLikelihood.getPlace().getLatLng());
}
}
placeLikelihoods.release();
LatLngBounds bounds = builder.build();
int padding = 10; // offset from edges of the map in pixels
CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, padding);
mMap.animateCamera(cu);
//mMap.moveCamera(CameraUpdateFactory.newLatLngBounds(builder.build(), 10));
// Remove listener to prevent position reset on camera move.
//mMap.setOnCameraChangeListener(null);
// LatLngBounds.Builder bounds = new LatLngBounds.Builder();
// for (DataItem location : mUserLocations.keySet()) {
// bounds.include(new LatLng(
// Double.parseDouble(location.mData
// .get("latitude")), Double
// .parseDouble(location.mData
// .get("longitude"))));
// }
//
// LatLngBounds mapBounds;
// for (DataItem location : result. ) {
// if (mapBounds==null) {
// LatLng point = new LatLng(Double.parseDouble(location.pbuff.get("latitude")), Double.parseDouble(location.mData.get("longitude")))
// mapBounds =new LatLngBounds(point, point);
// } else {
// mapBounds = mapBounds.including(new LatLng(Double.parseDouble(location.mData.get("latitude")), Double.parseDouble(location.mData.get("longitude"))));
// }
// }
}
});
// Add a marker in Sydney and move the camera
// LatLng sydney = new LatLng(-34, 151);
// mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
// mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
}
}
但该应用程序崩溃时出现以下错误:
java.lang.IllegalStateException:没有包含的点 在com.google.android.gms.common.internal.zzaa.zza(未知来源) 在 com.google.android.gms.maps.model.LatLngBounds $ Builder.build(未知 源)。
我已尝试在SO上发布各种其他链接,但似乎没有任何效果。请帮忙。这是关于SO的第一个问题,如果我犯了任何错误,请告诉我。