Example usage for android.location Criteria setAccuracy

List of usage examples for android.location Criteria setAccuracy

Introduction

In this page you can find the example usage for android.location Criteria setAccuracy.

Prototype

public void setAccuracy(int accuracy) 

Source Link

Document

Indicates the desired accuracy for latitude and longitude.

Usage

From source file:Main.java

public static String getBestProvider(LocationManager locationManager) throws IllegalArgumentException {
    Criteria criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_FINE);
    criteria.setPowerRequirement(Criteria.NO_REQUIREMENT);

    return locationManager.getBestProvider(criteria, true);
}

From source file:Main.java

/**
 * this criteria will settle for less accuracy, high power, and cost
 */// ww  w . j  a v a  2s  .c o  m
public static Criteria createCoarseCriteria() {

    Criteria c = new Criteria();
    c.setAccuracy(Criteria.ACCURACY_COARSE);
    c.setAltitudeRequired(false);
    c.setBearingRequired(false);
    c.setSpeedRequired(false);
    c.setCostAllowed(true);
    c.setPowerRequirement(Criteria.POWER_HIGH);
    return c;

}

From source file:Main.java

public static String getBestProvider(LocationManager locationmanager) {
    String s = null;//from   www .  j  av a  2 s.  c o  m
    if (locationmanager != null) {
        Criteria criteria = new Criteria();
        criteria.setAccuracy(2);
        criteria.setAltitudeRequired(false);
        criteria.setBearingRequired(false);
        criteria.setCostAllowed(true);
        criteria.setPowerRequirement(0);
        s = locationmanager.getBestProvider(criteria, true);
        if (s == null)
            s = "network";
    }
    return s;
}

From source file:Main.java

public static String getGpsString(Context context) {
    if (context == null) {
        return null;
    }/*from www  .  ja va 2  s. c  o  m*/

    String gps = null;

    Location location = null;
    Criteria criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_FINE);
    criteria.setAltitudeRequired(false);
    criteria.setBearingRequired(false);
    criteria.setCostAllowed(true);

    LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
    String provider = locationManager.getBestProvider(criteria, true);
    if (provider != null) {
        location = locationManager.getLastKnownLocation(provider);
    }
    if (location != null) {
        double longtitude = location.getLongitude();
        double latitude = location.getLatitude();
        gps = "Lon:" + longtitude + "; Lat:" + latitude;
    }

    if (gps == null) {
        gps = "Lon:0; Lat:0";
    }
    return gps;
}

From source file:com.appdynamics.demo.gasp.utils.LocationServices.java

/**
 * Set Location Services and get current location
 *///from   ww w. j  a v  a 2s  .c  o  m
public static Location getLocation(Context context) {
    Location location = null;
    try {
        String svcName = Context.LOCATION_SERVICE;
        LocationManager locationManager = (LocationManager) context.getSystemService(svcName);

        Criteria criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_FINE);
        criteria.setPowerRequirement(Criteria.POWER_LOW);
        criteria.setAltitudeRequired(false);
        criteria.setBearingRequired(false);
        criteria.setSpeedRequired(false);
        criteria.setCostAllowed(true);
        String provider = locationManager.getBestProvider(criteria, true);
        location = locationManager.getLastKnownLocation(provider);

        Log.i(TAG, "Current Latitude = " + location.getLatitude());
        Log.i(TAG, "Current Longitude = " + location.getLongitude());
    } catch (Exception e) {
        e.printStackTrace();
    }
    return location;
}

From source file:com.airbop.library.simple.CommonUtilities.java

/**
 * Simple helper that gets the location criteria that we want. 
 * @return//  www .j a va  2 s .  c  o  m
 */
public static Criteria getCriteria() {
    if (true) {
        Criteria criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_COARSE);
        criteria.setPowerRequirement(Criteria.POWER_LOW);
        criteria.setAltitudeRequired(false);
        criteria.setBearingRequired(false);
        criteria.setSpeedRequired(false);
        criteria.setCostAllowed(true);

        return criteria;
    }
    return null;
}

From source file:com.ushahidi.android.app.util.Util.java

/** this criteria needs high accuracy, high power, and cost */
public static Criteria createFineCriteria() {

    Criteria c = new Criteria();
    c.setAccuracy(Criteria.ACCURACY_FINE);
    c.setAltitudeRequired(false);/*w  ww . jav a  2 s .co m*/
    c.setBearingRequired(false);
    c.setSpeedRequired(false);
    c.setCostAllowed(true);
    c.setPowerRequirement(Criteria.POWER_HIGH);
    return c;

}

From source file:com.mobilevangelist.glass.helloworld.GetTheWeatherActivity.java

public static Location getLastLocation(Context context) {
    Location result = null;/*  www . ja v a  2s .c om*/
    LocationManager locationManager;
    Criteria locationCriteria;
    List<String> providers;

    locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
    locationCriteria = new Criteria();
    locationCriteria.setAccuracy(Criteria.NO_REQUIREMENT);
    providers = locationManager.getProviders(locationCriteria, true);

    // Note that providers = locatoinManager.getAllProviders(); is not used because the
    // list might contain disabled providers or providers that are not allowed to be called.

    //Note that getAccuracy can return 0, indicating that there is no known accuracy.

    for (String provider : providers) {
        Location location = locationManager.getLastKnownLocation(provider);
        if (result == null) {
            result = location;
        } else if (result.getAccuracy() == 0.0) {
            if (location.getAccuracy() != 0.0) {
                result = location;
                break;
            } else {
                if (result.getAccuracy() > location.getAccuracy()) {
                    result = location;
                }
            }
        }
    }

    return result;
}

From source file:de.madvertise.android.sdk.MadUtil.java

/**
 * Try to update current location. Non blocking call.
 * //from w  w  w . ja v a2s .  com
 * @param context
 *            application context
 */
protected static void refreshCoordinates(Context context) {
    if (PRINT_LOG)
        Log.d(LOG, "Trying to refresh location");

    if (context == null) {
        if (PRINT_LOG)
            Log.d(LOG, "Context not set - quit location refresh");
        return;
    }

    // check if we need a regular update
    if ((locationUpdateTimestamp + MadUtil.SECONDS_TO_REFRESH_LOCATION * 1000) > System.currentTimeMillis()) {
        if (PRINT_LOG)
            Log.d(LOG, "It's not time yet for refreshing the location");
        return;
    }

    synchronized (context) {
        // recheck, if location was updated by another thread while we paused
        if ((locationUpdateTimestamp + MadUtil.SECONDS_TO_REFRESH_LOCATION * 1000) > System
                .currentTimeMillis()) {
            if (PRINT_LOG)
                Log.d(LOG, "Another thread updated the loation already");
            return;
        }

        boolean permissionCoarseLocation = context.checkCallingOrSelfPermission(
                android.Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED;
        boolean permissionFineLocation = context.checkCallingOrSelfPermission(
                android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED;

        // return (null) if we do not have any permissions
        if (!permissionCoarseLocation && !permissionFineLocation) {
            if (PRINT_LOG)
                Log.d(LOG, "No permissions for requesting the location");
            return;
        }

        // return (null) if we can't get a location manager
        LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        if (locationManager == null) {
            if (PRINT_LOG)
                Log.d(LOG, "Unable to fetch a location manger");
            return;
        }

        String provider = null;
        Criteria criteria = new Criteria();
        criteria.setCostAllowed(false);

        // try to get coarse location first
        if (permissionCoarseLocation) {
            criteria.setAccuracy(Criteria.ACCURACY_COARSE);
            provider = locationManager.getBestProvider(criteria, true);
        }

        // try to get gps location if coarse locatio did not work
        if (provider == null && permissionFineLocation) {
            criteria.setAccuracy(Criteria.ACCURACY_FINE);
            provider = locationManager.getBestProvider(criteria, true);
        }

        // still no provider, return (null)
        if (provider == null) {
            if (PRINT_LOG)
                Log.d(LOG, "Unable to fetch a location provider");
            return;
        }

        // create a finalized reference to the location manager, in order to
        // access it in the inner class
        final LocationManager finalizedLocationManager = locationManager;
        locationUpdateTimestamp = System.currentTimeMillis();
        locationManager.requestLocationUpdates(provider, 0, 0, new LocationListener() {
            public void onLocationChanged(Location location) {
                if (PRINT_LOG)
                    Log.d(LOG, "Refreshing location");
                currentLocation = location;
                locationUpdateTimestamp = System.currentTimeMillis();
                // stop draining battery life
                finalizedLocationManager.removeUpdates(this);
            }

            // not used yet
            public void onProviderDisabled(String provider) {
            }

            public void onProviderEnabled(String provider) {
            }

            public void onStatusChanged(String provider, int status, Bundle extras) {
            }
        }, context.getMainLooper());
    }
}

From source file:de.madvertise.android.sdk.MadvertiseUtil.java

/**
 * Try to update current location. Non blocking call.
 * //from   w  ww.  java  2s. c o m
 * @param context
 *            application context
 */
public static void refreshCoordinates(final Context context) {
    MadvertiseUtil.logMessage(null, Log.DEBUG, "Trying to refresh location");

    if (context == null) {
        MadvertiseUtil.logMessage(null, Log.DEBUG, "Context not set - quit location refresh");
        return;
    }

    // check if we need a regular update
    if ((sLocationUpdateTimestamp + MadvertiseUtil.SECONDS_TO_REFRESH_LOCATION * 1000) > System
            .currentTimeMillis()) {
        MadvertiseUtil.logMessage(null, Log.DEBUG, "It's not time yet for refreshing the location");
        return;
    }

    synchronized (context) {
        // recheck, if location was updated by another thread while we
        // paused
        if ((sLocationUpdateTimestamp + MadvertiseUtil.SECONDS_TO_REFRESH_LOCATION * 1000) > System
                .currentTimeMillis()) {
            MadvertiseUtil.logMessage(null, Log.DEBUG, "Another thread updated the loation already");
            return;
        }

        boolean permissionCoarseLocation = MadvertiseUtil
                .checkPermissionGranted(android.Manifest.permission.ACCESS_COARSE_LOCATION, context);
        boolean permissionFineLocation = MadvertiseUtil
                .checkPermissionGranted(android.Manifest.permission.ACCESS_FINE_LOCATION, context);

        // return (null) if we do not have any permissions
        if (!permissionCoarseLocation && !permissionFineLocation) {
            MadvertiseUtil.logMessage(null, Log.DEBUG, "No permissions for requesting the location");
            return;
        }

        // return (null) if we can't get a location manager
        LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        if (locationManager == null) {
            MadvertiseUtil.logMessage(null, Log.DEBUG, "Unable to fetch a location manger");
            return;
        }

        String provider = null;
        Criteria criteria = new Criteria();
        criteria.setCostAllowed(false);

        // try to get coarse location first
        if (permissionCoarseLocation) {
            criteria.setAccuracy(Criteria.ACCURACY_COARSE);
            provider = locationManager.getBestProvider(criteria, true);
        }

        // try to get gps location if coarse locatio did not work
        if (provider == null && permissionFineLocation) {
            criteria.setAccuracy(Criteria.ACCURACY_FINE);
            provider = locationManager.getBestProvider(criteria, true);
        }

        // still no provider, return (null)
        if (provider == null) {
            MadvertiseUtil.logMessage(null, Log.DEBUG, "Unable to fetch a location provider");
            return;
        }

        // create a finalized reference to the location manager, in order to
        // access it in the inner class
        final LocationManager finalizedLocationManager = locationManager;
        sLocationUpdateTimestamp = System.currentTimeMillis();
        locationManager.requestLocationUpdates(provider, 0, 0, new LocationListener() {
            @Override
            public void onLocationChanged(Location location) {
                MadvertiseUtil.logMessage(null, Log.DEBUG, "Refreshing location");
                sCurrentLocation = location;
                sLocationUpdateTimestamp = System.currentTimeMillis();
                // stop draining battery life
                finalizedLocationManager.removeUpdates(this);
            }

            // not used yet
            @Override
            public void onProviderDisabled(String provider) {
            }

            @Override
            public void onProviderEnabled(String provider) {
            }

            @Override
            public void onStatusChanged(String provider, int status, Bundle extras) {
            }
        }, context.getMainLooper());
    }
}