gpt4 book ai didi

java - Android studio 不检查 mFusedLocationProviderClient.getLastLocation().addOnSuccessListener 并返回当前位置 null

转载 作者:行者123 更新时间:2023-12-01 19:48:37 25 4
gpt4 key购买 nike

我正在尝试从我的应用程序获取更新的位置。但是我的应用程序不会检查此代码块

public void onSuccess(Location location){}mFusedLocationProviderClient.getLastLocation() .addOnSuccessListener(this, new OnSuccessListener(){} 并返回空位置。即使我的设备位置已打开, map 也会显示我的位置。用于访问更新位置的代码如下:

  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

其他一切都正确,包括 list 权限gradle依赖项。可能有人可以帮助我解决我的问题。提前致谢。

最佳答案

如果至少有一个客户端连接到融合位置提供程序,则融合位置提供程序将提供位置。如果客户端连接,它将立即尝试获取位置。如果您的 Activity 是第一个连接的客户端,并且您调用 getLastLocation(),则可能没有足够的时间让第一个位置进入。

getLastLocation() 适用于立即需要位置或根本无法使用位置的情况。如果您确实想等待某个位置,最好使用 requestLocationUpdates() 并等待回调,而不是忙于在线程中等待。

这是使用requestLocationUpdates()获取位置的方法

    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);
}

并且不要忘记删除在 onStoponDestroy 中更新的位置

fusedLocationClient?.removeLocationUpdates(locationCallback)

关于java - Android studio 不检查 mFusedLocationProviderClient.getLastLocation().addOnSuccessListener 并返回当前位置 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52273927/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com