我正在使用以下功能打开“设置API”对话框,以便用户启用位置服务,然后访问用户的当前位置:
public void onConnected(Bundle bundle) {
if(ContextCompat.checkSelfPermission(getApplicationContext(), android.Manifest.permission.ACCESS_FINE_LOCATION)== PackageManager.PERMISSION_GRANTED) {
googleMapGlobal.setMyLocationEnabled(true);
Location mCurrentLocation = LocationServices.FusedLocationApi.getLastLocation(googleApiClient);
if (mCurrentLocation == null) {
// Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
//getApplicationContext().startActivity(intent);
LocationRequest locationRequest = LocationRequest.create();
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
locationRequest.setInterval(30 * 1000);
locationRequest.setFastestInterval(5 * 1000);
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder().addLocationRequest(locationRequest);
builder.setAlwaysShow(true);
PendingResult<LocationSettingsResult> result =
LocationServices.SettingsApi.checkLocationSettings(googleApiClient, builder.build());
result.setResultCallback(new ResultCallback<LocationSettingsResult>() {
@Override
public void onResult(LocationSettingsResult result) {
final Status status = result.getStatus();
final LocationSettingsStates state = result.getLocationSettingsStates();
switch (status.getStatusCode()) {
case LocationSettingsStatusCodes.SUCCESS:
// All location settings are satisfied. The client can initialize location
// requests here.
break;
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
// Location settings are not satisfied. But could be fixed by showing the user
// a dialog.
try {
// Show the dialog by calling startResolutionForResult(),
// and check the result in onActivityResult().
status.startResolutionForResult(VendorsOnMap.this, 1000);
} catch (IntentSender.SendIntentException e) {
// Ignore the error.
}
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.
break;
}
}
});
} else {
CameraPosition position = CameraPosition.builder()
.target(new LatLng(mCurrentLocation.getLatitude(),
mCurrentLocation.getLongitude()))
.zoom(15f)
.bearing(0.0f)
.tilt(0.0f)
.build();
googleMapGlobal.animateCamera(CameraUpdateFactory
.newCameraPosition(position), null);
LatLng latLng = new LatLng(31.220, 75.750);
Toast.makeText(getApplicationContext(), mCurrentLocation.getLatitude() + ", " + mCurrentLocation.getLongitude(), Toast.LENGTH_LONG).show();
googleMapGlobal.addMarker(new MarkerOptions().position(latLng).title("Marker here"));
}
}
else{
ActivityCompat.requestPermissions(getParent(),
new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, MY_PERMISSION_ACCESS_FINE_LOCATION);
}
}
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
final LocationSettingsStates states = LocationSettingsStates.fromIntent(intent);
if (ContextCompat.checkSelfPermission(getApplicationContext(), android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
{
Toast.makeText(getApplicationContext(), resultCode + " " + Activity.RESULT_OK, Toast.LENGTH_SHORT).show();
switch (requestCode) {
case 1000:
switch (resultCode) {
case Activity.RESULT_OK:
googleMapGlobal.setMyLocationEnabled(true);
Location mCurrentLocation;
mCurrentLocation = LocationServices.FusedLocationApi.getLastLocation(googleApiClient);
CameraPosition position = CameraPosition.builder()
.target(new LatLng(mCurrentLocation.getLatitude(), mCurrentLocation.getLongitude()))
.zoom(15f)
.bearing(0.0f)
.tilt(0.0f)
.build();
googleMapGlobal.animateCamera(CameraUpdateFactory.newCameraPosition(position), null);
LatLng latLng = new LatLng(31.220, 75.750);
Toast.makeText(getApplicationContext(), mCurrentLocation.getLatitude() + ", " + mCurrentLocation.getLongitude(), Toast.LENGTH_LONG).show();
googleMapGlobal.addMarker(new MarkerOptions().position(latLng).title("Marker here"));
break;
case Activity.RESULT_CANCELED:
break;
default:
break;
}
break;
}
}
}
}
问题是当我通过打开位置来使用应用程序时,它运行得非常好但是如果我从应用程序内的“设置API”对话框中打开该位置,则返回的位置为空。请帮忙。
答案 0 :(得分:-1)
让它工作。我在理解getLastLocation()
的工作原理方面犯了一个基本错误。它实际上是在寻找系统而不是应用程序所接收的位置。这是我修改过的代码。我现在使用设置API对话框来启用用户打开位置服务但是
在此之前,我拨打了requestLocationUpdates()
来允许我的应用请求位置更新。
然后我在无限循环中调用getLastLocation()
,直到它返回的值不为空。这样我就得到了设备的当前位置。
public void onConnected(Bundle bundle) {
if(ContextCompat.checkSelfPermission(getApplicationContext(), android.Manifest.permission.ACCESS_FINE_LOCATION)== PackageManager.PERMISSION_GRANTED) {
googleMapGlobal.setMyLocationEnabled(true);
locationRequest = LocationRequest.create();
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
locationRequest.setInterval(0);
locationRequest.setFastestInterval(5 * 1000);
locationRequest.setNumUpdates(5);
LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, locationRequest, this);
// Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
//getApplicationContext().startActivity(intent);
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder().addLocationRequest(locationRequest);
builder.setAlwaysShow(true);
PendingResult<LocationSettingsResult> result =
LocationServices.SettingsApi.checkLocationSettings(googleApiClient, builder.build());
result.setResultCallback(new ResultCallback<LocationSettingsResult>() {
@Override
public void onResult(LocationSettingsResult result) {
final Status status = result.getStatus();
final LocationSettingsStates state = result.getLocationSettingsStates();
switch (status.getStatusCode()) {
case LocationSettingsStatusCodes.SUCCESS:
// All location settings are satisfied. The client can initialize location
// requests here.
while(mCurrentLocation==null) {
if(ContextCompat.checkSelfPermission(getApplicationContext(), android.Manifest.permission.ACCESS_FINE_LOCATION)== PackageManager.PERMISSION_GRANTED)
mCurrentLocation=LocationServices.FusedLocationApi.getLastLocation(googleApiClient);
}
CameraPosition position = CameraPosition.builder()
.target(new LatLng(mCurrentLocation.getLatitude(),
mCurrentLocation.getLongitude()))
.zoom(15f)
.bearing(0.0f)
.tilt(0.0f)
.build();
googleMapGlobal.animateCamera(CameraUpdateFactory
.newCameraPosition(position), null);
LatLng latLng = new LatLng(31.220, 75.750);
Toast.makeText(getApplicationContext(), mCurrentLocation.getLatitude() + ", " + mCurrentLocation.getLongitude(), Toast.LENGTH_LONG).show();
googleMapGlobal.addMarker(new MarkerOptions().position(latLng).title("Marker here"));
break;
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
// Location settings are not satisfied. But could be fixed by showing the user
// a dialog.
try {
// Show the dialog by calling startResolutionForResult(),
// and check the result in onActivityResult().
status.startResolutionForResult(VendorsOnMap.this, 1000);
} catch (IntentSender.SendIntentException e) {
// Ignore the error.
}
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.
break;
}
}
});
}
else{
ActivityCompat.requestPermissions(getParent(),
new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, MY_PERMISSION_ACCESS_FINE_LOCATION);
}
}
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
final LocationSettingsStates states = LocationSettingsStates.fromIntent(intent);
if (ContextCompat.checkSelfPermission(getApplicationContext(), android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
{
Toast.makeText(getApplicationContext(), resultCode + " " + Activity.RESULT_OK, Toast.LENGTH_SHORT).show();
switch (requestCode) {
case 1000:
switch (resultCode) {
case Activity.RESULT_OK:
while (mCurrentLocation==null){
mCurrentLocation=LocationServices.FusedLocationApi.getLastLocation(googleApiClient);
}
CameraPosition position = CameraPosition.builder()
.target(new LatLng(mCurrentLocation.getLatitude(), mCurrentLocation.getLongitude()))
.zoom(15f)
.bearing(0.0f)
.tilt(0.0f)
.build();
googleMapGlobal.animateCamera(CameraUpdateFactory.newCameraPosition(position), null);
LatLng latLng = new LatLng(31.220, 75.750);
Toast.makeText(getApplicationContext(), mCurrentLocation.getLatitude() + ", " + mCurrentLocation.getLongitude(), Toast.LENGTH_LONG).show();
googleMapGlobal.addMarker(new MarkerOptions().position(latLng).title("Marker here"));
break;
case Activity.RESULT_CANCELED:
break;
default:
break;
}
break;
}
}
}
这里mCurrentLocation是Location类型的全局变量。