Home » Android » core » location » Location Based Services Application

About Ilias Tsagklis

Ilias Tsagklis
Ilias is a software developer turned online entrepreneur. He is co-founder and Executive Editor at Java Code Geeks.

Location Based Services Application

With this example we are going to create a location based services application for Android. Location Based Services are quite popular on mobiles nowadays.

To create a location based services application one should perform the following steps:

These are demonstrated in the code snippet(s) below:

package com.javacodegeeks.android.lbs;

import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class LbsGeocodingActivity extends Activity {

    private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 1; // in Meters
    private static final long MINIMUM_TIME_BETWEEN_UPDATES = 1000; // in Milliseconds

    protected LocationManager locationManager;

    protected Button retrieveLocationButton;

    @Override
    public void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);

  setContentView(R.layout.main);

  retrieveLocationButton = (Button) findViewById(R.id.retrieve_location_button);

  locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

  locationManager.requestLocationUpdates(

    LocationManager.GPS_PROVIDER, 

    MINIMUM_TIME_BETWEEN_UPDATES, 

    MINIMUM_DISTANCE_CHANGE_FOR_UPDATES,

    new MyLocationListener()

  );

    retrieveLocationButton.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

    showCurrentLocation();

}
    });

    }    

    protected void showCurrentLocation() {

  Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

  if (location != null) {

String message = String.format(

  "Current Location n Longitude: %1$s n Latitude: %2$s",

  location.getLongitude(), location.getLatitude()

);

Toast.makeText(LbsGeocodingActivity.this, message,

  Toast.LENGTH_LONG).show();

  }

    }   

    private class MyLocationListener implements LocationListener {

  public void onLocationChanged(Location location) {

String message = String.format(

  "New Location n Longitude: %1$s n Latitude: %2$s",

  location.getLongitude(), location.getLatitude()

);

Toast.makeText(LbsGeocodingActivity.this, message, Toast.LENGTH_LONG).show();

  }

  public void onStatusChanged(String s, int i, Bundle b) {

Toast.makeText(LbsGeocodingActivity.this, "Provider status changed",

  Toast.LENGTH_LONG).show();

  }

  public void onProviderDisabled(String s) {

Toast.makeText(LbsGeocodingActivity.this,

  "Provider disabled by the user. GPS turned off",

  Toast.LENGTH_LONG).show();

  }

  public void onProviderEnabled(String s) {

Toast.makeText(LbsGeocodingActivity.this,

  "Provider enabled by the user. GPS turned on",

  Toast.LENGTH_LONG).show();

  }

    }

}

 
This was an example of an Android location based services application.

Related Article:

(No Ratings Yet)
Start the discussion Views Tweet it!

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

 

Leave a Reply

avatar
  Subscribe  
Notify of