select Best Location - Android Map

Android examples for Map:Location

Description

select Best Location

Demo Code


import java.util.ArrayList;
import java.util.List;
import android.location.Location;
import android.location.LocationManager;
import android.util.Log;

public class Main{
    /**//from   w w  w.j  a va  2s.c o m
     * According to Wolfram Alpha mean walking speed is 1.1m/s and so every second of age is 1.1
     * meters of additional inaccuracy
     */
    private static final double TIMEFACTOR = 1100;
    public static Location selectBestLocation(List<Location> locations) {
        long currentTime = System.currentTimeMillis();
        Location best = null;
        double accuracy = -1;
        for (Location location : locations) {
            if (null == best) {
                best = location;
                accuracy = location.getAccuracy()
                        * ((currentTime - location.getTime()) / LocationHelper.TIMEFACTOR);
            } else if (location.hasAccuracy()) {
                double newAccuracy = location.getAccuracy()
                        * ((currentTime - location.getTime()) / LocationHelper.TIMEFACTOR);
                if (newAccuracy > 0 && newAccuracy < accuracy) {
                    best = location;
                    accuracy = newAccuracy;
                }
            }
        }
        return best;
    }
}

Related Tutorials