2

我正在尝试使用 gps lat 和 long 数据计算距离。

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

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
    }
}

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

4

3 回答 3

6

只需使用它,您就可以在两个不同的 sLocation.distanceTo(Location)之间拉开距离。Location

于 2013-09-16T07:50:35.847 回答
0

首先,我不相信这段代码适用于地球的不同地区。我没有看到 WGS 84 参数,只有一些幻数

 dist = dist * 60 * 1.1515;
 if (unit == 'K') {
     dist = dist * 1.609344;
 } else if (unit == 'N') {
     dist = dist * 0.8684;
 }

你从哪里得到那个的?你听得懂么?

其次,如果您的数据有噪音,您可以通过对 totalDistance 求和来添加噪音。您可能想要检查距离是否高于某个阈值,并且仅在超过距离阈值时才添加 totalDistance。

于 2013-09-16T07:56:40.253 回答
0

您可以使用 Google 距离矩阵 API 来查找两个地点之间距离的准确结果。

有关详细研究,您可以参考:

https://developers.google.com/maps/documentation/distancematrix/

于 2013-09-16T09:06:17.610 回答