Get the distance between two locations in android?

前端 未结 10 561
醉梦人生
醉梦人生 2020-11-28 07:57

i need to get distance between two location, but i need to get distance like blue line in the picture. \"picure\"

10条回答
  •  萌比男神i
    2020-11-28 08:53

    Try this:

    private double calculateDistance(double fromLong, double fromLat,
                double toLong, double toLat) {
            double d2r = Math.PI / 180;
            double dLong = (toLong - fromLong) * d2r;
            double dLat = (toLat - fromLat) * d2r;
            double a = Math.pow(Math.sin(dLat / 2.0), 2) + Math.cos(fromLat * d2r)
                    * Math.cos(toLat * d2r) * Math.pow(Math.sin(dLong / 2.0), 2);
            double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
            double d = 6367000 * c;
            return Math.round(d);
        }
    

    Hope this helps.

提交回复
热议问题