gpt4 book ai didi

Android Google Maps LocationManager 返回我两小时前所在的位置

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

我正在开发一个应用程序,它在一个地方显示 map 并记录用户当前位置。

我的手机 ATM 上有这个应用程序。

两个小时前,我穿过市中心的公园。我一直启用移动网络,WiFi 已经禁用了几天,GPS 也禁用了。

回到离那个地方10公里的家。

启动应用程序,打开 map fragment ,最后一个已知位置似乎是我两小时前所在的那个公园的某个地方。

这是我的代码。

public static void setUpMapIfNeeded() {
if (map == null) {
mapFragment = (MapFragment) fragmentManager.findFragmentById(R.id.location_map);
map = mapFragment.getMap();
if (map != null) {
setUpMap();
}
}
}

private static void setUpMap() {

map.setMyLocationEnabled(false);
map.setOnMapClickListener(new OnMapClickListener() {

@Override
public void onMapClick(LatLng latlng) {

if (marker != null) {
marker.remove();
}
marker = map.addMarker(new MarkerOptions().position(latlng).icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)));
latitude = latlng.latitude;
longitude = latlng.longitude;
cachedLocation = addressFromLocation(latitude, longitude);
}
});

Location myLocation = null;
LocationManager manager = (LocationManager) a.getSystemService(Context.LOCATION_SERVICE);
if (manager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
myLocation = manager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
Log.d("NETWORK_PROVIDER", addressFromLocation(myLocation.getLatitude(), myLocation.getLongitude()));
}
if (myLocation == null) {
myLocation = manager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
Log.d("GPS_PROVIDER", addressFromLocation(myLocation.getLatitude(), myLocation.getLongitude()));
}

if (myLocation != null) {
map.clear();
map.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(myLocation.getLatitude(), myLocation.getLongitude()), 15));
if (marker != null) {
marker.remove();
}
LatLng latlng = new LatLng(myLocation.getLatitude(), myLocation.getLongitude());
marker = map.addMarker(new MarkerOptions().position(latlng).icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)));
latitude = myLocation.getLatitude();
longitude = myLocation.getLongitude();
cachedLocation = addressFromLocation(latitude, longitude);

}

} // End of setUpMap

addressFromLocation 是一个自定义方法,它返回一个字符串,其中包含获得的第一个地址行。

当我运行它时,日志中显示的行是:
Log.d("NETWORK_PROVIDER", addressFromLocation(myLocation.getLatitude(), myLocation.getLongitude()));

它返回两个小时前的位置。

有没有办法强制它检查位置?

最佳答案

需要咨询here
详情。这里总结了对您有帮助的部分。

为您的案例定义两个重要变量,如下所示(当然还需要其他变量)

// The minimum distance to change Updates in meters
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters

// The minimum time between updates in milliseconds
private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; //

public Location getLocation() {
try {
locationManager = (LocationManager) mContext
.getSystemService(LOCATION_SERVICE);

// getting GPS status
isGPSEnabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);

// getting network status
isNetworkEnabled = locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

if (!isGPSEnabled && !isNetworkEnabled) {
// no network provider is enabled
} else {
this.canGetLocation = true;
// First get location from Network Provider
if (isNetworkEnabled) {
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("Network", "Network");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
// if GPS Enabled get lat/long using GPS Services
if (isGPSEnabled) {
if (location == null) {
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("GPS Enabled", "GPS Enabled");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
}
}

} catch (Exception e) {
e.printStackTrace();
}

return location;
}

关于Android Google Maps LocationManager 返回我两小时前所在的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25427011/

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