user3371750
user3371750

Reputation:

C++ - Latitude and Longitude Distance Calculator

My program calculates the distance between two points in kilometres, given two latitude and longitude values.

#include<iostream>
#include <cmath>

#define pi 3.14159265358979323846

using namespace std;

double calculateDistance(double lat1, double long1, double lat2, double long2) {
    double dist;
    dist = sin(lat1) * sin(lat2) + cos(lat1) * cos(lat2) * cos(long1 - long2);
    dist = acos(dist);
    dist = (6371 * pi * dist) / 180;
    return dist;
}

int main() {
    cout << calculateDistance(51.752021, -1.257726, 51.507351, -0.127758);
    return 0;
}

This is the formula I am trying to implement:

enter image description here

And the calculator I am using to test my output, http://www.movable-type.co.uk/scripts/latlong.html which states the answer should be 82.60km. (I am getting 33.6227km)

Upvotes: 1

Views: 11174

Answers (3)

M.Hefny
M.Hefny

Reputation: 2745

Work & Tested

Returns in meters. latitude & longitude are in degrees.

    #define PI 3.14159265358979323846
    #define RADIO_TERRESTRE 6372797.56085
    #define GRADOS_RADIANES PI / 180
    #define RADIANES_GRADOS 180 / PI
        
    double calcGPSDistance(double latitude_new, double longitude_new, double latitude_old, double longitude_old)
{
    double  lat_new = latitude_old * GRADOS_RADIANES;
    double  lat_old = latitude_new * GRADOS_RADIANES;
    double  lat_diff = (latitude_new-latitude_old) * GRADOS_RADIANES;
    double  lng_diff = (longitude_new-longitude_old) * GRADOS_RADIANES;

    double  a = sin(lat_diff/2) * sin(lat_diff/2) +
                cos(lat_new) * cos(lat_old) *
                sin(lng_diff/2) * sin(lng_diff/2);
    double  c = 2 * atan2(sqrt(a), sqrt(1-a));

    double  distance = RADIO_TERRESTRE * c;
    
    // std::cout <<__FILE__ << "." << __FUNCTION__ << " line:" << __LINE__ << "  "
    
    return distance;

Upvotes: 0

You could try this, for me its working and more precisely. I'm using the Haversine formula for this.

#include <iostream>
#include <math.h>
#include <algorithm>

#define PI 3.14159265358979323846
#define RADIO_TERRESTRE 6372797.56085
#define GRADOS_RADIANES PI / 180
using namespace std;

float CalcGPSDistance(float latitud1, float longitud1, float latitud2, float longitud2){
    double haversine;
    double temp;
    double distancia_puntos;

    latitud1  = latitud1  * GRADOS_RADIANES;
    longitud1 = longitud1 * GRADOS_RADIANES;
    latitud2  = latitud2  * GRADOS_RADIANES;
    longitud2 = longitud2 * GRADOS_RADIANES;

    haversine = (pow(sin((1.0 / 2) * (latitud2 - latitud1)), 2)) + ((cos(latitud1)) * (cos(latitud2)) * (pow(sin((1.0 / 2) * (longitud2 - longitud1)), 2)));
    temp = 2 * asin(min(1.0, sqrt(haversine)));
    distancia_puntos = RADIO_TERRESTRE * temp;

   return distancia_puntos;
}

int main(){

    cout << CalcGPSDistance(37.824574,-2.533276,37.821897,-2.537297)<< endl;

    return 0;
}

Upvotes: 2

G&#225;bor Bakos
G&#225;bor Bakos

Reputation: 9100

Something like this should work (not tested though):

double toRad(double degree) {
    return degree/180 * pi;
}

double calculateDistance(double lat1, double long1, double lat2, double long2) {
    double dist;
    dist = sin(toRad(lat1)) * sin(toRad(lat2)) + cos(toRad(lat1)) * cos(toRad(lat2)) * cos(toRad(long1 - long2));
    dist = acos(dist);
//        dist = (6371 * pi * dist) / 180;
    //got dist in radian, no need to change back to degree and convert to rad again.
    dist = 6371 * dist;
    return dist;
}

Upvotes: 2

Related Questions