Programming GPS on Android and calculating distance between two geographic locations

Standard

Download source code of the article.

Programming GPS on Android is fairly simple. In Android to get GPS updates you simply initiate LocationManager class, find a GPS provider and request for updates. To initiate LocationManager manager class you call getSystemService method of the Activity class. LocationManager class provides the location service and periodic updates of the geo location. Once you get the LocationManager class next step is to find out the best GPS provider available on the system which satisfies your criteria.

To get the provider you specify different criteria you need like accuracy, altitude, bearing, etc… For this we use the Criteria class. Following is a sample criterion:

final Criteria criteria = new Criteria();

criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_LOW);

To find out whether a GPS provider best matches the given criteria we use the getBestProvider method of the LocationManager class. This manager will return name of the location provider if it is available. Otherwise the criteria are modified/loosened in the following order:

    Power requirement
    Accuracy
    Bearing
    Speed and
    Altitude

Once we get the provider, we request updates using requestLocationUpdates method. If you only need current location and not need to receive continuous location update you can use getLastKnownLocation method to get the last known location of the device.

To request continuous location we device a variable of LocationListener class and override onLocationChanged method. LocationManager will call this method and pass a Location class whenever there is a change in the device geographic location.

To calculate the distance between two geographic locations, the Location class provides a static method distanceBetween. This method calculates the distance between two geographic locations in meters and returns the result in a float array you pass. You can pass an array of length 1, 2 or 3.

The first element (array[0]) will be the distance in meter. If the array has 2 or more elements the second element (array[1]) will hold the initial bearing value. If the array has 3 or more elements then the third element (array[2]) will hold the final bearing value.

To stop receiving the location updates you call the removeUpdates method of the LocationManager class.

The given sample initializes the LocationManager and requests for periodic location updates. When you press the “Start Measuring” button it calculates the distance between that position and your current position. Hope this will help you to start GPS programming on Android.

One thought on “Programming GPS on Android and calculating distance between two geographic locations

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>