user1096373
user1096373

Reputation:

GPS Distance Calculation

I want to calculate the distance between two GPS locations, each with a latitute value and a longitude value. The calculations should be accurate for short-distance results. eg. < 300m. If I use Google Earth (see coord in my code) , the distance is ~136m. If I use the solution provided by article: http://www.movable-type.co.uk/scripts/latlong.html (the haversine formula) the result is nothing near that.

used code:

public void GpsCalc(){
    double d = getDistance(51.342299,4.371359, 51.342490,4.371997); 
    Log.e("GpsCalc", String.valueOf(d));
}

public static double getDistance(double lat1, double lng1, double lat2, double lng2){
    double R = 6371; // earth’s radius (mean radius = 6,371km)
    double dLat =  Math.toRadians(lat2-lat1);

    double dLon =  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(dLon/2) * Math.sin(dLon/2); 
    double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
    double dr1 = R * c;//in radians      

    Log.e("getDistance-dr1", String.valueOf(dr1));

    return dr1;
}

I'm sure it should be some minor change, but i can't see it.

Upvotes: 1

Views: 3017

Answers (2)

PabloR
PabloR

Reputation: 506

A bit late, but two more options

  1. Using Apple Corelocation (49.2733 meters)

    CLLocation *currentLocation = [[CLLocation alloc] initWithLatitude:_posGPSCurrent.latitude longitude:_posGPSCurrent.longitude];
    
    CLLocation *location = [[CLLocation alloc] initWithLatitude:_posGPSTarget.latitude longitude:_posGPSTarget.longitude];
    
    // in kilometers
    CLLocationDistance distance =[currentLocation distanceFromLocation:location]/1000;
    
  2. Manual calculation (49.1393 meters)

Include Math library

#include <math.h>

// distance in Kilometers (Haversine)

-(double)distanceFromGPSlat1:(double)tlat1 lon2:(double)tlon1 lat2:(double)tlat2 lon2:(double)tlon2
{
double distance = ((acos(sin(tlat1*M_PI/180)*sin(tlat2*M_PI/180)+cos(tlat1*M_PI/180)*cos(tlat2*M_PI/180)*cos((tlon1-tlon2)*M_PI/180))*180/M_PI)*60*1.1515*1.609344);

return distance;
}

Perhaps I prefer the second method (Manual calculation).

Upvotes: 1

Steve Blackwell
Steve Blackwell

Reputation: 5924

I have also had trouble with the haversine formula on that page. I know it's not precisely an answer to your question, but I had more success with the law of cosines formula, which gives the same results as Google Earth. In case it helps, it looked like this:

public double getDistance(double lat1, double lon1, double lat2, double lon2) {
    double latA = Math.toRadians(lat1);
    double lonA = Math.toRadians(lon1);
    double latB = Math.toRadians(lat2);
    double lonB = Math.toRadians(lon2);
    double cosAng = (Math.cos(latA) * Math.cos(latB) * Math.cos(lonB-lonA)) +
                    (Math.sin(latA) * Math.sin(latB));
    double ang = Math.acos(cosAng);
    double dist = ang * EARTH_RADIUS;
    return dist;
}

EDIT:

I tried your coordinates in Google Maps and Google Earth and in my code, and I'm getting 49m for all of them. Maybe there was never a problem? screenshot

Upvotes: 1

Related Questions