Code example for LocationManager: Finding current location

Methods: getLastKnownLocationisProviderEnabledrequestLocationUpdates

200
	        locationManager = (LocationManager) myAndroidContext
	                .getSystemService(Context.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;
	            if (isNetworkEnabled) {
	                locationManager.requestLocationUpdates(
	                        LocationManager.NETWORK_PROVIDER,
	                        MIN_TIME_BW_UPDATES,
	                        MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
	                Log.d("activity", "LOC Network Enabled");
	                if (locationManager != null) {
	                    location = locationManager
	                            .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
	                    if (location != null) {
	    	                Log.d("activity", "LOC by Network");
	                        latitude = location.getLatitude();
	                        longitude = location.getLongitude();
	                    } 
	                } 
	            } 
	            // if GPS Enabled get lat/long using GPS Services 
	            if (isGPSEnabled) {
	                if (location == null) {
	                    locationManager.requestLocationUpdates(
	                            LocationManager.GPS_PROVIDER,
	                            MIN_TIME_BW_UPDATES,
	                            MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
	                    Log.d("activity", "RLOC: GPS Enabled");
	                    if (locationManager != null) {
	                        location = locationManager
	                                .getLastKnownLocation(LocationManager.GPS_PROVIDER);
	                        if (location != null) {
	    	                    Log.d("activity", "RLOC: loc by GPS");
 
	                            latitude = location.getLatitude();
	                            longitude = location.getLongitude();