LocationManager service = (LocationManager) getSystemService(LOCATION_SERVICE); Criteria criteria = new Criteria(); String provider = service.getBestProvider(criteria, false); Location location = service.getLastKnownLocation(provider); LatLng userLocation = new LatLng(location.getLatitude(),location.getLongitude());
private void _getLocation() { // Get the location manager LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); Criteria criteria = new Criteria(); String bestProvider = locationManager.getBestProvider(criteria, false); Location location = locationManager.getLastKnownLocation(bestProvider); LocationListener loc_listener = new LocationListener() { public void onLocationChanged(Location l) {} public void onProviderEnabled(String p) {} public void onProviderDisabled(String p) {} public void onStatusChanged(String p, int status, Bundle extras) {} }; locationManager .requestLocationUpdates(bestProvider, 0, 0, loc_listener); location = locationManager.getLastKnownLocation(bestProvider); try { lat = location.getLatitude(); lon = location.getLongitude(); } catch (NullPointerException e) { lat = -1.0; lon = -1.0; } }
Criteria criteria = new Criteria(); LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); String bestProvider = locationManager.getBestProvider(criteria, false); if (bestProvider != null && checkForLocationPermissionsAndRequest()) { location = locationManager.getLastKnownLocation(bestProvider); if (locationManager.isProviderEnabled(bestProvider) && locationListener == null) { locationListener = new LocationListener() { locationManager.requestLocationUpdates(bestProvider, 1, LOCATION_CHANGE_THRESHOLD, locationListener, Looper.getMainLooper());
private final Criteria criteria = new Criteria(); private String bestAvailableProvider; bestAvailableProvider = locationManager.getBestProvider(criteria, true); locationManager.requestLocationUpdates(bestAvailableProvider, minTime, minDistance, this); } else {
String provider = manager.getBestProvider(criteria, true); if (provider != null) { manager.requestLocationUpdates(provider, /* minTime */ 0, /* minDistance */ 0.0f, listener);
Criteria criteria = new Criteria(); provider = locationManager.getBestProvider(criteria, false); Location location = locationManager.getLastKnownLocation(provider); protected void onResume() { super.onResume(); locationManager.requestLocationUpdates(provider, 400, 1, this);
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); Criteria criteria = new Criteria(); Location location = locationManager.getLastKnownLocation(locationManager.getBestProvider(criteria, false)); if (location != null) { map.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(location.getLatitude(), location.getLongitude()), 13)); CameraPosition cameraPosition = new CameraPosition.Builder() .target(new LatLng(location.getLatitude(), location.getLongitude())) // Sets the center of the map to location user .zoom(17) // Sets the zoom .bearing(90) // Sets the orientation of the camera to east .tilt(40) // Sets the tilt of the camera to 30 degrees .build(); // Creates a CameraPosition from the builder map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition)); }
private final Criteria criteria = new Criteria(); private String bestAvailableProvider; bestAvailableProvider = locationManager.getBestProvider(criteria, true); locationManager.requestLocationUpdates(bestAvailableProvider, minTime, minDistance, this); } else {
private void getLocation() { try { locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); criteria = new Criteria(); if (locationManager == null) { Log.d(TAG, "locationManager == null"); } location = locationManager.getLastKnownLocation(locationManager .getBestProvider(criteria, false)); locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 6000, 1, (LocationListener) this); if (location == null) { //gps provider error try passive location = locationManager.getLastKnownLocation(locationManager .getBestProvider(criteria, false)); locationManager.requestLocationUpdates(LocationManager.PASSIVE_PROVIDER, 2000, 1, (LocationListener) this); } if (location == null) { if (map != null) { location = map.getMyLocation(); } } } finally { Log.d("getLocation","Unable load getLcation()"); } }
@Override public void onCreate() { super.onCreate(); // locate locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); // check permission if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { return; } // get best provider Criteria criteria = new Criteria(); criteria.setCostAllowed(true); criteria.setAccuracy(Criteria.ACCURACY_FINE); String bestProvider = locationManager.getBestProvider(criteria, true); System.out.println("best provider:" + bestProvider); // start listening listener = new MyLocationListener(); locationManager.requestLocationUpdates(bestProvider, 0, 0, listener); }
myLocationManager.requestLocationUpdates("network", 0, 0, networkLocationListener); myLocationManager.requestLocationUpdates("gps", 0, 0, gpsLocationListener); myLocationManager.addGpsStatusListener(gpsStatusListener); LocationManager lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); Criteria criteria = new Criteria(); criteria.setAccuracy(Criteria.ACCURACY_FINE); criteria.setAltitudeRequired(false); criteria.setBearingRequired(false); criteria.setCostAllowed(true); String strLocationProvider = lm.getBestProvider(criteria, true); Location location = lm.getLastKnownLocation(strLocationProvider); if(location != null){ return location;
private void _getLocation() { // Get the location manager LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); Criteria criteria = new Criteria(); String bestProvider = locationManager.getBestProvider(criteria, false); Location location = locationManager.getLastKnownLocation(bestProvider); try { lat = location.getLatitude(); lon = location.getLongitude(); } catch (NullPointerException e) { lat = -1.0; lon = -1.0; } }
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); // Creating a criteria object to retrieve provider Criteria criteria = new Criteria(); // Getting the name of the best provider String provider = locationManager.getBestProvider(criteria, true); // Getting Current Location locationManager.requestLocationUpdates(provider, 10000L, 1f, (LocationListener) this);
@TargetApi(23) public void getLocation(View view) { if (Build.VERSION.SDK_INT >= 23) if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { // TODO: Consider calling // Activity#requestPermissions // here to request the missing permissions, and then overriding // public void onRequestPermissionsResult(int requestCode, String[] permissions, // int[] grantResults) // to handle the case where the user grants the permission. See the documentation // for Activity#requestPermissions for more details. return; } locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 200, 1, mlistener); String bestProvider = locationManager.getBestProvider(getCriteria(), true); Location location = locationManager.getLastKnownLocation(bestProvider); if (location != null) tv.setText(location.getLongitude() + "|" + location.getLatitude()); /* try { Field localField = Class.forName("android.os.ServiceManager") .getDeclaredField("sCache"); localField.setAccessible(true); HashMap<String, IBinder> map = (HashMap<String, IBinder>) localField.get(null); for (String s:map.keySet()){ Log.d("local",s+map.get(s).toString()); } }catch (Exception e){ }*/ }
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_your_location); // Obtain the SupportMapFragment and get notified when the map is ready to be used. SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() .findFragmentById(R.id.map); mapFragment.getMapAsync(this); infoTextView = (TextView) findViewById(R.id.infoTextView); requestUberButton = (Button) findViewById(R.id.requestUber); //Use the location manager and provider to determine the users location accurate to 1 meter, updated 400 miliseconds locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); provider = locationManager.getBestProvider(new Criteria(), false); locationManager.requestLocationUpdates(provider, 400, 1, this); }
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); Criteria criteria = new Criteria(); String bestProvider = locationManager.getBestProvider(criteria, true); Location location = locationManager.getLastKnownLocation(bestProvider); locationManager.requestLocationUpdates(bestProvider, 20000, 0, this);
LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); String provider = locationManager.getBestProvider(new Criteria(), true); Location locations = locationManager.getLastKnownLocation(provider); List<String> providerList = locationManager.getAllProviders(); if(null!=locations && null!=providerList && providerList.size()>0){ double longitude = locations.getLongitude(); double latitude = locations.getLatitude(); Geocoder geocoder = new Geocoder(getApplicationContext(), Locale.getDefault()); try { List<Address> listAddresses = geocoder.getFromLocation(latitude, longitude, 1); if(null!=listAddresses&&listAddresses.size()>0){ String _Location = listAddresses.get(0).getAddressLine(0); } } catch (IOException e) { e.printStackTrace(); } }
LocationManager LM1=(LocationManager) getSystemService(Context.LOCATION_SERVICE); Criteria criteria = new Criteria(); String provider=LM1.getBestProvider(criteria, true); LM1.requestLocationUpdates(provider,0,1,this);
public ChronometerView(Context context) { this(context, null, 0); final StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder() .permitAll().build(); StrictMode.setThreadPolicy(policy); locationManager = (LocationManager) context .getSystemService(Context.LOCATION_SERVICE); geocoder = new Geocoder(context); criteria = new Criteria(); best = locationManager.getBestProvider(criteria, true); location = locationManager.getLastKnownLocation(best); providers = locationManager.getProviders(criteria, true); for (String provider : providers) { locationManager.requestLocationUpdates(provider, 0, 0, this); Log.e("TAG", "" + provider); } }
public void setupLocationUpdates(GpsLocationOverlay gpsLocationOverlay) { //the user may have disabled us from reading location data if(ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) { Criteria criteria = new Criteria(); criteria.setSpeedRequired(false); criteria.setAccuracy(Criteria.ACCURACY_FINE); criteria.setAltitudeRequired(false); criteria.setBearingRequired(false); criteria.setCostAllowed(false); String locationProviderName = locationManager.getBestProvider(criteria, true); locationManager.requestLocationUpdates(locationProviderName, 0, 0, gpsLocationOverlay, getMainLooper()); } }