【问题标题】:Marking GPS coordinates by using accuracy in Android在 Android 中使用精度标记 GPS 坐标
【发布时间】:2013-12-29 19:06:56
【问题描述】:

我的问题是关于在一定半径内有效地存储以前的位置(由准确性决定)。

getLatitude()getLongitude() 返回坐标的 double,而 getAccuracy() 返回米的浮点数。

我不知道是否可以找到米到坐标的转换,但我的问题实际上是开发一种可以提供快速查找和操作的数据结构。

我想到的第一件事是使用像 trie 这样的东西,其中每个数字也保持准确性,但这会占用大量内存。我会将这些信息存储在用户的设备中。所以这不是一个好方法。

我想了很多,唯一能想到的就是我可以创建动态的小坐标平面。例如,用户在纬度 100.242312、经度 50.53412 时激活应用程序,每次移动后,更改的位数决定了坐标平面的大小。无论如何,即使是这个听起来也过于复杂且难以实施。

我的问题是,有没有针对这个问题提供的 API?如果没有任何简单的方法来解决这个问题?

【问题讨论】:

  • Location 对象有一种方法可以直接比较距离(以米为单位)。不过,我不确定您到底要达到什么目标,但很容易确定,例如任何位置是否距离初始起点 x 米
  • 你的意思是两个Location对象是否重叠?

标签: java android gps location coordinates


【解决方案1】:

我并没有完全遵循您想要做的事情。

如果您只想在移动一定距离后存储位置,那么您可以简单地存储最后一个位置进行比较。然后,稍后,当当前位置与存储位置之间的距离超过距离阈值时,您替换存储位置。

这里有一些代码展示了这个想法:

public class DistanceFixedImpl extends AbstractStrategyImpl {

private static String TAG = "DistanceFixed"; 
protected int distance;

protected Location lastLocation;


public DistanceFixedImpl(Context context) {
    super(context);
    distance = PreferencesData.getInt(context,
            DistanceBasedFixedDistance.STATE_DISTANCE, 30);
}

/**
 * This is just to enable DistanceAccelerometer, which extends this class, to change what is sent to server
 * 
 * @param TAG
 */
protected void setTAG(String TAG) {
    DistanceFixedImpl.TAG = TAG;
}

public void setLocationStrategyListener(
        LocationStrategyUpdateListener strategyUpdateListener) {
    this.strategyListener = strategyUpdateListener;
}

public void setDistance(int distance) {
    PreferencesData.saveInt(context,
            DistanceBasedFixedDistance.STATE_DISTANCE, distance);
    this.distance = distance;
}

@Override
public void startStrategy() {
    startUsingGPS();
    strategyListener.strategyMessageCallback("Awaiting GPS first-fix"); 
}

@Override
public void stopStrategy() {
    stopUsingGPS();
    super.stopStrategy();
}

@Override
public void onFirsFixChanged(boolean hasGPSfix) {
    strategyListener.strategyMessageCallback("Received a first-fix change to: " + hasGPSfix);
}

private Integer[] outputPercentages = new Integer[]{25,50,75,100};
private int outputPercentIndex;

@Override
public void onLocationChanged(Location location) {
    incrementLocationChangesOnDevice();
    // ignore any incomming locations as long as first fix is not set
    if (!hasFirstFix())
        return;

    if (lastLocation == null) {

        /**---------------------------------------------------------------------------------------------- *
         *  No location to compare against yet, so we just save it as two is needed to calculate distance * 
         * -----------------------------------------------------------------------------------------------*/

        lastLocation = location;
        sendLocationToServer(location, TAG);
    } else {
        float distanceMoved = lastLocation.distanceTo(location);

        int outputDistancePercent = outputPercentages[outputPercentIndex];
        if ((distanceMoved/distance)*100 >= outputDistancePercent) { 
            outputPercentIndex = (outputPercentIndex+1) % 4;
            strategyListener.strategyMessageCallback("Moved: " + outputDistancePercent + "% of " + distance + " meters");
        }

        if (distanceMoved > distance) {

            // threshold exceeded - send location to server

            strategyListener.strategyMessageCallback("SEND: " + distanceMoved
                    + " meters");

            sendLocationToServer(location, TAG);

            lastLocation = location;

        } 

    }
}

}

以上代码是我为 Pervasive Positioning 课程制作的项目的一部分。

如果这是您想要实现的,那么我可以提供项目源代码的链接。

【讨论】:

  • 不是我想要的。我只是在制作一个实用工具,它将使用坐标来启用/禁用服务。我的意图是,例如,用户将选择他们的位置作为家,并在用户在家时将其作为家。一旦用户离开家,用户将取消选择家,所以下次通过使用我之前收集的位置,我希望能够确定用户是否在家。
  • 好吧,理论上你也可以使用这个想法。您无需将当前位置与 lastLocation 进行比较,而是将其与所有保存的位置进行比较。这样您就可以知道到例如家的距离以及是否选择或取消选择它。
  • 我无法真正遵循您的代码,您到底在哪里进行比较?
  • 在 onLocationChanged at distanceMoved > 距离,距离是一个预定义的阈值
  • 所以基本上,我正在寻找的是 lastLocation.distanceTo(location) 的列表?
猜你喜欢
  • 1970-01-01
  • 2020-11-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-02
  • 2012-12-21
相关资源
最近更新 更多