【问题标题】:Distance for GPS Application [duplicate]GPS应用距离[重复]
【发布时间】:2012-11-01 16:07:17
【问题描述】:

可能重复:
How to track distance via GPS on Android?

我设计了一个 GPS 应用程序,它可以很好地告诉我我的位置。但现在我想包含更多功能。我将如何在那里做一个半径?有5或6公里的周边区域!我怎么能提到那个区域的某个地方和我的地方之间的距离?

【问题讨论】:

  • 在 android 中有很多方法可以绘制地图。每种方法都有不同的方法来获取两点之间的距离。您需要提供有关如何创建地图的更多信息地图。
  • 还指定您是尝试计算用户与半径内的所有点之间的距离,还是仅从用户到单个特定位置的距离。您如何存储所有这些坐标,最后您尝试了什么?
  • 我正在使用 google api 进行位置跟踪。我想计算用户与特定地点之间的距离。我不确定存储数据。我怎样才能做到这一点,我可以在半径中存储少量数据(纬度,经度)并找到从我的地方到那些地方的距离。我在这里分享我的主要编码部分:
  • 我认为编码部分对于这个地方来说太重了。我在地图中使用了叠加层,并使用了 google apis 进行映射。我怎么能有距离?如果您需要了解任何其他信息,请提及。

标签: java android gps


【解决方案1】:

如果您只是有不同的坐标并想用它们进行计算,只需查看已有的 Android 功能即可: http://developer.android.com/reference/android/location/Location.html

您可以创建 Location 对象,将纬度/经度坐标与设置函数一起使用,然后只需使用

float distanceInMeters=location1.distanceTo(location2);

得到结果。

【讨论】:

    【解决方案2】:

    我觉得这个问题开始变成很多问题。我决定通过将其指向您的问题标题“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 很高兴我能提供帮助,让我知道有什么令人困惑的地方,我可以尝试提供帮助。
    猜你喜欢
    • 1970-01-01
    • 2012-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-07
    • 1970-01-01
    相关资源
    最近更新 更多