可能的重复:
如何通过Android上的GPS跟踪距离?
我设计了一个GPS应用程序,它很好地说明了我的位置。但是现在我想包含更多功能。我将如何在那里制定半径?周围面积为5或6公里!我如何提及该区域和我的位置之间的距离?
如果您只有不同的坐标并想对其进行计算,只需查看已经可用的Android功能:http://developer.android.com/reference/android/location/location.html
您可以创建位置对象,将LAT/长坐标与设置功能放置,然后只使用
float distanceInMeters=location1.distanceTo(location2);
获得结果。
我觉得这个问题开始变成很多问题。我决定通过将其引导到您的问题标题" GPS应用程序的距离" 来解决此答案。
在我的应用程序中,而不是使用Google的API我请求用户与GPS坐标列表的距离通过以下内容:
在我的JJMath
类中:
获得距离(英里为英里):
/**
* @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();
}
我不使用地图覆盖层,但我确定有很棒的教程或答案。