use*_*539 20 performance gps android location
我需要从gps获得速度和前进.但是我唯一的号码location.getSpeed()是0或有时不可用.我的代码:
String provider = initLocManager();
if (provider == null)
return false;
LocationListener locListener = new LocationListener() {
public void onLocationChanged(Location location) {
updateWithNewLocation(location, interval, startId);
Log.i(getString(R.string.logging_tag), "speed =" + location.getSpeed());
}
public void onProviderDisabled(String provider){
updateWithNewLocation(null, interval, startId);
}
public void onProviderEnabled(String provider) {}
public void onStatusChanged(String provider, int status, Bundle extras) {}
};
_locManager.requestLocationUpdates(provider, interval, DEFAULT_GPS_MIN_DISTANCE, locListener);
private String initLocManager() {
String context = Context.LOCATION_SERVICE;
_locManager = (LocationManager) getSystemService(context);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(true);
criteria.setSpeedRequired(true);
criteria.setCostAllowed(true);
//criteria.setPowerRequirement(Criteria.POWER_LOW);
String provider = _locManager.getBestProvider(criteria, true);
if (provider == null || provider.equals("")) {
displayGPSNotEnabledWarning(this);
return null;
}
return provider;
}
Run Code Online (Sandbox Code Playgroud)
我尝试使用Criteria但没有成功.有谁知道这是什么问题?
Joh*_*hey 20
location.getSpeed()仅返回使用location.setSpeed()设置的内容.这是您可以为位置对象设置的值.
要使用GPS计算速度,您需要做一些数学运算:
Speed = distance / time
Run Code Online (Sandbox Code Playgroud)
所以你需要这样做:
(currentGPSPoint - lastGPSPoint) / (time between GPS points)
Run Code Online (Sandbox Code Playgroud)
全部转换为英尺/秒,或者您想要显示速度.这就是我制作跑步者应用程序时的方法.
更具体地说,您需要计算绝对距离:
(sqrt((currentGPSPointX - lastGPSPointX)^2) + (currentGPSPointY - lastGPSPointY)^2)) / (time between GPS points)
Run Code Online (Sandbox Code Playgroud)
制作一个新的TrackPoint类或其他东西可能有所帮助,它可以保持GPS的位置和时间.
如果有速度,我的自定义LocationListener用于手动获取速度和位置对象.
new LocationListener() {
private Location mLastLocation;
@Override
public void onLocationChanged(Location pCurrentLocation) {
//calcul manually speed
double speed = 0;
if (this.mLastLocation != null)
speed = Math.sqrt(
Math.pow(pCurrentLocation.getLongitude() - mLastLocation.getLongitude(), 2)
+ Math.pow(pCurrentLocation.getLatitude() - mLastLocation.getLatitude(), 2)
) / (pCurrentLocation.getTime() - this.mLastLocation.getTime());
//if there is speed from location
if (pCurrentLocation.hasSpeed())
//get location speed
speed = pCurrentLocation.getSpeed();
this.mLastLocation = pCurrentLocation;
////////////
//DO WHAT YOU WANT WITH speed VARIABLE
////////////
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
@Override
public void onProviderEnabled(String s) {
}
@Override
public void onProviderDisabled(String s) {
}
};
Run Code Online (Sandbox Code Playgroud)
Imbru的答案看起来非常好,但是如果你正在与单位合作,那就没有用了.
这就是我用来计算速度(米/秒)(m/s)的方法.
new LocationListener() {
private Location lastLocation = null;
private double calculatedSpeed = 0;
@Override
public synchronized void onLocationChanged(Location location) {
if (lastLocation != null) {
double elapsedTime = (location.getTime() - lastLocation.getTime()) / 1_000; // Convert milliseconds to seconds
calculatedSpeed = lastLocation.distanceTo(location) / elapsedTime;
}
this.lastLocation = location;
double speed = location.hasSpeed() ? location.getSpeed() : calculatedSpeed;
/* There you have it, a speed value in m/s */
. . .
}
. . .
}
Run Code Online (Sandbox Code Playgroud)
小智 5
在球形行星上,应使用以下公式计算距离:
private static Double distance(Location one, Location two) {
int R = 6371000;
Double dLat = toRad(two.getLatitude() - one.getLatitude());
Double dLon = toRad(two.getLongitude() - one.getLongitude());
Double lat1 = toRad(one.getLatitude());
Double lat2 = toRad(two.getLatitude());
Double a = Math.sin(dLat / 2) * Math.sin(dLat / 2)
+ Math.sin(dLon / 2) * Math.sin(dLon / 2) * Math.cos(lat1) * Math.cos(lat2);
Double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
Double d = R * c;
return d;
}
private static double toRad(Double d) {
return d * Math.PI / 180;
}
Run Code Online (Sandbox Code Playgroud)