我正试图为(我认为)过时的代码找到解决方案。我使用的FusedLocationApi.getLastLocation
总是returns null
,因此我无法获取位置。我总是得到吐司“无法获得您的位置”。
private void displayLocation() {
if(ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
requestRuntimePermission();
}
else {
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
if(mLastLocation != null) {
double latitude = mLastLocation.getLatitude();
double longitude = mLastLocation.getLongitude();
//Add marker in your location and move the camera
LatLng yourLocation = new LatLng(latitude,longitude);
mMap.addMarker(new MarkerOptions().position(yourLocation).title("Your location"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(yourLocation));
mMap.animateCamera(CameraUpdateFactory.zoomTo(17.0f));
}
else {
Toast.makeText(this, "Could not get your location", Toast.LENGTH_SHORT).show();
}
}
}
答案 0 :(得分:-1)
这是更新的代码,如何使用新的熔融LocationApi
private void requestLocation() {
if (!permissionGranted) return;
locationRequest = LocationRequest.create();
locationRequest.setPriority(priority);
if (updateInterval != 0)
locationRequest.setInterval(updateInterval);
if (meter != 0)
locationRequest.setSmallestDisplacement(meter);
// Create LocationSettingsRequest object using location request
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder();
builder.addLocationRequest(locationRequest);
builder.setAlwaysShow(true);
LocationSettingsRequest locationSettingsRequest = builder.build();
// Check whether location settings are satisfied
// https://developers.google.com/android/reference/com/google/android/gms/location/SettingsClient
SettingsClient settingsClient = LocationServices.getSettingsClient(context);
Task<LocationSettingsResponse> task = settingsClient.checkLocationSettings(locationSettingsRequest);
task.addOnCompleteListener(task1 -> {
locationRequested = true;
try {
LocationSettingsResponse response = task1.getResult(ApiException.class);
// All location settings are satisfied. The client can initialize location
// requests here.
Log.v(TAG, "Location settings satisfied");
locationStatusOk = true;
checkInitialLocation();
} catch (ApiException exception) {
locationRequested = false;
Log.v(" Failed ", String.valueOf(exception.getStatusCode()));
switch (exception.getStatusCode()) {
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
// Location settings are not satisfied. But could be fixed by showing the
// user a dialog.
// Cast to a resolvable exception.
ResolvableApiException resolvable = (ResolvableApiException) exception;
// Show the dialog by calling startResolutionForResult(),
// and check the result in onActivityResult().
try {
resolvable.startResolutionForResult(
activity,
REQUEST_CHECK_SETTINGS);
} catch (IntentSender.SendIntentException e) {
e.printStackTrace();
}
break;
case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
// Location settings are not satisfied. However, we have no way to fix the
// settings so we won't show the dialog.
locationStatusOk = false;
changeSettings = true;
break;
}
}
});
}
这是该类的链接,以获取更多信息,并且其中处理了所有信息: https://github.com/mesunnysaini/locationfetcher/blob/master/LocationFetcher
如果您有任何疑问或问题,请告诉我,我也将分享如何实现它。