可能是重复问题:
如何在Android上通过GPS跟踪距离?
我设计了一个GPS应用程序,它能够很好地告诉我的位置。但现在我想包括更多的功能。我该如何设置半径呢?以便有一个5或6公里的周围区域!我如何说明那个区域内的某个地方和我的位置之间的距离呢?
可能是重复问题:
如何在Android上通过GPS跟踪距离?
我设计了一个GPS应用程序,它能够很好地告诉我的位置。但现在我想包括更多的功能。我该如何设置半径呢?以便有一个5或6公里的周围区域!我如何说明那个区域内的某个地方和我的位置之间的距离呢?
float distanceInMeters=location1.distanceTo(location2);
获取结果。
我觉得这个问题开始变成了很多问题。我决定通过将答案指向你的问题标题"GPS应用程序的距离"来回答这个问题。
在我的应用程序中,我不使用Google的API,而是通过以下方式请求用户与一系列GPS坐标的距离:
在我的JJMath
类中:
获取距离(Haversine公式,以英里为单位):
/**
* @param lat1
* Latitude which was given by the device's internal GPS or Network location provider of the users location
* @param lng1
* Longitude which was given by the device's internal GPS or Network location provider of the users location
* @param lat2
* Latitude of the object in which the user wants to know the distance they are from
* @param lng2
* Longitude of the object in which the user wants to know the distance they are from
* @return
* Distance from which the user is located from the specified target
*/
public static double distFrom(double lat1, double lng1, double lat2, double lng2) {
double earthRadius = 3958.75;
double dLat = Math.toRadians(lat2-lat1);
double dLng = Math.toRadians(lng2-lng1);
double sindLat = Math.sin(dLat / 2);
double sindLng = Math.sin(dLng / 2);
double a = Math.pow(sindLat, 2) + Math.pow(sindLng, 2) * Math.cos(lat1) * Math.cos(lat2);
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
double dist = earthRadius * c;
return dist;
}
然后我通过以下方式将该数字四舍五入:
/** This gives me numeric value to the tenth (i.e. 6.1) */
public static double round(double unrounded) {
BigDecimal bd = new BigDecimal(unrounded);
BigDecimal rounded = bd.setScale(1, BigDecimal.ROUND_CEILING);
return rounded.doubleValue();
}
我不使用地图叠加,但我相信会有很棒的教程或答案出现。