List of usage examples for android.location LocationManager getBestProvider
public String getBestProvider(Criteria criteria, boolean enabledOnly)
From source file:com.eutectoid.dosomething.PickerActivity.java
@Override protected void onStart() { super.onStart(); if (FRIEND_PICKER.equals(getIntent().getData())) { try {// w ww . ja v a2 s . co m friendPickerFragment.loadData(false); } catch (Exception ex) { onError(ex); } } else if (PLACE_PICKER.equals(getIntent().getData())) { try { Location location = null; Criteria criteria = new Criteria(); LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); String bestProvider = locationManager.getBestProvider(criteria, false); // API 23: we have to check if ACCESS_FINE_LOCATION and/or ACCESS_COARSE_LOCATION permission are granted if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) { location = locationManager.getLastKnownLocation(bestProvider); if (locationManager.isProviderEnabled(bestProvider) && locationListener == null) { locationListener = new LocationListener() { @Override public void onLocationChanged(Location location) { boolean updateLocation = true; Location prevLocation = placePickerFragment.getLocation(); if (prevLocation != null) { updateLocation = location.distanceTo(prevLocation) >= LOCATION_CHANGE_THRESHOLD; } if (updateLocation) { placePickerFragment.setLocation(location); placePickerFragment.loadData(true); } } @Override public void onStatusChanged(String s, int i, Bundle bundle) { } @Override public void onProviderEnabled(String s) { } @Override public void onProviderDisabled(String s) { } }; locationManager.requestLocationUpdates(bestProvider, 1, LOCATION_CHANGE_THRESHOLD, locationListener, Looper.getMainLooper()); } } if (location != null) { placePickerFragment.setLocation(location); placePickerFragment.setRadiusInMeters(SEARCH_RADIUS_METERS); placePickerFragment.setSearchText(SEARCH_TEXT); placePickerFragment.setResultsLimit(SEARCH_RESULT_LIMIT); placePickerFragment.loadData(false); } } catch (Exception ex) { onError(ex); } } }
From source file:com.example.david.wheretogo_test1.PickerActivity.java
@Override protected void onStart() { super.onStart(); if (FRIEND_PICKER.equals(getIntent().getData())) { try {//from ww w.j a v a 2 s. c o m friendPickerFragment.loadData(false); } catch (Exception ex) { onError(ex); } } else if (PLACE_PICKER.equals(getIntent().getData())) { try { Location location = null; Criteria criteria = new Criteria(); LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); String bestProvider = locationManager.getBestProvider(criteria, false); if (bestProvider != null) { location = locationManager.getLastKnownLocation(bestProvider); if (locationManager.isProviderEnabled(bestProvider) && locationListener == null) { locationListener = new LocationListener() { @Override public void onLocationChanged(Location location) { boolean updateLocation = true; Location prevLocation = placePickerFragment.getLocation(); if (prevLocation != null) { updateLocation = location.distanceTo(prevLocation) >= LOCATION_CHANGE_THRESHOLD; } if (updateLocation) { placePickerFragment.setLocation(location); placePickerFragment.loadData(true); } } @Override public void onStatusChanged(String s, int i, Bundle bundle) { } @Override public void onProviderEnabled(String s) { } @Override public void onProviderDisabled(String s) { } }; locationManager.requestLocationUpdates(bestProvider, 1, LOCATION_CHANGE_THRESHOLD, locationListener, Looper.getMainLooper()); } } if (location == null) { String model = Build.MODEL; if (model.equals("sdk") || model.equals("google_sdk") || model.contains("x86")) { // this may be the emulator, pretend we're in an exotic place location = SAN_FRANCISCO_LOCATION; } } if (location != null) { placePickerFragment.setLocation(location); placePickerFragment.setRadiusInMeters(SEARCH_RADIUS_METERS); placePickerFragment.setSearchText(SEARCH_TEXT); placePickerFragment.setResultsLimit(SEARCH_RESULT_LIMIT); placePickerFragment.loadData(false); } else { onError(getResources().getString(R.string.no_location_error), true); } } catch (Exception ex) { onError(ex); } } }
From source file:com.example.amapapplicationtest.MainActivity.java
/** Called when the activity is first created. */ @Override/* ww w . j a v a2s .co m*/ public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initMap(); LocationManager lm = (LocationManager) getSystemService(LOCATION_SERVICE); String provider = lm.getBestProvider(new Criteria(), true); LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); Criteria criteria = new Criteria(); Location location = locationManager.getLastKnownLocation(locationManager.getBestProvider(criteria, false)); if (location != null) { googlemap.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 googlemap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition)); } if (provider == null) { onProviderDisabled(provider); } data = new MarkerDataSource(context); try { data.open(); } catch (Exception e) { Log.i("hello", "hello"); } List<MyMarkerObj> m = data.getMyMarkers(); for (int i = 0; i < m.size(); i++) { String[] slatlng = m.get(i).getPosition().split(" "); LatLng lat = new LatLng(Double.valueOf(slatlng[0]), Double.valueOf(slatlng[1])); googlemap.addMarker( new MarkerOptions().title(m.get(i).getTitle()).snippet(m.get(i).getSnippet()).position(lat)); googlemap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() { public void onInfoWindowClick(Marker marker) { Toast.makeText(getBaseContext(), "Info Window clicked@" + marker.getId(), Toast.LENGTH_SHORT) .show(); } }); // marker.view(); // data.getMarker(new MyMarkerObj(marker.getTitle(), marker.getSnippet(), marker.getPosition().latitude + " " + marker.getPosition().longitude)); } googlemap.setOnMapLongClickListener(new GoogleMap.OnMapLongClickListener() { public void onMapLongClick(final LatLng latlng) { LayoutInflater li = LayoutInflater.from(context); final View v = li.inflate(R.layout.alertlayout, null); AlertDialog.Builder builder = new AlertDialog.Builder(context); builder.setView(v); builder.setCancelable(false); builder.setPositiveButton("Create", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { EditText title = (EditText) v.findViewById(R.id.ettitle); EditText snippet = (EditText) v.findViewById(R.id.etsnippet); googlemap.addMarker(new MarkerOptions().title(title.getText().toString()) .snippet(snippet.getText().toString()) .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE)) .position(latlng)); String sll = latlng.latitude + " " + latlng.longitude; data.addMarker( new MyMarkerObj(title.getText().toString(), snippet.getText().toString(), sll)); } }); builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.cancel(); } }); AlertDialog alert = builder.create(); alert.show(); } }); }
From source file:mx.developerbus.foodbus.FoodBus_Main.java
public LatLng 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); Double lat, lon;/*w w w . j ava 2 s . c o m*/ try { lat = location.getLatitude(); lon = location.getLongitude(); return new LatLng(lat, lon); } catch (NullPointerException e) { e.printStackTrace(); return null; } }
From source file:heartware.com.heartware_master.FB_PickerActivity.java
@Override protected void onStart() { super.onStart(); if (FRIEND_PICKER.equals(getIntent().getData())) { try {// w ww. j a va 2 s . c o m friendPickerFragment.loadData(false); } catch (Exception ex) { onError(ex); } } else if (PLACE_PICKER.equals(getIntent().getData())) { try { Location location = null; Criteria criteria = new Criteria(); LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); String bestProvider = locationManager.getBestProvider(criteria, false); if (bestProvider != null) { location = locationManager.getLastKnownLocation(bestProvider); if (locationManager.isProviderEnabled(bestProvider) && locationListener == null) { locationListener = new LocationListener() { @Override public void onLocationChanged(Location location) { boolean updateLocation = true; Location prevLocation = placePickerFragment.getLocation(); if (prevLocation != null) { updateLocation = location.distanceTo(prevLocation) >= LOCATION_CHANGE_THRESHOLD; } if (updateLocation) { placePickerFragment.setLocation(location); placePickerFragment.loadData(true); } } @Override public void onStatusChanged(String s, int i, Bundle bundle) { } @Override public void onProviderEnabled(String s) { } @Override public void onProviderDisabled(String s) { } }; locationManager.requestLocationUpdates(bestProvider, 1, LOCATION_CHANGE_THRESHOLD, locationListener, Looper.getMainLooper()); } } if (location == null) { String model = Build.MODEL; if (model.equals("sdk") || model.equals("google_sdk") || model.contains("x86")) { // this may be the emulator, pretend we're in an exotic place location = TEMPE_AZ_LOCATION; } location = TEMPE_AZ_LOCATION; // @TODO HARDCODED location } if (location != null) { location = TEMPE_AZ_LOCATION; placePickerFragment.setLocation(location); placePickerFragment.setRadiusInMeters(SEARCH_RADIUS_METERS); placePickerFragment.setSearchText(SEARCH_TEXT); placePickerFragment.setResultsLimit(SEARCH_RESULT_LIMIT); placePickerFragment.loadData(false); } else { onError(getResources().getString(R.string.no_location_error), true); } } catch (Exception ex) { onError(ex); } } }
From source file:com.example.scandevice.MainActivity.java
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); context = getApplicationContext();/*from w w w. ja v a 2 s . com*/ LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); Criteria criteria = new Criteria(); // Getting the name of the provider that meets the criteria provider = locationManager.getBestProvider(criteria, true); if (provider == null && !locationManager.isProviderEnabled(provider)) { // Get the location from the given provider List<String> list = locationManager.getAllProviders(); for (int i = 0; i < list.size(); i++) { //Get device name; String temp = list.get(i); //check usable if (locationManager.isProviderEnabled(temp)) { provider = temp; break; } } } //get location where reference last. Location location = locationManager.getLastKnownLocation(provider); if (location == null) Toast.makeText(this, "There are no available position information providers.", Toast.LENGTH_SHORT) .show(); else //GPS start from last location. onLocationChanged(location); // Set by <content src="index.html" /> in config.xml loadUrl(launchUrl); }
From source file:com.example.demo_dv_fuse.MapScreen.java
/** * @see android.app.Activity#onCreate(android.os.Bundle) *//* www.ja v a2 s . c o m*/ @Override protected void onCreate(final Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.map_screen); this.map = ((MapFragment) getFragmentManager().findFragmentById(R.id.mapView)).getMap(); this.map.setMapType(GoogleMap.MAP_TYPE_NORMAL); final int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getBaseContext()); if (status == ConnectionResult.SUCCESS) { // Google Play Services is available // Enabling MyLocation Layer of Google Map this.map.setMyLocationEnabled(true); // Getting LocationManager object from System Service LOCATION_SERVICE final LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); // Creating a criteria object to retrieve provider final Criteria criteria = new Criteria(); // Getting the name of the best provider final String provider = locationManager.getBestProvider(criteria, true); // Getting Current Location final Location location = locationManager.getLastKnownLocation(provider); if (location != null) { onLocationChanged(location); // } else { // final String coordinates[] = {"1.352566007", "103.78921587"}; // final double lat = Double.parseDouble(coordinates[0]); // final double lng = Double.parseDouble(coordinates[1]); // // // Creating a LatLng object for the current location // LatLng latLng = new LatLng(lat, lng); // // // Showing the current location in Google Map // this.map.moveCamera(CameraUpdateFactory.newLatLng(latLng)); // this.map.animateCamera(CameraUpdateFactory.zoomTo(15)); } boolean enabledGPS = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); boolean enabledWiFi = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER); // Check if enabled and if not send user to the GSP settings // Better solution would be to display a dialog and suggesting to // go to the settings if (!enabledGPS || !enabledWiFi) { Toast.makeText(this, "GPS signal not found", Toast.LENGTH_LONG).show(); Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); startActivity(intent); } locationManager.requestLocationUpdates(provider, 20000, 0, this); } else { // Google Play Services are not available final int requestCode = 10; final Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, this, requestCode); dialog.show(); } }
From source file:org.akvo.flow.ui.fragment.MapFragment.java
/** * Center the map in the given record's coordinates. If no record is provided, * the user's location will be used./*from w w w. j a v a 2 s .c om*/ * @param record */ private void centerMap(SurveyedLocale record) { if (mMap == null) { return; // Not ready yet } LatLng position = null; if (record != null && record.getLatitude() != null && record.getLongitude() != null) { // Center the map in the data point position = new LatLng(record.getLatitude(), record.getLongitude()); } else { // When multiple points are shown, center the map in user's location LocationManager manager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE); Criteria criteria = new Criteria(); criteria.setAccuracy(Criteria.ACCURACY_FINE); String provider = manager.getBestProvider(criteria, true); if (provider != null) { Location location = manager.getLastKnownLocation(provider); if (location != null) { position = new LatLng(location.getLatitude(), location.getLongitude()); } } } if (position != null) { mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(position, 10)); } }
From source file:com.wikitude.example.ARBrowserActivity.java
/** Called when the activity is first created. */ @Override//from w w w . j av a 2s . c o m public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // let the application be fullscreen this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); // check if the device fulfills the SDK'S minimum requirements if (!ArchitectView.isDeviceSupported(this)) { Toast.makeText(this, "minimum requirements not fulfilled", Toast.LENGTH_LONG).show(); this.finish(); return; } setContentView(R.layout.main); // set the devices' volume control to music to be able to change the // volume of possible soundfiles to play this.setVolumeControlStream(AudioManager.STREAM_MUSIC); this.architectView = (ArchitectView) this.findViewById(R.id.architectView); // onCreate method for setting the license key for the SDK architectView.onCreate(apiKey); // in order to inform the ARchitect framework about the user's location // Androids LocationManager is used in this case // NOT USED IN THIS EXAMPLE // locManager = // (LocationManager)getSystemService(Context.LOCATION_SERVICE); // locManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, // 0, this); // Getting LocationManager object from System Service LOCATION_SERVICE 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 Location location = locationManager.getLastKnownLocation(provider); locationManager.requestLocationUpdates(provider, 20000, 0, this); // Getting latitude of the current location fLat = location.getLatitude(); // Getting longitude of the current location fLng = location.getLongitude(); }
From source file:ly.apps.android.rest.client.example.activities.MainActivity.java
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textViewDescription = (TextView) findViewById(R.id.textview_description); textViewTemp = (TextView) findViewById(R.id.textview_temp); imageViewIcon = (ImageView) findViewById(R.id.imageview_icon); textViewWind = (TextView) findViewById(R.id.textview_wind_response); textViewHumidity = (TextView) findViewById(R.id.textview_humidity_response); textViewTempMax = (TextView) findViewById(R.id.textview_tempmax_response); textViewTempMin = (TextView) findViewById(R.id.textview_tempmin_response); textViewCity = (TextView) findViewById(R.id.textview_city); contentProgressBar = (LinearLayout) findViewById(R.id.content_progressbar); linearLayoutContent = (LinearLayout) findViewById(R.id.content); contentBottom = (LinearLayout) findViewById(R.id.bottom_content); descriptionTempContent = (LinearLayout) findViewById(R.id.description_temp_content); RestClient client = RestClientFactory.defaultClient(getApplicationContext()); api = RestServiceFactory.getService(getString(R.string.base_url), OpenWeatherAPI.class, client); LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); Criteria criteria = new Criteria(); String provider = locationManager.getBestProvider(criteria, false); Location location = locationManager.getLastKnownLocation(provider); checkImmersiveMode();/* www. jav a 2s. com*/ if ((location != null) && checkConnection(getApplicationContext())) { setLocation(location); } else { failMessage(); } }