,所以我基本上是一个应用程序项目,该项目必须扫描数据,并在我坐在东海岸的西海岸的位置。
这里的主要问题是,当我启动应用程序时,它要求允许打开"位置/GPS"。如果完成此操作,我启动扫描太快了,它将获得我的位置0.0 lat和0.0长的时间,这将使我在我和另一个位置之间的距离很疯狂,并且无休止地陷入困境(可能是循环的东西(可能是新秀的东西)我不知道如何退出,请参阅while loop futher down)。
- 我相信我几乎到处都在搜索,但我似乎找不到解决方案,我最好的答案是为其制作睡眠线程计时器,并在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}
我认为您在请求位置更新后不应该updateLocation()
:
if (isGPSEnabled) {
this.canGetLocation = true;
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
updateLocation();
}
在这里,在调用任何onLocationChanged()
之前调用updateLocation()
。我认为这就是为什么您的价值观为" 0"。您应该在onLocationChanged()
中致电updateLocation()
。
希望它有帮助