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
答案 0 :(得分:2)
我假设您只需要计算两点之间的大圆距离即可。 以下隐藏了数学表达式:http://www.movable-type.co.uk/scripts/latlong.html
检查TomTom类:com.tomtom.online.sdk.common.util.DistanceCalculator
和方法:
DistanceCalculator#greatCircleDistance(LatLng latLng, LatLng refLatLng)
DistanceCalculator#calcDistInKilometers(LatLng destination, LatLng origin)
如果您需要精确计算路线距离,请使用路线sdk:https://developer.tomtom.com/maps-android-sdk/routing
答案 1 :(得分:0)
如果您已经拥有两个用户的坐标,则可以使用以下功能进行计算。答案来自SO线程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);
}