为什么手机gps在不同的位置提供相同的坐标

Osm*_*oni 5 gps android android-service android-gps

背景:我正在开发一款Android应用程序,用于跟踪我需要每10秒获取一次设备位置的车辆,以及管理决策.我们已经开发了这个应用程序,它按时提供我们预期的时间间隔.

问题:当车辆在道路上行驶时,应用程序会给我们正确的坐标作为我们预期的时间间隔,但有时5-20分钟它提供相同的坐标,但车辆位于不同的位置.

代码审查: 1)我们在LocationClass中使用名为getLocation()的方法,它为我们提供设备当前位置的纬度经度.

private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 5;
private static final long MIN_TIME_BW_UPDATES = 1000*20; 

public Location getLocation(Context context) {
    Location location=null;
    try {
        locationManager = (LocationManager) context
                .getSystemService(Context.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;
            if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                Toast.makeText(context,"Please! Provide Location Access Permission.....", Toast.LENGTH_LONG).show();
                return TODO;
            }

            // if GPS Enabled get lat/long using GPS Services               

                if(isGPSEnabled) {
                    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();
                            location.setProvider("GPS");                              
                        }
                    }
                }
                if (isNetworkEnabled && location==null) {
                    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();
                            location.setProvider("AGPS");                                
                        }
                    }
                }                

        }

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

    return location;
}
Run Code Online (Sandbox Code Playgroud)

2)我们以固定的时间间隔使用名为GPSTracker的服务上的计时器重复此方法.

public class GPSTracker extends Service implements LocationListener {
private Timer mTimer = null;
private Handler mHandler = new Handler();
Location location;
@Override
public void onCreate() {
    super.onCreate();

    if (mTimer != null) {
        mTimer.cancel();
    } else {
        // recreate new
        mTimer = new Timer();
    }
    // schedule task
    mTimer.scheduleAtFixedRate(new TimeDisplayTimerTask(), 10 * 1000, 10 * 1000);
}

class TimeDisplayTimerTask extends TimerTask {

    @Override
    public void run() {
        // run on another thread
        mHandler.post(new Runnable() {

            @Override
            public void run() {
                location=new Location("");
                LocationCls cls = new LocationCls();
                location = cls.getLocation(getBaseContext());
            }

        });
    }

}
}
Run Code Online (Sandbox Code Playgroud)

我试图解决这个问题2天,但尚未找到任何编码问题或解决方案.任何解决方案或建议都非常感谢.

提前致谢....

小智 5

根据你的代码,

locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER)

如果在当前请求中找不到坐标,则getLastKnownLocation提供最后一次已知坐标.您应该使用以下方法获取新位置...

@Override
public void onLocationChanged(Location location) {
    double lat=location.getLatitude();
    double lng=location.getLongitude();
}
Run Code Online (Sandbox Code Playgroud)

它总是为您提供新的位置而不是最后的已知位置.

另一方面,您可以使用LocationServices API为您的问题提供服务.尝试使用以下链接:https://guides.codepath.com/android/Retrieving-Location-with-LocationServices-API