首次启动应用程序时无法获取位置



这是我的代码:

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
FusedLocationProviderClient mFusedLocationClient;
LocationRequest mLocationRequest;
LocationCallback mLocationCallback;
private void startLocationUpdates() {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MapsActivity.this,new String[]{Manifest.permission.ACCESS_COARSE_LOCATION,Manifest.permission.ACCESS_FINE_LOCATION},99);
return;
}
mFusedLocationClient.requestLocationUpdates(mLocationRequest,
mLocationCallback,
null );
}
public void getLastLocationFun() {

if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, "Not permitted", Toast.LENGTH_SHORT).show();
}
mFusedLocationClient.getLastLocation().addOnCompleteListener(new OnCompleteListener<Location>() {
@Override
public void onComplete(@NonNull Task<Location> task) {
if (task.isSuccessful()) {
Location location = task.getResult();
mMap.clear();
LatLng currentLocation = new LatLng(location.getLatitude(), location.getLongitude());
mMap.addMarker(new MarkerOptions().position(currentLocation).title("Current Location"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(currentLocation));
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(currentLocation,18.0f));
}
}
});
}
public void reqP(){
ActivityCompat.requestPermissions(MapsActivity.this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION,Manifest.permission.ACCESS_COARSE_LOCATION},
99);
}
public boolean checkP(){
return (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED);
}
private void stopLocationUpdates() {
mFusedLocationClient.removeLocationUpdates(mLocationCallback);
}
protected void createLocationRequest() {
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(2000);
mLocationRequest.setFastestInterval(2000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}
@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.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
if(!checkP()){
reqP();
}
mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
createLocationRequest();
mLocationCallback = new LocationCallback(){
@Override
public void onLocationResult(LocationResult locationResult) {
super.onLocationResult(locationResult);
if(locationResult == null){
return;
}
for (Location location : locationResult.getLocations()) {
// Add a marker on current location and move the camera
mMap.clear();
LatLng currentLocation = new LatLng(location.getLatitude(), location.getLongitude());
mMap.addMarker(new MarkerOptions().position(currentLocation).title("Current Location"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(currentLocation));
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(currentLocation,18.0f));
}
}
};
getLastLocationFun();
startLocationUpdates();
ProcessLifecycleOwner.get().getLifecycle().addObserver(new AppLifecycleListener());
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
}
@Override
protected void onDestroy() {
super.onDestroy();
stopLocationUpdates();
}
public class AppLifecycleListener implements LifecycleObserver {
@OnLifecycleEvent(Lifecycle.Event.ON_START)
public void onMoveToForeground() {
// app moved to foreground
}
@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
public void onMoveToBackground() {
Toast.makeText(MapsActivity.this, "LifeCycleEvent", Toast.LENGTH_SHORT).show();
startService(new Intent(MapsActivity.this, MyService.class));
}
}
}

在这里,我试图使用FusedLocationListener获取位置,并在该位置显示一个标记
但谷歌地图只在应用程序首次启动时显示默认的世界地图
随后,当我从最近的应用程序中删除该应用程序并再次启动该应用程序时,它运行良好
我第一次发布时它就无法定位吗?

您需要将其添加到类中。因此,当用户授予位置权限时,您可以获得位置。

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
List<Fragment> fragments = getSupportFragmentManager().getFragments();
if (requestCode != 0) {
mFusedLocationClient.requestLocationUpdates(mLocationRequest,
mLocationCallback,
null );
}

}

相关内容

  • 没有找到相关文章

最新更新