How can I calculate distance between multiple lati

2020-05-10 09:17发布

I want to calculate total distance between each points both contains lat and long, these points are stored in local database, so the scenario is I want to calculate distance from point a to b & b to c & c to d and these points(lat and long) are stored into database using google place api enter image description here

All the points are fetched from database.

4条回答
相关推荐>>
2楼-- · 2020-05-10 09:22

I'm not sure if I understand the scope of this question. Do you need to take into account the curvature of earth? Or it is just 2 random points?

If it's the first : I can show you this link as i cannot help you out:

https://www.movable-type.co.uk/scripts/latlong.html

If it is the latter:

Suppose 1 is latitude and 2 is longitude, so that a1 is latitude of point A.

Calculating distance from 2 points

Calculating 2 points in a plane is something like somethign like this:

Distance = sqrt((a1−b1)+(a2−b2))

So, to calculate for example distance from D to E is: sqrt((e1-d1) + (e2-d2))

查看更多
我想做一个坏孩纸
3楼-- · 2020-05-10 09:22
float[] distanceWidth = new float[2];
Location.distanceBetween(
                startLatitude,
                startLongitude,
                endLatitude,
                endLongitude,
                distanceWidth);

//You will get float[] of results after Location.distanceBetween().

float distance = distanceHeight[0];
float distanceInKm = distance/1000;
查看更多
4楼-- · 2020-05-10 09:25

you can calculate distance between 2 location points without using places api

    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);
}
查看更多
来,给爷笑一个
5楼-- · 2020-05-10 09:45
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) {
        if ((lat1 == lat2) && (lon1 == lon2)) {
            return 0;
        }
        else {
            double theta = lon1 - lon2;
            double dist = Math.sin(Math.toRadians(lat1)) * Math.sin(Math.toRadians(lat2)) + Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) * Math.cos(Math.toRadians(theta));
            dist = Math.acos(dist);
            dist = Math.toDegrees(dist);
            dist = dist * 60 * 1.1515;
            if (unit == "K") {
                dist = dist * 1.609344;
            } else if (unit == "N") {
                dist = dist * 0.8684;
            }
            return (dist);
        }
    }
}
查看更多
登录 后发表回答