public Location getLocation() { try { locationManager = (LocationManager) mContext.getSystemService(LOCATION_SERVICE); // getting GPS status isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); // getting network status isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER); if (!isGPSEnabled && !isNetworkEnabled) { // no network provider is enabled } else { this.canGetLocation = true; // First get location from Network Provider if (isNetworkEnabled) { locationManager.requestLocationUpdates( LocationManager.NETWORK_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this); Log.d("Network", "Network"); if (locationManager != null) { location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); if (location != null) { latitude = location.getLatitude(); longitude = location.getLongitude(); } } } } // if GPS Enabled get lat/long using GPS Services if (isGPSEnabled) { { Criteria crit = new Criteria(); crit.setAccuracy( Criteria.ACCURACY_LOW); // sets the level of accuracy you reqired in order to get a // reading locationManager.requestLocationUpdates( locationManager.getBestProvider(crit, false), MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this); Log.d("GPS Enabled", "GPS Enabled"); if (locationManager != null) { if (location == null) { location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); if (location != null) { latitude = location.getLatitude(); longitude = location.getLongitude(); } } } } } } catch (Exception e) { e.printStackTrace(); } finally { if (location != null) listener.OnLicationChanged( location); // trigger the Location update listener manually to set the initial location // taken from Mobile service provider or through WiFi } return location; }
@Override public void onLocationChanged(Location location) { // Toast.makeText(getApplicationContext(), "Your Location is - \nLat: " + location.getLatitude() // + "\nLong: " + location.getLongitude(), Toast.LENGTH_LONG).show(); listener.OnLicationChanged(location); }