List of usage examples for android.location Criteria Criteria
public Criteria()
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);//from w ww.jav a 2 s . com c.setBearingRequired(false); c.setSpeedRequired(false); c.setCostAllowed(true); c.setPowerRequirement(Criteria.POWER_HIGH); return c; }
From source file:com.keysolutions.meteorparties.PartyMapFragment.java
/** * Gets current location using Android's location provider * so you can zoom the map to it/*from w w w . java2 s . com*/ * @return last known Location */ @SuppressWarnings("unused") private Location getCurrentLocation() { Criteria criteria = new Criteria(); LocationManager locMan = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE); String towers = locMan.getBestProvider(criteria, false); Location location = locMan.getLastKnownLocation(towers); return location; }
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); }/*from ww w.ja va 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:com.example.mapdemo.BasicMapDemoActivity.java
private void setGeoLocation() { latitude = (TextView) findViewById(R.id.lat); longitude = (TextView) findViewById(R.id.lon); altitude = (TextView) findViewById(R.id.alt); heading = (TextView) findViewById(R.id.hea); provText = (TextView) findViewById(R.id.prov); choice = (TextView) findViewById(R.id.choice); fineAcc = (CheckBox) findViewById(R.id.fineAccuracy); choose = (Button) findViewById(R.id.chooseRadio); // Get the location manager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); // Define the criteria how to select the location provider criteria = new Criteria(); criteria.setAccuracy(Criteria.ACCURACY_COARSE); //default // user defines the criteria choose.setOnClickListener(new View.OnClickListener() { @Override//w ww . java 2 s. c o m public void onClick(View v) { // TODO Auto-generated method stub if (fineAcc.isChecked()) { criteria.setAccuracy(Criteria.ACCURACY_FINE); choice.setText("fine accuracy selected"); } else { criteria.setAccuracy(Criteria.ACCURACY_COARSE); choice.setText("coarse accuracy selected"); } } }); criteria.setCostAllowed(false); // get the best provider depending on the criteria provider = locationManager.getBestProvider(criteria, false); // the last known location of this provider Location location = locationManager.getLastKnownLocation(provider); mylistener = new MyLocationListener(); if (location != null) { mylistener.onLocationChanged(location); } else { // leads to the settings because there is no last known location Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); startActivity(intent); } // location updates: at least 1 meter and 200 millsecs change locationManager.requestLocationUpdates(provider, 200, 1, mylistener); }
From source file:de.sindzinski.wetter.MainActivity.java
public Location getLocation() { Location location = null;//from w w w. jav a2 s.co m String provider = ""; String locationSetting = ""; if (!checkAndAskForPermission()) { return null; } try { // Get the location manager LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); // Define the criteria how to select the locatioin provider -> use // default Criteria criteria = new Criteria(); if (locationManager != null) { provider = locationManager.getBestProvider(criteria, false); location = locationManager.getLastKnownLocation(provider); } } catch (Exception ex) { Log.e(LOG_TAG, "Error creating location service: " + ex.getMessage()); } return location; }
From source file:com.mobilyzer.util.PhoneUtils.java
/** * Lazily initializes the location manager. * * As a side effect, assigns locationManager and locationProviderName. */// w w w . j a va2s . c om private synchronized void initLocation() { if (locationManager == null) { LocationManager manager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); Criteria criteriaCoarse = new Criteria(); /* "Coarse" accuracy means "no need to use GPS". * Typically a gShots phone would be located in a building, * and GPS may not be able to acquire a location. * We only care about the location to determine the country, * so we don't need a super accurate location, cell/wifi is good enough. */ criteriaCoarse.setAccuracy(Criteria.ACCURACY_COARSE); criteriaCoarse.setPowerRequirement(Criteria.POWER_LOW); String providerName = manager.getBestProvider(criteriaCoarse, /*enabledOnly=*/true); List<String> providers = manager.getAllProviders(); for (String providerNameIter : providers) { try { LocationProvider provider = manager.getProvider(providerNameIter); } catch (SecurityException se) { // Not allowed to use this provider Logger.w("Unable to use provider " + providerNameIter); continue; } Logger.i(providerNameIter + ": " + (manager.isProviderEnabled(providerNameIter) ? "enabled" : "disabled")); } /* Make sure the provider updates its location. * Without this, we may get a very old location, even a * device powercycle may not update it. * {@see android.location.LocationManager.getLastKnownLocation}. */ manager.requestLocationUpdates(providerName, /*minTime=*/0, /*minDistance=*/0, new LoggingLocationListener(), Looper.getMainLooper()); locationManager = manager; locationProviderName = providerName; } assert locationManager != null; assert locationProviderName != 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 www.j ava 2 s .com 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:com.wishlist.Wishlist.java
public void fetchCurrentLocation() { new Thread() { public void run() { Looper.prepare();//from w w w . ja v a 2s . 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: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 {// w ww. j a va2s . 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:li.barter.fragments.AbstractBarterLiFragment.java
public boolean isLocationServiceEnabled() { LocationManager lm = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE); Criteria criteria = new Criteria(); String provider = lm.getBestProvider(criteria, true); return ((provider != null) && !LocationManager.PASSIVE_PROVIDER.equals(provider)); }