Search This Blog

Showing posts with label Location APIs. Show all posts
Showing posts with label Location APIs. Show all posts

Sunday, 24 June 2012

Implementing the location aware apps on Android



Android provide the platform to build the location aware applications. You can implement the location based application using the android.location package classes.

The core of the android location framework is Location Manager. You can request the location manager service from system using the getSystemService API call.

See below the implementation for the activity requesting the location of the user from GPS location provider.

//create the object for the location manager
private LocationManager mLocationManager;
//create the object for the GPS location
private Location gpsLocation;
//frequency for location determination
private static final int TEN_SECONDS = 10000;
//distance for location determination
private static final int TEN_METERS = 10;

// Get a reference to the LocationManager object.
mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

Now implement a check for the location services are enabled on the device or not.

    protected void onStart() {
        super.onStart();

        // Check if the GPS setting is currently enabled on the device.
        // This check should be implement in onStart Method as the system calls this method every time the activity resume
        LocationManager locationManager =
                (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        final boolean gpsProvider = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);

        if (!gpsProvider) {
            // Create an alert dialog to request user to enable the location services
            new EnableGpsDialogFragment().show(getSupportFragmentManager(), "enableGpsDialog");
        }
    }


Now add the following method to call the settings interface using the intents (Start another activity)

  // Method to launch Settings interface
    private void enableLocationSettings() {
        Intent settingsIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
        startActivity(settingsIntent);
    }

Now implement the dialog interface for user to choose the option to enable the location services

/**
     * Dialog to prompt users to enable GPS on the device.
     */
    private class EnableGpsDialogFragment extends DialogFragment {

        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            return new AlertDialog.Builder(getActivity())
                    .setTitle(R.string.enable_gps)
                    .setMessage(R.string.enable_gps_dialog)
                    .setPositiveButton(R.string.enable_gps, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            enableLocationSettings();
                        }
                    })
                    .create();
        }
    }

Now request the location provider from location manager

requestUpdatesFromProvider(
                    LocationManager.GPS_PROVIDER, R.string.not_support_gps);

Call this method upon the activity resume by the user.

 @Override
    protected void onResume() {
        super.onResume();
        gpsLocation = requestUpdatesFromProvider();
    }

Now implement the above method to register the provider

private Location requestUpdatesFromProvider(final String provider, final int errorResId) {
        Location location = null;
        if (mLocationManager.isProviderEnabled(provider)) {
            mLocationManager.requestLocationUpdates(provider, TEN_SECONDS, TEN_METERS, listener);
            location = mLocationManager.getLastKnownLocation(provider);
        } else {
            Toast.makeText(this, errorResId, Toast.LENGTH_LONG).show();
        }
        return location;
    }

Now create the location listener with the handler methods

private final LocationListener listener = new LocationListener() {

        @Override
        public void onLocationChanged(Location location) {
            // A new location update is received.  Do something useful with it.  Update the UI with
            // here you will get the location update which you can update in UI
            
        }

        @Override
        public void onProviderDisabled(String provider) {
        }

        @Override
        public void onProviderEnabled(String provider) {
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
        }
    };

Make sure that you will remove the location updates from activity on onStop method

  // Stop receiving location updates whenever the Activity becomes invisible.
    @Override
    protected void onStop() {
        super.onStop();
        mLocationManager.removeUpdates(listener);
    }

Create your first app to get your location and enjoy with Android.










Location Aware Android Apps

Location aware android platform provide a rich set of features you can build in your application which depends on the location information.

Android platform provides the android.location package to create the location aware applications.

The core component of the Android Location package is LocationManager which provides the APIs to determine the device location.Location Manager is the system service provides an internal layer to device to determine the location awareness.

Location Manager instance could not be created directly as this is a system services and you could get an instance of the location manager using the getSystemService API. The API return a handle to the location service.


Once your application has a LocationManager, your application can do following things
  • Query for the list of all LocationProviders for the last known user location.
  • Register/unregister for periodic updates of the user's current location from a location provider (specified either by criteria or name).
  • Register/unregister for a given Intent to be fired if the device comes within a given proximity (specified by radius in meters) of a given lat/long.
There are two types of location providers you can use to determine the user position.

1) GPS Based Provider - GPS based provider is more accurate but limited to outdoors. GPS Providers consumes more power and take a bit of time to determine the user location.

2) Network Location Based Provider- Android Network Location based provider determines the user location based on the mobile tower location, Wi-Fi Signals, so this works for outdoors and indoors.
The response time is fast and the overall battery power consumption is low.

You can use one or multiple providers to determine the location of the user and provide lot of smart informative apps for the user.