Reputation: 3784
My problem is about efficiently storing previous locations within a certain radius(determined by accuracy).
getLatitude()
and getLongitude()
returns a double
of coordinates whereas getAccuracy()
returns a float of meters.
I don't know if there is a conversion that I can find for meters to coordinates but my problem is actually to develop a data structure that can provide fast lookup and manipulation.
First thing that came to my mind is to use something like trie where each number also holds accuracy, but that uses a lot of memory. I will be storing these information in user's device. So it wouldn't be a good way to do.
I thought a bit more about it and only thing that I can think of is I can create dynamic small coordinate plane. E.g user activates the app while at latitude 100.242312, longitude 50.53412 and after each movement, the amount of digits changed determines the size of coordinate plane. Anyway, even this one sounds like overly complex and hard to implement.
My question is, are there any APIs provided for this problem? If not any easy way approach for this problem?
Upvotes: 0
Views: 711
Reputation: 7108
I am not quite following what it is that you want to do.
If you only want to store locations after moving a certain distance, then you can simply store the last location for comparison. Then, later, when the distance between the current position and the stored location exceed the distance threshold, you replace the stored location.
Here is some code showing the idea:
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;
}
}
}
}
The above code is part of a project I made for a Pervasive Positioning course.
If this is what you want to implement, then I can provide a link for the project source code.
Upvotes: 1