GPS连接延迟android

问题描述 投票:0回答:1

因此,我基本上坐在一个应用程序项目中,当我坐在东海岸时,必须扫描数据并给出我们说美国西海岸的位置。

这里的主要问题是,当我启动应用程序时,它要求允许打开“位置/ GPS”。如果这样做了,我开始扫描太快,它将获得我的位置0.0LAT和0.0LONG这将给我一个疯狂的距离我和其他位置之间,并陷入无休止的循环(可能是一个新手的事情,我不知道如何退出,再看下面的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}
android gps location delay
1个回答
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()

希望能帮助到你

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.