iOS Question How to calculate distance in meters between locations

moster67

Expert
Licensed User
Longtime User
How can I calculate distance in meters between locations (I have lat/long for each location)?

I searched the forum but could not find anything specific. I found B4a examples but most use the DistanceTo method from the GPS-library of B4a which unfortunately does not seem to be available for B4i.

I can of course write my own function but maybe I missed a post specific for B4i...
 

moster67

Expert
Licensed User
Longtime User
Never mind - I found the DistanceTo method for B4i.
Will try to go from here...
 
Upvote 0

Johan Schoeman

Expert
Licensed User
Longtime User
How can I calculate distance in meters between locations (I have lat/long for each location)?

I searched the forum but could not find anything specific. I found B4a examples but most use the DistanceTo method from the GPS-library of B4a which unfortunately does not seem to be available for B4i.

I can of course write my own function but maybe I missed a post specific for B4i...

B4X:
/**
*Calculate the distance (in kilometers) between two geographical points
*by using the Haversine Formula (pass values as decimal degrees).
*East Longitude is +, West Longitude is -
*North Latitude is +, South Latitude is -
*Example:<code>
*Dim myDistance As Double
*myDistance = mymath.hsGreatCircleKm(40.35, 74.65, 48.87, -2.33)</code>
*/
public double hsGreatCircleKm(double lat1, double long1, double lat2, double long2) {
    double R = 6372.8; // In kilometers
    double dLat = Math.toRadians(lat2 - lat1);
    double dLon = Math.toRadians(long2 - long1);
    lat1 = Math.toRadians(lat1);
    lat2 = Math.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.asin(Math.sqrt(a));
    return R * c;
}


Should be easy to converts to B4i
 
Upvote 0

Johan Schoeman

Expert
Licensed User
Longtime User
How can I calculate distance in meters between locations (I have lat/long for each location)?

I searched the forum but could not find anything specific. I found B4a examples but most use the DistanceTo method from the GPS-library of B4a which unfortunately does not seem to be available for B4i.

I can of course write my own function but maybe I missed a post specific for B4i...

In nautical miles using the Law of Cosines and the Haversine Formula...
B4X:
/**
*Calculate the distance (in nautical miles) between two geographical points
*by using the law of cosines (pass values as decimal degrees).
*East Longitude is +, West Longitude is -
*North Latitude is +, South Latitude is -
*Example:<code>
*Dim myDistance As Double
*myDistance = mymath.locGreatCircleNm(59.9, -30.3, 37.8, 122.4)</code>
*/
public double locGreatCircleNm(double lat1, double long1, double lat2, double long2) {

        double x1 = Math.toRadians(lat1);
        double y1 = Math.toRadians(long1);
        double x2 = Math.toRadians(lat2);
        double y2 = Math.toRadians(long2);

       /*************************************************************************
        * Compute using law of cosines
        *************************************************************************/
        // great circle distance in radians
        double angle1 = Math.acos(Math.sin(x1) * Math.sin(x2)
                      + Math.cos(x1) * Math.cos(x2) * Math.cos(y1 - y2));

        // convert back to degrees
        angle1 = Math.toDegrees(angle1);

        // each degree on a great circle of Earth is 60 nautical miles
        double distance1 = 60 * angle1;

        return distance1;
}

/**
*Calculate the distance (in nautical miles) between two geographical points
*by using the Haversine Formula (pass values as decimal degrees).
*East Longitude is +, West Longitude is -
*North Latitude is +, South Latitude is -
*Example:<code>
*Dim myDistance As Double
*myDistance = mymath.hsGreatCircleNm(40.35, 74.65, 48.87, -2.33)</code>
*/
public double hsGreatCircleNm(double lat1, double long1, double lat2, double long2) {

        double x1 = Math.toRadians(lat1);
        double y1 = Math.toRadians(long1);
        double x2 = Math.toRadians(lat2);
        double y2 = Math.toRadians(long2);


       /*************************************************************************
        * Compute using Haversine formula
        *************************************************************************/
        double a = Math.pow(Math.sin((x2-x1)/2), 2)
                 + Math.cos(x1) * Math.cos(x2) * Math.pow(Math.sin((y2-y1)/2), 2);

        // great circle distance in radians
        double angle2 = 2 * Math.asin(Math.min(1, Math.sqrt(a)));

        // convert back to degrees
        angle2 = Math.toDegrees(angle2);

        // each degree on a great circle of Earth is 60 nautical miles
        double distance2 = 60 * angle2;

       return distance2;
}
 
Upvote 0

moster67

Expert
Licensed User
Longtime User
Thanks. I resolved my problem since I found the DistanceTo for B4i - however, I may convert them anyway just to compare the results.
 
Upvote 0
Top