使用 android 中两个不同点的纬度和经度查找以米为单位的距离

Find distance in meters using latitude and longitude of two different point in android

我正在使用 latlong 寻找两点之间的距离。

我已遵循这些准则。

    /* 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;
    return (dist);*/

    Log.e("distancefinder","Past   lat: "+lat1);
    Log.e("distancefinder","Past   long: "+lon1);
    Log.e("distancefinder","current lat : "+lat2);
    Log.e("distancefinder","current long : "+lon2);

    int R = 6371; // km
    double dLat = toRadians(lat2-lat1);
    double dLon = toRadians(lon2-lon1);
    lat1 = toRadians(lat1);
    lat2 = toRadians(lat2);

    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)); 
    return R * c;

    /*      double earthRadius = 6371000; //meters

    double dLat = Math.toRadians(lat2-lat1);
    double dLng = Math.toRadians(lon2-lon1);
    double a = Math.sin(dLat/2) * Math.sin(dLat/2) +
            Math.cos(Math.toRadians(lat1)) * 
            Math.cos(Math.toRadians(lat2)) *
            Math.sin(dLng/2) * Math.sin(dLng/2);
    double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
    float dist = (float) (earthRadius * c);

    return dist;*/


    /*      double distance;
    Location locationA = new Location("");
    locationA.setLatitude(lat1);
    locationA.setLongitude(lon1);
    Location locationB = new Location("");
    locationB.setLatitude(lat2);
    locationB.setLongitude(lon2);
    distance = locationA.distanceTo(locationB);   //in meters
    return distance;*/
    //      distance = locationA.distanceTo(locationB)/1000;   //in km

但我没有得到以米为单位的准确距离。这是我的日志猫结果:

11-20 12:28:15.621: E/distancefinder(5309): compare lat and long
11-20 12:28:15.621: E/distancefinder(5309): Same latitude and longtitude and return false
11-20 12:28:25.384: E/distancefinder(5592): compare lat and long
11-20 12:28:25.384: E/distancefinder(5592): Same latitude and longtitude and return false
11-20 12:28:30.380: E/distancefinder(5592): compare lat and long
11-20 12:28:30.381: E/distancefinder(5592): Same latitude and longtitude and return false
11-20 12:28:35.404: E/distancefinder(5592): compare lat and long
11-20 12:28:35.404: E/distancefinder(5592): Past   lat: 12.9876692
11-20 12:28:35.404: E/distancefinder(5592): Past   long: 77.5950608
11-20 12:28:35.404: E/distancefinder(5592): current lat : 12.9876766
11-20 12:28:35.404: E/distancefinder(5592): current long : 77.5950719
11-20 12:28:35.404: E/distancefinder(5592): Distance in km :0.0014572341093100844
11-20 12:28:35.417: E/distancefinder(5592):  Distance in mts : 1.4572341093100845
11-20 12:28:40.399: E/distancefinder(5592): compare lat and long
11-20 12:28:40.399: E/distancefinder(5592): Same latitude and longtitude and return false
11-20 12:28:45.403: E/distancefinder(5592): compare lat and long
11-20 12:28:45.403: E/distancefinder(5592): Same latitude and longtitude and return false
11-20 12:28:50.412: E/distancefinder(5592): compare lat and long
11-20 12:28:50.412: E/distancefinder(5592): Same latitude and longtitude and return false
11-20 12:28:55.398: E/distancefinder(5592): compare lat and long
11-20 12:28:55.398: E/distancefinder(5592): Same latitude and longtitude and return false
11-20 12:29:00.381: E/distancefinder(5592): compare lat and long
11-20 12:29:00.381: E/distancefinder(5592): Past   lat: 12.9876766
11-20 12:29:00.381: E/distancefinder(5592): Past   long: 77.5950719
11-20 12:29:00.381: E/distancefinder(5592): current lat : 12.9876807
11-20 12:29:00.381: E/distancefinder(5592): current long : 77.5950323
11-20 12:29:00.382: E/distancefinder(5592): Distance in km :0.004314827685750207
11-20 12:29:00.387: E/distancefinder(5592):  Distance in mts : 4.314827685750207
11-20 12:29:05.407: E/distancefinder(5592): compare lat and long
11-20 12:29:05.407: E/distancefinder(5592): Same latitude and longtitude and return false
11-20 12:29:10.408: E/distancefinder(5592): compare lat and long
11-20 12:29:10.408: E/distancefinder(5592): Same latitude and longtitude and return false
11-20 12:29:15.401: E/distancefinder(5592): compare lat and long
11-20 12:29:15.401: E/distancefinder(5592): Same latitude and longtitude and return false
11-20 12:29:20.405: E/distancefinder(5592): compare lat and long
11-20 12:29:20.405: E/distancefinder(5592): Same latitude and longtitude and return false
11-20 12:29:25.386: E/distancefinder(5592): compare lat and long
11-20 12:29:25.386: E/distancefinder(5592): Past   lat: 12.9876807
11-20 12:29:25.386: E/distancefinder(5592): Past   long: 77.5950323
11-20 12:29:25.386: E/distancefinder(5592): current lat : 12.9876729
11-20 12:29:25.386: E/distancefinder(5592): current long : 77.5950339
11-20 12:29:25.387: E/distancefinder(5592): Distance in km :8.844764712143432E-4
11-20 12:29:25.396: E/distancefinder(5592):  Distance in mts : 0.8844764712143431
11-20 12:29:30.392: E/distancefinder(5592): compare lat and long
11-20 12:29:30.392: E/distancefinder(5592): Same latitude and longtitude and return false
11-20 12:29:35.393: E/distancefinder(5592): compare lat and long
11-20 12:29:35.393: E/distancefinder(5592): Same latitude and longtitude and return false
11-20 12:29:40.401: E/distancefinder(5592): compare lat and long
11-20 12:29:40.402: E/distancefinder(5592): Same latitude and longtitude and return false
11-20 12:29:45.386: E/distancefinder(5592): compare lat and long
11-20 12:29:45.386: E/distancefinder(5592): Same latitude and longtitude and return false
11-20 12:29:50.403: E/distancefinder(5592): compare lat and long
11-20 12:29:50.403: E/distancefinder(5592): Past   lat: 12.9876729
11-20 12:29:50.403: E/distancefinder(5592): Past   long: 77.5950339
11-20 12:29:50.403: E/distancefinder(5592): current lat : 12.9876932
11-20 12:29:50.403: E/distancefinder(5592): current long : 77.5950936
11-20 12:29:50.403: E/distancefinder(5592): Distance in km :0.0068510533659564225
11-20 12:29:50.418: E/distancefinder(5592):  Distance in mts : 6.851053365956423
11-20 12:29:55.404: E/distancefinder(5592): compare lat and long
11-20 12:29:55.404: E/distancefinder(5592): Same latitude and longtitude and return false
11-20 12:30:00.401: E/distancefinder(5592): compare lat and long
11-20 12:30:00.401: E/distancefinder(5592): Same latitude and longtitude and return false
11-20 12:30:05.381: E/distancefinder(5592): compare lat and long
11-20 12:30:05.381: E/distancefinder(5592): Same latitude and longtitude and return false
11-20 12:30:10.419: E/distancefinder(5592): compare lat and long
11-20 12:30:10.419: E/distancefinder(5592): Same latitude and longtitude and return false
11-20 12:30:15.386: E/distancefinder(5592): compare lat and long
11-20 12:30:15.387: E/distancefinder(5592): Past   lat: 12.9876932
11-20 12:30:15.387: E/distancefinder(5592): Past   long: 77.5950936
11-20 12:30:15.387: E/distancefinder(5592): current lat : 12.9876918
11-20 12:30:15.387: E/distancefinder(5592): current long : 77.5950864
11-20 12:30:15.387: E/distancefinder(5592): Distance in km :7.955033100235504E-4
11-20 12:30:15.394: E/distancefinder(5592):  Distance in mts : 0.7955033100235503
11-20 12:30:20.373: E/distancefinder(5592): compare lat and long
11-20 12:30:20.373: E/distancefinder(5592): Same latitude and longtitude and return false
11-20 12:30:25.401: E/distancefinder(5592): compare lat and long
11-20 12:30:25.401: E/distancefinder(5592): Same latitude and longtitude and return false
11-20 12:30:30.411: E/distancefinder(5592): compare lat and long
11-20 12:30:30.411: E/distancefinder(5592): Same latitude and longtitude and return false
11-20 12:30:35.391: E/distancefinder(5592): compare lat and long
11-20 12:30:35.391: E/distancefinder(5592): Same latitude and longtitude and return false
11-20 12:30:40.383: E/distancefinder(5592): compare lat and long
11-20 12:30:40.384: E/distancefinder(5592): Past   lat: 12.9876918
11-20 12:30:40.384: E/distancefinder(5592): Past   long: 77.5950864
11-20 12:30:40.384: E/distancefinder(5592): current lat : 12.9876826
11-20 12:30:40.384: E/distancefinder(5592): current long : 77.5950311
11-20 12:30:40.384: E/distancefinder(5592): Distance in km :0.006078478068578999
11-20 12:30:40.392: E/distancefinder(5592):  Distance in mts : 6.078478068578999
11-20 12:30:45.532: E/distancefinder(5592): compare lat and long
11-20 12:30:45.532: E/distancefinder(5592): Same latitude and longtitude and return false
11-20 12:30:50.402: E/distancefinder(5592): compare lat and long
11-20 12:30:50.402: E/distancefinder(5592): Same latitude and longtitude and return false
11-20 12:30:55.412: E/distancefinder(5592): compare lat and long
11-20 12:30:55.412: E/distancefinder(5592): Same latitude and longtitude and return false
11-20 12:31:00.409: E/distancefinder(5592): compare lat and long
11-20 12:31:00.409: E/distancefinder(5592): Same latitude and longtitude and return false
11-20 12:31:05.387: E/distancefinder(5592): compare lat and long
11-20 12:31:05.387: E/distancefinder(5592): Past   lat: 12.9876826
11-20 12:31:05.387: E/distancefinder(5592): Past   long: 77.5950311
11-20 12:31:05.387: E/distancefinder(5592): current lat : 12.987683
11-20 12:31:05.387: E/distancefinder(5592): current long : 77.5950143
11-20 12:31:05.388: E/distancefinder(5592): Distance in km :0.0018208297478854096
11-20 12:31:05.395: E/distancefinder(5592):  Distance in mts : 1.8208297478854096
11-20 12:31:10.399: E/distancefinder(5592): compare lat and long
11-20 12:31:10.399: E/distancefinder(5592): Same latitude and longtitude and return false
11-20 12:31:15.405: E/distancefinder(5592): compare lat and long
11-20 12:31:15.405: E/distancefinder(5592): Same latitude and longtitude and return false
11-20 12:31:20.405: E/distancefinder(5592): compare lat and long
11-20 12:31:20.405: E/distancefinder(5592): Same latitude and longtitude and return false
11-20 12:31:25.411: E/distancefinder(5592): compare lat and long
11-20 12:31:25.418: E/distancefinder(5592): Same latitude and longtitude and return false
11-20 12:31:30.409: E/distancefinder(5592): compare lat and long
11-20 12:31:30.409: E/distancefinder(5592): Past   lat: 12.987683
11-20 12:31:30.409: E/distancefinder(5592): Past   long: 77.5950143
11-20 12:31:30.409: E/distancefinder(5592): current lat : 12.9876792
11-20 12:31:30.409: E/distancefinder(5592): current long : 77.5950169
11-20 12:31:30.409: E/distancefinder(5592): Distance in km :5.078402770198048E-4
11-20 12:31:30.423: E/distancefinder(5592):  Distance in mts : 0.5078402770198048
11-20 12:31:35.410: E/distancefinder(5592): compare lat and long
11-20 12:31:35.410: E/distancefinder(5592): Same latitude and longtitude and return false
11-20 12:31:40.405: E/distancefinder(5592): compare lat and long
11-20 12:31:40.405: E/distancefinder(5592): Same latitude and longtitude and return false
11-20 12:31:45.395: E/distancefinder(5592): compare lat and long
11-20 12:31:45.395: E/distancefinder(5592): Same latitude and longtitude and return false
11-20 12:31:50.407: E/distancefinder(5592): compare lat and long
11-20 12:31:50.407: E/distancefinder(5592): Same latitude and longtitude and return false
11-20 12:31:55.399: E/distancefinder(5592): compare lat and long
11-20 12:31:55.399: E/distancefinder(5592): Past   lat: 12.9876792
11-20 12:31:55.399: E/distancefinder(5592): Past   long: 77.5950169
11-20 12:31:55.399: E/distancefinder(5592): current lat : 12.9876777
11-20 12:31:55.399: E/distancefinder(5592): current long : 77.5950228
11-20 12:31:55.399: E/distancefinder(5592): Distance in km :6.606681020544278E-4
11-20 12:31:55.414: E/distancefinder(5592):  Distance in mts : 0.6606681020544278
11-20 12:32:00.404: E/distancefinder(5592): compare lat and long
11-20 12:32:00.404: E/distancefinder(5592): Same latitude and longtitude and return false
11-20 12:32:05.404: E/distancefinder(5592): compare lat and long
11-20 12:32:05.404: E/distancefinder(5592): Same latitude and longtitude and return false
11-20 12:32:10.418: E/distancefinder(5592): compare lat and long
11-20 12:32:10.418: E/distancefinder(5592): Same latitude and longtitude and return false
11-20 12:32:15.414: E/distancefinder(5592): compare lat and long
11-20 12:32:15.414: E/distancefinder(5592): Same latitude and longtitude and return false
11-20 12:32:20.392: E/distancefinder(5592): compare lat and long
11-20 12:32:20.392: E/distancefinder(5592): Past   lat: 12.9876777
11-20 12:32:20.392: E/distancefinder(5592): Past   long: 77.5950228
11-20 12:32:20.392: E/distancefinder(5592): current lat : 12.9876642
11-20 12:32:20.392: E/distancefinder(5592): current long : 77.5950319
11-20 12:32:20.393: E/distancefinder(5592): Distance in km :0.0017959869680722845
11-20 12:32:20.403: E/distancefinder(5592):  Distance in mts : 1.7959869680722844
11-20 12:32:25.410: E/distancefinder(5592): compare lat and long
11-20 12:32:25.410: E/distancefinder(5592): Same latitude and longtitude and return false
11-20 12:32:30.409: E/distancefinder(5592): compare lat and long
11-20 12:32:30.410: E/distancefinder(5592): Same latitude and longtitude and return false
11-20 12:32:35.399: E/distancefinder(5592): compare lat and long
11-20 12:32:35.399: E/distancefinder(5592): Same latitude and longtitude and return false
11-20 12:32:40.403: E/distancefinder(5592): compare lat and long
11-20 12:32:40.403: E/distancefinder(5592): Same latitude and longtitude and return false
11-20 12:32:45.412: E/distancefinder(5592): compare lat and long
11-20 12:32:45.412: E/distancefinder(5592): Past   lat: 12.9876642
11-20 12:32:45.412: E/distancefinder(5592): Past   long: 77.5950319
11-20 12:32:45.412: E/distancefinder(5592): current lat : 12.9876828
11-20 12:32:45.412: E/distancefinder(5592): current long : 77.5950043
11-20 12:32:45.413: E/distancefinder(5592): Distance in km :0.0036359967167283705
11-20 12:32:45.425: E/distancefinder(5592):  Distance in mts : 3.6359967167283704
11-20 12:32:50.435: E/distancefinder(5592): compare lat and long
11-20 12:32:50.435: E/distancefinder(5592): Same latitude and longtitude and return false
11-20 12:32:55.420: E/distancefinder(5592): compare lat and long
11-20 12:32:55.420: E/distancefinder(5592): Same latitude and longtitude and return false
11-20 12:33:00.405: E/distancefinder(5592): compare lat and long
11-20 12:33:00.405: E/distancefinder(5592): Same latitude and longtitude and return false
11-20 12:33:05.407: E/distancefinder(5592): compare lat and long
11-20 12:33:05.407: E/distancefinder(5592): Same latitude and longtitude and return false
11-20 12:33:10.410: E/distancefinder(5592): compare lat and long
11-20 12:33:10.411: E/distancefinder(5592): Past   lat: 12.9876828
11-20 12:33:10.411: E/distancefinder(5592): Past   long: 77.5950043
11-20 12:33:10.411: E/distancefinder(5592): current lat : 12.9876684
11-20 12:33:10.411: E/distancefinder(5592): current long : 77.5950334
11-20 12:33:10.411: E/distancefinder(5592): Distance in km :0.0035362761324678593
11-20 12:33:10.419: E/distancefinder(5592):  Distance in mts : 3.5362761324678593
11-20 12:33:15.411: E/distancefinder(5592): compare lat and long
11-20 12:33:15.411: E/distancefinder(5592): Same latitude and longtitude and return false
11-20 12:33:20.392: E/distancefinder(5592): compare lat and long
11-20 12:33:20.393: E/distancefinder(5592): Same latitude and longtitude and return false
11-20 12:33:25.408: E/distancefinder(5592): compare lat and long
11-20 12:33:25.408: E/distancefinder(5592): Same latitude and longtitude and return false
11-20 12:33:30.427: E/distancefinder(5592): compare lat and long
11-20 12:33:30.427: E/distancefinder(5592): Same latitude and longtitude and return false
11-20 12:33:35.387: E/distancefinder(5592): compare lat and long
11-20 12:33:35.388: E/distancefinder(5592): Past   lat: 12.9876684
11-20 12:33:35.388: E/distancefinder(5592): Past   long: 77.5950334
11-20 12:33:35.388: E/distancefinder(5592): current lat : 12.9876785
11-20 12:33:35.388: E/distancefinder(5592): current long : 77.5950269
11-20 12:33:35.388: E/distancefinder(5592): Distance in km :0.0013256282494112776
11-20 12:33:35.397: E/distancefinder(5592):  Distance in mts : 1.3256282494112777
11-20 12:33:40.408: E/distancefinder(5592): compare lat and long
11-20 12:33:40.408: E/distancefinder(5592): Same latitude and longtitude and return false
11-20 12:33:45.410: E/distancefinder(5592): compare lat and long
11-20 12:33:45.410: E/distancefinder(5592): Same latitude and longtitude and return false
11-20 12:33:50.413: E/distancefinder(5592): compare lat and long
11-20 12:33:50.413: E/distancefinder(5592): Same latitude and longtitude and return false
11-20 12:33:55.416: E/distancefinder(5592): compare lat and long
11-20 12:33:55.416: E/distancefinder(5592): Same latitude and longtitude and return false
11-20 12:34:00.412: E/distancefinder(5592): compare lat and long
11-20 12:34:00.412: E/distancefinder(5592): Past   lat: 12.9876785
11-20 12:34:00.412: E/distancefinder(5592): Past   long: 77.5950269
11-20 12:34:00.414: E/distancefinder(5592): current lat : 12.9876793
11-20 12:34:00.414: E/distancefinder(5592): current long : 77.595028
11-20 12:34:00.414: E/distancefinder(5592): Distance in km :1.4872230719746918E-4
11-20 12:34:00.425: E/distancefinder(5592):  Distance in mts : 0.14872230719746918
11-20 12:34:05.417: E/distancefinder(5592): compare lat and long
11-20 12:34:05.417: E/distancefinder(5592): Same latitude and longtitude and return false
11-20 12:34:10.408: E/distancefinder(5592): compare lat and long
11-20 12:34:10.410: E/distancefinder(5592): Same latitude and longtitude and return false
11-20 12:34:15.411: E/distancefinder(5592): compare lat and long
11-20 12:34:15.411: E/distancefinder(5592): Same latitude and longtitude and return false
11-20 12:34:20.408: E/distancefinder(5592): compare lat and long
11-20 12:34:20.408: E/distancefinder(5592): Same latitude and longtitude and return false
11-20 12:34:25.388: E/distancefinder(5592): compare lat and long
11-20 12:34:25.388: E/distancefinder(5592): Past   lat: 12.9876793
11-20 12:34:25.388: E/distancefinder(5592): Past   long: 77.595028
11-20 12:34:25.388: E/distancefinder(5592): current lat : 12.9876759
11-20 12:34:25.388: E/distancefinder(5592): current long : 77.5950243
11-20 12:34:25.388: E/distancefinder(5592): Distance in km :5.510439055217952E-4
11-20 12:34:25.395: E/distancefinder(5592):  Distance in mts : 0.5510439055217952
11-20 12:34:30.415: E/distancefinder(5592): compare lat and long
11-20 12:34:30.415: E/distancefinder(5592): Same latitude and longtitude and return false
11-20 12:34:35.412: E/distancefinder(5592): compare lat and long
11-20 12:34:35.412: E/distancefinder(5592): Same latitude and longtitude and return false

那么,解决这个问题的方法是什么。请帮我解决这个问题。

使用下面的方法计算距离b/w 两个lant lngs。

public static float calculateDistanceBetweenTwoPoints(Double latitude, Double longitude, Double toLat, Double toLng){
        float[] results = new float[2];
        Location.distanceBetween(latitude, longitude, toLat, toLng, results);
        float distanceInMeters = results[0];
        return distanceInMeters;
    }
package com.remo.distancefinder;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

public class GPSservice extends Service{

    private Context main_context;
    private GPSTracker gpsTracker;
    private Handler handler= new Handler();
    private Timer timer = new Timer();
    private Distance pastDistance = new Distance();
    private Distance currentDistance = new Distance();
    public static double DISTANCE;
    boolean flag = true ;
    private double totalDistance ;

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // TODO Auto-generated method stub
        Log.e("distancefinder","service started");

        main_context = getApplicationContext();
        gpsTracker = new GPSTracker(main_context);

        pastDistance.setLatitude(gpsTracker.getLocation().getLatitude());
        pastDistance.setLongitude(gpsTracker.getLocation().getLongitude());

        TimerTask timerTask = new TimerTask() {

            @Override
            public void run() {
                handler.post(new Runnable() {

                    @Override
                    public void run() {
                        currentDistance.setLatitude(gpsTracker.getLocation().getLatitude());
                        currentDistance.setLongitude(gpsTracker.getLocation().getLongitude());
                        Toast.makeText(main_context, "latitude:"+gpsTracker.getLocation().getLatitude()+
                                "Longitude:"+gpsTracker.getLocation().getLongitude(), Toast.LENGTH_SHORT).show();
                        comapre_LatitudeLongitude();
                        /*                      if(flag){
                            pastDistance.setLatitude(gpsTracker.getLocation().getLatitude());
                            pastDistance.setLongitude(gpsTracker.getLocation().getLongitude());
                            flag = false;
                        }else{
                            Log.e("distancefinder","In else statement");
                            currentDistance.setLatitude(gpsTracker.getLocation().getLatitude());
                            currentDistance.setLongitude(gpsTracker.getLocation().getLongitude());
                            flag = comapre_LatitudeLongitude();
                        }*/



                    }
                });


            }
        };

        timer.schedule(timerTask,0, 5000);

        return super.onStartCommand(intent, flags, startId);
    }


    /*  private double distance(double lat1, double lon1, double lat2, double lon2) {
                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;
        return (dist);

        Log.e("distancefinder","Past   lat: "+lat1);
        Log.e("distancefinder","Past   long: "+lon1);
        Log.e("distancefinder","current lat : "+lat2);
        Log.e("distancefinder","current long : "+lon2);

        int R = 6371; // km
        double dLat = toRadians(lat2-lat1);
        double dLon = toRadians(lon2-lon1);
        lat1 = toRadians(lat1);
        lat2 = toRadians(lat2);

        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)); 
        return R * c;

                double earthRadius = 6371000; //meters

        double dLat = Math.toRadians(lat2-lat1);
        double dLng = Math.toRadians(lon2-lon1);
        double a = Math.sin(dLat/2) * Math.sin(dLat/2) +
                Math.cos(Math.toRadians(lat1)) * 
                Math.cos(Math.toRadians(lat2)) *
                Math.sin(dLng/2) * Math.sin(dLng/2);
        double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
        float dist = (float) (earthRadius * c);

        return dist;


                double distance;
        Location locationA = new Location("");
        locationA.setLatitude(lat1);
        locationA.setLongitude(lon1);
        Location locationB = new Location("");
        locationB.setLatitude(lat2);
        locationB.setLongitude(lon2);
        distance = locationA.distanceTo(locationB);   //in meters
        return distance;
        //      distance = locationA.distanceTo(locationB)/1000;   //in km
    }


    public double toRadians(double deg) {
        return deg * (Math.PI/180);
    }

    private double deg2rad(double deg) {
        return (deg * Math.PI / 180.0);
    }

    private double rad2deg(double rad) {
        return (rad * 180.0 / Math.PI);
    }*/

    public float distFrom(float lat1, float lng1, float lat2, float lng2) {

        Log.e("distancefinder","Past   lat: "+lat1);
        Log.e("distancefinder","Past   long: "+lng1);
        Log.e("distancefinder","current lat : "+lat2);
        Log.e("distancefinder","current long : "+lng2);

        double earthRadius = 6371000; //meters
        double dLat = Math.toRadians(lat2-lat1);
        double dLng = Math.toRadians(lng2-lng1);
        double a = Math.sin(dLat/2) * Math.sin(dLat/2) +
                Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) *
                Math.sin(dLng/2) * Math.sin(dLng/2);
        double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
        float dist = (float) (earthRadius * c);

        return dist;
    }

    @Override
    public void onDestroy() {

        super.onDestroy();
        System.out.println("--------------------------------onDestroy -stop service ");
        timer.cancel();
        DISTANCE = totalDistance ;
    }

    public void comapre_LatitudeLongitude(){
        Log.e("distancefinder","compare lat and long");

        if(pastDistance.getLatitude() == currentDistance.getLatitude() && pastDistance.getLongitude() == currentDistance.getLongitude()){
            Log.e("distancefinder","Same latitude and longtitude and return false");
            //          return false;
        }else{
            final double distance = distFrom((float)pastDistance.getLatitude(),(float)pastDistance.getLongitude(),(float)currentDistance.getLatitude(),(float)currentDistance.getLongitude());
            Log.e("distancefinder","Distance in km :"+distance);
            handler.post(new Runnable() {

                @Override
                public void run() {
                    /*float kilometer=1.609344f;
                    Log.e("distancefinder","Total distance initial"+distance);
                    totalDistance = totalDistance +  distance * kilometer*/;
                    //DISTANCE = distance;
                    pastDistance.setLatitude(currentDistance.getLatitude());
                    pastDistance.setLongitude(currentDistance.getLongitude());
                    Log.e("distancefinder"," Distance in mts : "+distance);
                    Toast.makeText(main_context, "distance in mtrs:"+distance, Toast.LENGTH_SHORT).show();

                }
            });

            //          return true;
        }

    }
}

你得到 double a 的计算看起来不对,我不确定你用 toRadians() 做了什么(这是一种方法还是应该是 Math.toRadians()?) .我使用下面的方法,它没有问题(returns 以公里为单位的距离到小数点后一位,但您可以将其更改为您需要的任何内容)。

public static double getDistance(double deviceLatitude, double deviceLongitude,
    double destinationLatitude, double destinationLongitude) {
    double distanceLatitude = Math.toRadians(deviceLatitude - destinationLatitude);
    double distanceLongitude = Math.toRadians(deviceLongitude - destinationLongitude);
    double a = Math.sin(distanceLatitude / 2)
        * Math.sin(distanceLatitude / 2)
        + Math.cos(Math.toRadians(destinationLatitude))
        * Math.cos(Math.toRadians(deviceLatitude))
        * Math.sin(distanceLongitude / 2)
        * Math.sin(distanceLongitude / 2);
    double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
    return Double.valueOf(new DecimalFormat("#.#").format(6371 * c));
}

如果您使用的是 google 地图 V2 API 您可以使用下面的代码来获得准确的结果 //假设你有

 LatLng latLong1,latLong2;
 latLong1=new LatLng(12.77, 77.88);
 latLong2=new LatLng(17.77, 74.88);

 //Convert latLong to location object

 Location location1=new Location("location1");
 location1.setLongitude(latLong1.latitude);
 location1.setLongitude(latLong1.longitude);

 Location location2=new Location("location2");
 location2.setLongitude(latLong2.latitude);
 location2.setLongitude(latLong2.longitude);

 float  distanceInMeters= location1.distanceTo(location2);

希望对您有所帮助