gpt4 book ai didi

android - 如何获得我在 android 中的最后一个位置?

转载 作者:行者123 更新时间:2023-11-29 02:25:41 25 4
gpt4 key购买 nike

我想知道如何获得我最后的已知位置(提供的 GPS 或网络)。我问这个是因为我知道如何获取我的位置,但它会定期更新,我只需要一次,不再需要。

这是我的实际代码:

@Override
public void onLocationChanged(Location location) {
Toast.makeText(getContext(),"Cambio posicion "+location.getProvider(),Toast.LENGTH_SHORT).show();

this.marca.title("Mi ubicacion");
this.marca.position(new LatLng(location.getLatitude(),location.getLongitude()));

mMap.addMarker(this.marca);

this.camara = CameraPosition.builder()
.bearing(0)
.zoom(15)
.target(new LatLng(location.getLatitude(),location.getLongitude()))
.tilt(30)
.build();

mMap.animateCamera(CameraUpdateFactory.newCameraPosition(this.camara));


}

我正在使用 LocationManager。感谢您的帮助。

最佳答案

如果您正在使用 LocationManager 并且您只想获取最后一个位置一次。您可以在第一次 onLocationChanged 之后删除监听位置更新,例如

@Override
public void onLocationChanged(Location location) {

mLocationManager.removeUpdates(mListener);
}

我认为可能有帮助的另一个解决方案是。您可以使用 GoogleLocationService 而不是 LocationManager。然后你可以使用这个方法来获得get last known location

mFusedLocationClient.getLastLocation()
.addOnSuccessListener(this, new OnSuccessListener<Location>() {
@Override
public void onSuccess(Location location) {
// Got last known location
}
});

关于android - 如何获得我在 android 中的最后一个位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52396140/

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