【发布时间】:2018-09-21 18:59:37
【问题描述】:
我有这个代码:
// Using this method to center MyLocation as default location when app is started
private void getDeviceLocation() {
Log.d(TAG, "getDeviceLocation: getting the devices current location");
mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
try {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
//Request Location Permission
checkLocationPermission();
} else {
final Task location = mFusedLocationClient.getLastLocation();
location.addOnCompleteListener(this, new OnCompleteListener() {
@Override
public void onComplete(@NonNull Task task) {
if (task.isSuccessful()) {
Log.d(TAG, "onComplete: found location!");
Location mLastLocation = (Location) task.getResult();
// mLastLocation = location;
if (mCurrentLocationMarker != null) {
mCurrentLocationMarker.remove();
}
LatLng latLng = new LatLng(mLastLocation.getLatitude(), mLastLocation.getLongitude());
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.position(latLng);
markerOptions.title("My position");
markerOptions.icon(BitmapDescriptorFactory.fromResource(R.drawable.emptyicon));
mCurrentLocationMarker = mMap.addMarker(markerOptions);
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 15));
} else {
Log.d(TAG, "onComplete: current location is null");
Toast.makeText(MapsActivity.this, "unable to get current location", Toast.LENGTH_SHORT).show();
}
}
});
}
} catch (SecurityException e) {
Log.e(TAG, "getDeviceLocation: SecurityException: " + e.getMessage());
}
这一行:location.addCompleteListener 被 Android Studio 标记为未选中,这会使应用程序崩溃。所以我必须以某种方式让它“检查”。 这发生在 Android Studio 的最新更新补丁之后。
我该如何解决这个问题?
【问题讨论】:
-
你能显示完整的未检查警告吗?
-
getLastLocation 可以返回 null:developers.google.com/android/reference/com/google/android/gms/…
标签: android google-maps geolocation google-maps-markers