gpt4 book ai didi

android - 使用 gps 数据使用纬度和经度计算距离。

转载 作者:太空狗 更新时间:2023-10-29 16:19:25 25 4
gpt4 key购买 nike

我正在尝试使用 gps 纬度和经度数据计算距离。

但是我得不到准确的结果。当我得到一个静态数据时,距离公式就完美了,

public class NWDService extends Service implements LocationListener {


private LocationManager myLocationManager;
private LocationProvider myLocationProvider;

NotificationManager myNotificationManager;
private long frequency;

private Handler handler;
private double total_distance = 0;
private Location currentLocation;
public void onLocationChanged(Location newLocation) {
try {
System.out.println("latitude current :"+currentLocation.getLatitude());
System.out.println("latitude current :"+currentLocation.getLongitude());
System.out.println("latitude new :"+newLocation.getLatitude());
System.out.println("latitude new :"+newLocation.getLongitude());
System.out.println("distance total :"+total_distance);
//System.out.println(distance(22.306813, 73.180239,22.301016, 73.177986, 'K') + " Kilometers\n");
double diff = 0.0;
diff = currentLocation.getLatitude()- newLocation.getLatitude();
System.out.println("difference ::"+diff);
if(diff != 0){

total_distance = total_distance + distance(currentLocation.getLatitude(), currentLocation.getLongitude(), newLocation.getLatitude(), newLocation.getLatitude(), 'K');
//total_distance = distance(22.307309,73.181098,23.030000,72.580000,'K');
handler.post(new Runnable() {

@Override
public void run() {
Toast.makeText(getApplicationContext(),"Total Distance:"+total_distance, Toast.LENGTH_LONG).show();
}
});
}

currentLocation = newLocation;
} catch (Exception e) {
currentLocation = newLocation;
e.printStackTrace();
}
}
private double distance(double lat1, double lon1, double lat2, double lon2, char unit) {
double theta = lon1 - lon2;
double dist = Math.sin(deg2rad(lat1)) * Math.sin(deg2rad(lat2)) + Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * Math.cos(deg2rad(theta));
dist = Math.acos(dist);
dist = rad2deg(dist);
dist = dist * 60 * 1.1515;
if (unit == 'K') {
dist = dist * 1.609344;
} else if (unit == 'N') {
dist = dist * 0.8684;
}
return (dist);
}

/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
/*:: This function converts decimal degrees to radians :*/
/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
private double deg2rad(double deg) {
return (deg * Math.PI / 180.0);
}

/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
/*:: This function converts radians to decimal degrees :*/
/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
private double rad2deg(double rad) {
return (rad * 180.0 / Math.PI);
}

private void myNotify(String text) {
Notification notif = new Notification(R.drawable.ic_launcher, text, System
.currentTimeMillis());
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, Home.class), 0);
notif.setLatestEventInfo(this, "NotWhileDriving", text, contentIntent);
// notif.defaults = Notification.DEFAULT_VIBRATE;
myNotificationManager.notify((int) System.currentTimeMillis(), notif);
}

@Override
public void onCreate() {
super.onCreate();
handler = new Handler();

android.util.Log.d("NWD", "creating");

myLocationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
System.out.println("location manager:"+myLocationManager.getAllProviders());
myLocationProvider = myLocationManager.getProvider("gps");
myNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
updatePreferences();
}
public void updatePreferences() {
// sync local variables with preferences
android.util.Log.d("NWD", "updating preferences");
frequency = 10;
// update the LM with the new frequency
myLocationManager.removeUpdates(this);
myLocationManager.requestLocationUpdates(myLocationProvider.getName(),frequency, 0, this);

}
@Override
public void onDestroy() {
super.onDestroy();
android.util.Log.d("NWD", "destroying");
myLocationManager.removeUpdates(this);
myNotify("stopping");
}

@SuppressWarnings("deprecation")
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
android.util.Log.d("NWD", "starting");

currentLocation = myLocationManager
.getLastKnownLocation(myLocationProvider.getName());

myNotify("starting");
}

public void onProviderDisabled(String arg0) {

}

public void onProviderEnabled(String arg0) {

}

public void onStatusChanged(String arg0, int arg1, Bundle arg2) {

}

@Override
public IBinder onBind(Intent arg0) {
return null; // this is for heavy IPC, not used
}
}

请帮我看看我的代码有什么问题...

最佳答案

只需使用 Location.distanceTo(Location),它会为您提供两个不同 Location 之间的真实距离。

关于android - 使用 gps 数据使用纬度和经度计算距离。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18822823/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com