我正在尝试从我的应用程序中获取更新的位置。 但是我的应用程序不检查此代码块
public void onSuccess(Location location({}insidemFusedLocationProviderClient.getLastLocation(( .addOnSuccessListener(this, new OnSuccessListener(({}并返回 me null location.甚至我的设备位置也已打开,地图会显示我的位置。我用于访问更新位置的代码如下:
protected void onCreate(Bundle savedInstanceState) {
mFusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);
}
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
Toast.makeText(this, "Map is ready", Toast.LENGTH_SHORT).show();
if (mLocationPermissionGranted) {
getDeviceLocation();
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
return;
}
mMap.setMyLocationEnabled(true);
}
}
private void getDeviceLocation(){
try {
if(mLocationPermissionGranted) //true in my case
{
mFusedLocationProviderClient.getLastLocation()
.addOnSuccessListener(this, new OnSuccessListener<Location>() {
@Override
public void onSuccess(Location location) {
// Got last known location. In some rare situations this can be null.
if (location != null) {
moveCamera(new LatLng(location.getLatitude(),location.getLongitude()),17);
// mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(location.getLatitude(),location.getLongitude()),12),2000,null);
}
else{
Toast.makeText(MapsActivity.this, "Unable to get current location", Toast.LENGTH_SHORT).show();
}
}
});
}
}catch (SecurityException ex){
}
}
我在堆栈跟踪中收到以下异常
09-11 15:12:14.525 5864-5864/? E/Zygote: v2
09-11 15:12:14.530 5864-5864/? E/Zygote: accessInfo : 0
09-11 15:12:27.333 5864-5864/com.example.muzammil.maptestingproject E/BoostFramework: BoostFramework() : Exception_1 = java.lang.ClassNotFoundException: Didn't find class "com.qualcomm.qti.Performance" on path: DexPathList[[],nativeLibraryDirectories=[/system/lib, /vendor/lib]]
09-11 15:12:39.327 5864-5864/com.example.muzammil.maptestingproject E/art: The String#value field is not present on Android versions >= 6.0
其他一切都是正确的,包括清单权限和gradle 依赖项。可能是任何身体帮助我解决我的问题。提前谢谢。
如果至少有一个客户端连接到该位置,则融合位置提供程序将提供该位置。如果客户端连接,它将立即尝试获取位置。如果您的活动是第一个要连接的客户端,并且您呼叫getLastLocation()
则可能没有足够的时间让第一个位置进入。
getLastLocation()
适用于立即需要位置或根本无法使用的位置。如果您真的想等待某个位置,最好使用requestLocationUpdates()
并等待回调,而不是忙于在线程中等待。
这是使用requestLocationUpdates()
获取location
的方法
LocationCallback locationCallback;
LocationRequest locationRequest;
FusedLocationProviderClient fusedLocationClient;
void getLocation() {
locationRequest = LocationRequest.create()
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
.setInterval(30 * 1000)
.setFastestInterval(5 * 1000);
locationCallback = new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
super.onLocationResult(locationResult);
//Location received
}
};
fusedLocationClient = LocationServices.getFusedLocationProviderClient(getApplicationContext());
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
fusedLocationClient.requestLocationUpdates(locationRequest, locationCallback, null);
}
并且不要忘记删除在onStop
或onDestroy
中更新的位置
fusedLocationClient?.removeLocationUpdates(locationCallback)