GPS应用的距离

3

可能是重复问题:
如何在Android上通过GPS跟踪距离?

我设计了一个GPS应用程序,它能够很好地告诉我的位置。但现在我想包括更多的功能。我该如何设置半径呢?以便有一个5或6公里的周围区域!我如何说明那个区域内的某个地方和我的位置之间的距离呢?


2
在Android中绘制地图有许多方法,每种方法都有不同的获取两点之间距离的方式。您需要提供更多关于如何创建地图的信息。 - JustinMorris
你是否正在尝试计算用户与半径内所有点之间的距离,还是只是从用户到单个特定位置的距离?你是如何存储所有这些坐标的,最后你尝试了什么? - jnthnjns
我正在使用Google API进行位置跟踪。我想计算用户与特定地点之间的距离。我不确定如何存储数据。我该如何实现在半径内存储少量数据(纬度、经度),并找到从我的位置到这些位置的距离。我在此分享我的主要编码部分: - Saad
我认为编码部分对这个地方来说太重了。我在我的地图中使用了叠加层,并使用了谷歌API进行映射。我如何获得距离?如果您需要了解其他信息,请提出。 - Saad
2个回答

2
如果您只是拥有不同的坐标并希望对其进行计算,可以直接查看Android已经提供的相关函数:http://developer.android.com/reference/android/location/Location.html
您可以创建位置对象,使用set函数设置纬度/经度坐标,然后直接使用。
float distanceInMeters=location1.distanceTo(location2);

获取结果。


0

我觉得这个问题开始变成了很多问题。我决定通过将答案指向你的问题标题"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();
}

我不使用地图叠加,但我相信会有很棒的教程或答案出现。


谢谢你,Asok。我需要处理这个问题。还有一些事情仍然让我感到困惑,但我正在努力。再次感谢。 - Saad
@Saad 很高兴我能帮到你,如果有什么不清楚的地方,请告诉我,我可以尝试帮助你。 - jnthnjns

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接