Get distance between users using tomtom location api android

548 Views Asked by At

I am developing an android app in which user can know distance between him and other users. I am using tomtom location api for this. I just want to know how to get the distance between current user and other user while i have latitude and longitude of both of them ?

Thanks in advance

2

There are 2 best solutions below

0
karan On

If you already have coordinates of both users then you can calculate using below function. Answer is from SO thread here.

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);
}

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

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

I assume that you just need to calculate the great-circle distance between two points. There is math explenation hidden under this: http://www.movable-type.co.uk/scripts/latlong.html

Check TomTom class: com.tomtom.online.sdk.common.util.DistanceCalculator and methods:

  • DistanceCalculator#greatCircleDistance(LatLng latLng, LatLng refLatLng)
  • DistanceCalculator#calcDistInKilometers(LatLng destination, LatLng origin)

If you need to calculate precisely route distance, use routing sdk: https://developer.tomtom.com/maps-android-sdk/routing