GPS连接延迟android

时间:2019-03-01 07:49:56

标签: android gps location delay

所以我基本上是坐在一个应用程序项目中,该项目必须扫描数据并给出我坐在美国东海岸的位置,例如美国西海岸。

这里的主要问题是,当我启动应用程序时,它会请求打开“位置/ GPS”的权限。如果完成此操作,并且我过快开始扫描,它将获得我的0.0LAT和0.0LONG位置,这将使我与另一个位置之间的距离达到疯狂,并陷入无休止的while循环中(这可能是菜鸟的事,我不知道如何退出,请进一步查看while循环。

  • 我相信我已经搜索了几乎所有地方,但是似乎找不到解决方法,我最好的答案是为此设置一个睡眠线程计时器,并使其尝试在20秒后找到正确的位置?
  • 我不确定的另一件事是使用onStatusChange,尽管我不确定。

那里有什么主意吗?

while(mLat.equals("0.0") && mLon.equals("0.0")) {

            mLat = String.valueOf(gpsHelper.getLatitude());
            mLon = String.valueOf(gpsHelper.getLongitude());

            Location.distanceBetween(Double.valueOf(mLat), 
            Double.valueOf(mLon), Double.valueOf(lat), Double.valueOf(lon), dist);
            System.out.println("lat: " + lat + "\nlong: " + lon + "\nmLat: " + mLat + "\nmLong: " + mLon + "\n" + "\nDist: " + Arrays.toString(dist));
        } 

这是GPSHelper:

public final class GPSHelper implements LocationListener {

//**************************************************************************/
// VARIABLES
//**************************************************************************/
//region Variables

private String TAG = "GPSHelper";

// Context using GPS
private final Context mContext;

// Flag for GPS status
private boolean canGetLocation = false;

// Properties
private Location location;
private double latitude;
private double longitude;
private double speed;

// The minimum Distance to change Updates in meters
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 1; // 1 meter
// The minimum time between updates in milliseconds
private static final long MIN_TIME_BW_UPDATES = 1; // 1 millisecond

// Declaring a Location Manager
private LocationManager locationManager;

//endregion

//**************************************************************************/
// PROPERTIES
/***************************************************************************/
//region Properties

public Location getLocation() {
    return location;
}

private void setLocation(Location location) {
    this.location = location;
}

//endregion

//**************************************************************************/
// CONSTRUCTOR
/***************************************************************************/
//region Constructor

public GPSHelper(Context context) {
    this.mContext = context;
    connectToGPS();
}

//endregion

//**************************************************************************/
// FUNCTIONS
//**************************************************************************/
//region Functions

//***************************************************/
// Connect til GPS
//***************************************************/
public void connectToGPS() {
    try {
        locationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);

        // Flag for GPS turned on
        boolean isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
        Log.i(TAG, "Enabled: " + isGPSEnabled);

        // Is GPS turned on
        if (isGPSEnabled) {
            this.canGetLocation = true;

            locationManager.requestLocationUpdates(
                    LocationManager.GPS_PROVIDER,
                    MIN_TIME_BW_UPDATES,
                    MIN_DISTANCE_CHANGE_FOR_UPDATES, this);

            updateLocation();
        } else {
            this.canGetLocation = false;
            this.setLocation(null);
        }
    } catch (SecurityException e) {
        e.printStackTrace();
    }


}

//***************************************************/
// Update location
//***************************************************/
private void updateLocation() throws SecurityException {
    // Get location
    if (locationManager != null) {
        if (getLocation() != null) {
            latitude = getLocation().getLatitude();
            longitude = getLocation().getLongitude();
            speed = getLocation().getSpeed();
        }
    }
}

//***************************************************/
// Stop use of GPS
//***************************************************/
public void disconnectFromGPS() {
    if (locationManager != null) {
        locationManager.removeUpdates(GPSHelper.this);
    }
}

//***************************************************/
// Get latitude/Breddegrad
//***************************************************/
public double getLatitude() {
    updateLocation();

    if (getLocation() != null) {
        return latitude;
    } else {
        return 0;
    }
}

//***************************************************/
// Get longitude/Længdegrad
//***************************************************/
public double getLongitude() {
    updateLocation();

    if (getLocation() != null) {
        return longitude;
    } else {
        return 0;
    }
}

//***************************************************/
// Get speed
//***************************************************/
public double getSpeed() {
    updateLocation();

    if (getLocation() != null) {
        double tempSpeed = speed / 3.6;
        //DecimalFormat  df = new DecimalFormat("#");
        //tempSpeed = Double.valueOf(df.format(tempSpeed));
        //tempSpeed = Math.round(tempSpeed);
        return tempSpeed;
    } else {
        return 0;
    }
}

//***************************************************/
// Check for connection to satellites
//***************************************************/
public boolean canGetLocation() {

    return this.canGetLocation;

}

//***************************************************/
// Ask user to turn on GPS
//***************************************************/
public void showSettingsAlert() {
    AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);

    // Set title
    alertDialog.setTitle(mContext.getString(R.string.gps_helper_gps_status));
    // Set message
    alertDialog.setMessage(mContext.getString(R.string.gps_helper_gps_is_not_enabled));

    // "Ja" button
    alertDialog.setPositiveButton(mContext.getString(R.string.yes),
            (dialog, which) -> {
                Intent intent = new Intent(
                        Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                mContext.startActivity(intent);
            });

    // "Nej" button
    alertDialog.setNegativeButton(mContext.getString(R.string.no),
            (dialog, which) -> dialog.cancel());

    // Show message
    alertDialog.show();
}

//endregion

//**************************************************************************/
// EVENTS
//**************************************************************************/
//region Events

@Override
public void onLocationChanged(Location location) {
    this.setLocation(location);

    latitude = location.getLatitude();
    longitude = location.getLongitude();
    speed = location.getSpeed();
}

@Override
public void onStatusChanged(String s, int i, Bundle bundle) {

}

@Override
public void onProviderEnabled(String s) {

}

@Override
public void onProviderDisabled(String s) {

}
//endregion}

1 个答案:

答案 0 :(得分:0)

我认为您不应该在请求位置更新后updateLocation()

 if (isGPSEnabled) {
        this.canGetLocation = true;
    locationManager.requestLocationUpdates(
            LocationManager.GPS_PROVIDER,
            MIN_TIME_BW_UPDATES,
            MIN_DISTANCE_CHANGE_FOR_UPDATES, this);

    updateLocation();
}

在这里,updateLocation()在调用任何onLocationChanged()之前被调用。我认为这就是为什么您的值是“ 0”的原因。您应该在updateLocation()内致电onLocationChanged()

希望有帮助