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:name.gumartinm.weather.information.activity.MapActivity.java

public void onClickGetLocation(final View v) {
    // TODO: Somehow I should show a progress dialog.
    // If Google Play Services is available
    if (this.mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
        // TODO: Hopefully there will be results even if location did not change...   
        final Criteria criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_FINE);
        criteria.setAltitudeRequired(false);
        criteria.setBearingAccuracy(Criteria.NO_REQUIREMENT);
        criteria.setBearingRequired(false);
        criteria.setCostAllowed(false);/*w ww. java  2 s.c  o m*/
        criteria.setHorizontalAccuracy(Criteria.ACCURACY_HIGH);
        criteria.setPowerRequirement(Criteria.POWER_MEDIUM);
        criteria.setSpeedAccuracy(Criteria.NO_REQUIREMENT);
        criteria.setSpeedRequired(false);
        criteria.setVerticalAccuracy(Criteria.ACCURACY_HIGH);

        this.mLocationManager.requestSingleUpdate(criteria, this, null);
    } else {
        Toast.makeText(this, this.getString(R.string.weather_map_not_enabled_location), Toast.LENGTH_LONG)
                .show();
    }
    // Trying to use the synchronous calls. Problems: mGoogleApiClient read/store from different threads.
    // new GetLocationTask(this).execute();
}

From source file:com.fpil.android.remotesensor.MainDisplay.java

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);

    Criteria locationCriteria = new Criteria();
    locationCriteria.setAccuracy(Criteria.ACCURACY_FINE);
    locationCriteria.setAltitudeRequired(false);
    locationCriteria.setBearingRequired(false);
    locationCriteria.setCostAllowed(true);
    locationCriteria.setPowerRequirement(Criteria.NO_REQUIREMENT);

    locationProviderName = locationManager.getBestProvider(locationCriteria, true);

    if (ActivityCompat.checkSelfPermission(getActivity(),
            Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
            && ActivityCompat.checkSelfPermission(getActivity(),
                    Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        Toast.makeText(getActivity(), R.string.turn_on_gps, Toast.LENGTH_LONG).show();
        Intent myIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
        this.startActivity(myIntent);
    }/*from   w w w .  ja v  a2 s. c o  m*/

    if (locationProviderName != null && locationManager.isProviderEnabled(locationProviderName)) {
        // Provider is enabled
        Toast.makeText(getActivity(), R.string.gps_available, Toast.LENGTH_LONG).show();
    } else {
        // Provider not enabled, prompt user to enable it
        Toast.makeText(getActivity(), R.string.turn_on_gps, Toast.LENGTH_LONG).show();
        Intent myIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
        this.startActivity(myIntent);
    }
}

From source file:com.anton.gavel.GavelMain.java

@TargetApi(Build.VERSION_CODES.GINGERBREAD)
@Override//from   ww  w .j a  v  a2s  .c  om
public void onClick(View arg0) {
    //click listener for location button

    LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    Criteria criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_FINE);
    criteria.setPowerRequirement(Criteria.POWER_HIGH);
    //String provider = locationManager.getBestProvider(criteria, true);

    locationManager.requestSingleUpdate(criteria, new LocationListener() {
        @Override
        public void onLocationChanged(Location location) {
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.GINGERBREAD)
                createDialog(DIALOG_NO_GEOCODING);
            else if (Geocoder.isPresent())
                (new ReverseGeocodingTask(getBaseContext())).execute(new Location[] { location });
            // Invoking reverse geocoding in an AsyncTask. 
        }

        @Override
        public void onProviderDisabled(String provider) {
        }

        @Override
        public void onProviderEnabled(String provider) {
        }

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

    }, null);

}

From source file:fashiome.android.fragments.MapListFragment.java

private LatLng getLastKnownLocation(boolean isMoveMarker) {
    LocationManager lm = (LocationManager) AppStarter.getAppContext()
            .getSystemService(Context.LOCATION_SERVICE);
    Criteria criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_LOW);
    String provider = lm.getBestProvider(criteria, true);
    if (provider == null) {
        return null;
    }//from   w w w  . ja v a2s  . co  m
    Activity activity = getActivity();
    if (activity == null) {
        return null;
    }
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (activity.checkSelfPermission(
                Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
                && activity.checkSelfPermission(
                        Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            return null;
        }
    }
    Location loc = lm.getLastKnownLocation(provider);
    if (loc != null) {
        LatLng latLng = new LatLng(loc.getLatitude(), loc.getLongitude());
        if (isMoveMarker) {
            moveMarker(latLng);
        }
        return latLng;
    }
    return null;
}

From source file:org.opensmc.mytracks.cyclesmc.MainInput.java

private LatLng myCurrentLocation() {
    Criteria criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_FINE);
    Location loc = locationManager.getLastKnownLocation(locationManager.getBestProvider(criteria, false));
    if (loc != null) {
        return new LatLng(loc.getLatitude(), loc.getLongitude());
    } else {//from  w ww.  ja va  2 s  .  co m
        // try with coarse accuracy
        criteria.setAccuracy(Criteria.ACCURACY_FINE);
        loc = locationManager.getLastKnownLocation(locationManager.getBestProvider(criteria, false));

        if (loc == null) {
            return new LatLng(39.952451, -75.163664); // city hall by default
        }
    }
    return null;
}

From source file:com.alibaba.weex.extend.module.location.DefaultLocation.java

private WXLocationListener findLocation(String watchId, String sucCallback, String errorCallback,
        boolean enableHighAccuracy, boolean enableAddress) {
    //    WXLogUtils.d(TAG, "into--[findLocation] mWatchId:" + watchId + "\nsuccessCallback:" + sucCallback + "\nerrorCallback:" + errorCallback + "\nenableHighAccuracy:" + enableHighAccuracy + "\nmEnableAddress:" + enableAddress);

    if (mLocationManager == null) {
        mLocationManager = (LocationManager) mWXSDKInstance.getContext()
                .getSystemService(Context.LOCATION_SERVICE);
    }//w  w  w  .jav a 2  s .c o m
    Criteria criteria = new Criteria();
    if (enableHighAccuracy) {
        criteria.setAccuracy(Criteria.ACCURACY_COARSE);
    }
    //String provider = locationManager.getBestProvider(criteria, false);
    if (ActivityCompat.checkSelfPermission(mWXSDKInstance.getContext(),
            Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
            && ActivityCompat.checkSelfPermission(mWXSDKInstance.getContext(),
                    Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
        WXLocationListener WXLocationListener = new WXLocationListener(mLocationManager, mWXSDKInstance,
                watchId, sucCallback, errorCallback, enableAddress);
        mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME, MIN_DISTANCE,
                WXLocationListener);
        mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME, MIN_DISTANCE,
                WXLocationListener);
        return WXLocationListener;
    } else {
        Map<String, Object> options = new HashMap<>();
        options.put(ERROR_CODE, ErrorCode.NO_PERMISSION_ERROR);
        options.put(ERROR_MSG, ErrorMsg.NO_PERMISSION_ERROR);
        WXSDKManager.getInstance().callback(mWXSDKInstance.getInstanceId(), errorCallback, options);
    }
    return null;
}

From source file:org.berlin_vegan.bvapp.activities.LocationsOverviewActivity.java

private void requestGpsLocationUpdates() {
    mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

    Criteria criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_FINE);
    String provider = mLocationManager.getBestProvider(criteria, true);
    if (provider != null) {
        mGpsProviderAvailable = true;/*w ww .j  a  v  a2 s . c om*/
        mLocationManager.requestSingleUpdate(criteria, mLocationListener, null);
    } else {
        mGpsProviderAvailable = false;
    }
}

From source file:com.wishlist.Wishlist.java

public void fetchCurrentLocation() {
    new Thread() {
        public void run() {
            Looper.prepare();/*from w w w.j a v a  2 s.  c  o m*/
            mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
            MyLocationListener locationListener = new MyLocationListener();
            Criteria criteria = new Criteria();
            criteria.setAccuracy(Criteria.ACCURACY_COARSE);
            String provider = mLocationManager.getBestProvider(criteria, true);
            if (provider != null && mLocationManager.isProviderEnabled(provider)) {
                mLocationManager.requestLocationUpdates(provider, 1, 0, locationListener,
                        Looper.getMainLooper());
            } else {
                showToast("Please turn on handset's GPS");
            }
            Looper.loop();
        }
    }.start();
}

From source file:com.jesjimher.bicipalma.MesProperesActivity.java

/** Called when the activity is first created. */
@Override//w  w  w .j  av a 2s  .c  o  m
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mesproperes);
    this.prefs = PreferenceManager.getDefaultSharedPreferences(this);

    // Si s'ha d'activar el wifi en inici, fer-ho
    WifiManager wm = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
    // Guardar el estado actual para restaurarlo al salir
    this.estatWifi = wm.isWifiEnabled();
    if (prefs.getBoolean("activarWifiPref", false))
        wm.setWifiEnabled(true);

    // Cargamos la versin cacheada de las estaciones
    leerCacheEstaciones();

    actualizarListado();

    // Iniciamos la descarga de las estaciones y su estado desde la web (en un thread aparte)
    descargaEstaciones = new RecuperarEstacionesTask(this);
    descargaEstaciones.execute();

    // Inicialmente se busca por red (ms rpido)
    //       dBuscaUbic=ProgressDialog.show(c, "",getString(R.string.buscandoubica),true,true);
    //        Toast.makeText(getApplicationContext(), "Activando", Toast.LENGTH_SHORT).show();
    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    Criteria criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_COARSE);
    providerCoarse = locationManager.getBestProvider(criteria, true);

    if (providerCoarse == null) {
        Toast.makeText(getApplicationContext(), "No hay forma de posicionarse", Toast.LENGTH_SHORT).show();
        return;
    }
    locationManager.requestLocationUpdates(providerCoarse, 10, 0, (LocationListener) this);

    // Usar ltima ubicacin conocida de red para empezar y recibir futuras actualizaciones
    lBest = locationManager.getLastKnownLocation(providerCoarse);

    // Guardar el inicio de bsqueda de ubicacin para no pasarse de tiempo
    //tIni=new Date().getTime();
    tIni = System.currentTimeMillis();

    // Crear listeners para mostrar estacin en mapa, o abrir men con clic largo
    ListView lv = (ListView) findViewById(R.id.listado);
    lv.setOnItemClickListener((OnItemClickListener) this);
    lv.setOnItemLongClickListener((OnItemLongClickListener) this);
}

From source file:com.zainsoft.ramzantimetable.QiblaActivity.java

private void registerForGPS() {
    Criteria criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_COARSE);
    criteria.setPowerRequirement(Criteria.POWER_LOW);
    criteria.setAltitudeRequired(false);
    criteria.setBearingRequired(false);// ww  w. j  a va 2 s .  co  m
    criteria.setSpeedRequired(false);
    criteria.setCostAllowed(true);
    LocationManager locationManager = ((LocationManager) getSystemService(Context.LOCATION_SERVICE));
    String provider = locationManager.getBestProvider(criteria, true);

    if (provider != null) {
        locationManager.requestLocationUpdates(provider, MIN_LOCATION_TIME, MIN_LOCATION_DISTANCE,
                qiblaManager);
    }
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_LOCATION_TIME,
            MIN_LOCATION_DISTANCE, qiblaManager);
    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_LOCATION_TIME,
            MIN_LOCATION_DISTANCE, qiblaManager);
    Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    if (location == null) {
        location = ((LocationManager) getSystemService(Context.LOCATION_SERVICE))
                .getLastKnownLocation(LocationManager.GPS_PROVIDER);
    }
    if (location != null) {
        qiblaManager.onLocationChanged(location);
    }

}