B4J Question Calculate distance between two latitude-longitude points?

suciwulans

Active Member
Licensed User
Longtime User
How do I calculate the distance between two points specified by latitude and longitude?
For clarification, I'd like the distance in kilometers; the points use the WGS84 system and I'd like to understand the relative accuracies of the approaches available.
 

Philip Prins

Active Member
Licensed User
Longtime User
Hello Erel,

Can you give an example to calculate distance between to Lat Lng points in B4J , i use 51.4000 and 4.32432 As Lat and Lon

Tnx
Philip
 
Upvote 0

Jmu5667

Well-Known Member
Licensed User
Longtime User
You can covert the java code to b4j, it just the formula you need.

B4X:
/*::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
/*:: :*/
/*:: This routine calculates the distance between two points (given the :*/
/*:: latitude/longitude of those points). It is being used to calculate :*/
/*:: the distance between two locations using GeoDataSource (TM) prodducts :*/
/*:: :*/
/*:: Definitions: :*/
/*:: South latitudes are negative, east longitudes are positive :*/
/*:: :*/
/*:: Passed to function: :*/
/*:: lat1, lon1 = Latitude and Longitude of point 1 (in decimal degrees) :*/
/*:: lat2, lon2 = Latitude and Longitude of point 2 (in decimal degrees) :*/
/*:: unit = the unit you desire for results :*/
/*:: where: 'M' is statute miles (default) :*/
/*:: 'K' is kilometers :*/
/*:: 'N' is nautical miles :*/
/*:: Worldwide cities and other features databases with latitude longitude :*/
/*:: are available at http://www.geodatasource.com :*/
/*:: :*/
/*:: For enquiries, please contact [email protected] :*/
/*:: :*/
/*:: Official Web site: http://www.geodatasource.com :*/
/*:: :*/
/*:: GeoDataSource.com (C) All Rights Reserved 2015 :*/
/*:: :*/
/*::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/

import java.util.*;
import java.lang.*;
import java.io.*;

class DistanceCalculator
{
public static void main (String[] args) throws java.lang.Exception
{
System.out.println(distance(32.9697, -96.80322, 29.46786, -98.53506, "M") + " Miles\n");
System.out.println(distance(32.9697, -96.80322, 29.46786, -98.53506, "K") + " Kilometers\n");
System.out.println(distance(32.9697, -96.80322, 29.46786, -98.53506, "N") + " Nautical Miles\n");
}

private static double distance(double lat1, double lon1, double lat2, double lon2, String 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 static double deg2rad(double deg) {
return (deg * Math.PI / 180.0);
}

/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
/*:: This function converts radians to decimal degrees :*/
/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
private static double rad2deg(double rad) {
return (rad * 180 / Math.PI);
}
}
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
In B4x:
B4X:
Dim Lat1 As Double, Long1 As Double, Lat2 As Double, Long2 As Double
Dim EarthRadius, Angle, Distance As Double
EarthRadius = 6378.137 ' WGS -84 kilometers?

lat1 = txtlatdeg.Text
lat2 = txtlatdeg2.Text
long1 = txtlondeg.Text
long2 = txtlondeg2.Text

Angle = ACos(SinD(Lat1) * SinD(Lat2) + CosD(Lat1) * CosD(Lat2) * CosD(Long2 - Long1))
Distance = EarthRadius * Angle
txtdistance = Distance
It's not as accurate as the DistanceTo method in the GPS libraries. But, depending on your needs, it may be OK.
You may have look HERE, I copied the equations from there.
 
Upvote 0
Top