Code example for LocationManager: Finding current location

Methods: getLastKnownLocationisProviderEnabledrequestLocationUpdates

0
	public Location getLocation() {
		try { 
			mLocationManager = (LocationManager) mContext.getSystemService(LOCATION_SERVICE);
 
			// getting GPS status 
			isGPSEnabled = mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
 
			// getting network status 
			isNetworkEnabled = mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
 
			if (!isGPSEnabled && !isNetworkEnabled) {
				// no network provider is enabled 
			} else { 
				this.canGetLocation = true;
				if (isNetworkEnabled) {
					mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES,
							this);
					Log.d("Network", "Network");
					if (mLocationManager != null) {
						location = mLocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
						if (location != null) {
							latitude = location.getLatitude();
							longitude = location.getLongitude();
						} 
					} 
				} 
				// if GPS Enabled get lat/long using GPS Services 
				else if (isGPSEnabled) {
					if (location == null) {
						mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES,
								this);
						Log.d("GPS Enabled", "GPS Enabled");
						if (mLocationManager != null) {
							location = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
							if (location != null) {
								latitude = location.getLatitude();
								longitude = location.getLongitude();
							} 
						}