Logo Icon Logo
A Crowd-sourced Cookbook on Writing Great Android® Apps
GitHub logo Twitter logo OReilly Book Cover Art

Mocking GPS Coordinates on a Device

Author: Emaad Manzoor -- Published? true -- FormatLanguage: W

Problem:

You need to demonstrate your application, but are scared it might choke trying to triangulate your GPS coordinates. Or you'd like to simulate being in a place you're not.

Solution:

Attach a mock location provider to your LocationManager object, then attach mock coordinates to the mock location provider.

Discussion:

Contents


Write The setMockLocation Method

This function is what you will eventually use in your application to set mock GPS coordinates on the device.

private void setMockLocation(double latitude, double longitude, float accuracy) {
    lm.addTestProvider (LocationManager.GPS_PROVIDER,
                        "requiresNetwork" == "",
                        "requiresSatellite" == "",
                        "requiresCell" == "",
                        "hasMonetaryCost" == "",
                        "supportsAltitude" == "",
                        "supportsSpeed" == "",
                        "supportsBearing" == "",
                         android.location.Criteria.POWER_LOW,
                         android.location.Criteria.ACCURACY_FINE);      

    Location newLocation = new Location(LocationManager.GPS_PROVIDER);

    newLocation.setLatitude(latitude);
    newLocation.setLongitude(longitude);
    newLocation.setAccuracy(accuracy);

    lm.setTestProviderEnabled(LocationManager.GPS_PROVIDER, true);

    lm.setTestProviderStatus(LocationManager.GPS_PROVIDER,
                             LocationProvider.AVAILABLE,
                             null,System.currentTimeMillis());    
  
    lm.setTestProviderLocation(LocationManager.GPS_PROVIDER, newLocation);      

}

What's Happening?

Add a mock provider to the LocationManager lm: The (java.lang.String,%20boolean,%20boolean,%20boolean,%20boolean,%20boolean,%20boolean,%20boolean,%20int,%20int) addTestProvider method of the LocationManager class enables the creation and configuration of a mock location provider.

Create a new location: The Location object allows you to set its (double) Latitude, (double) Longitude and (float) Accuracy.

Activate the mock provider: Set a mock (java.lang.String,%20boolean) enabled value for the LocationManager, set a mock (java.lang.String,%20int,%20android.os.Bundle,%20long) status and then set a mock (java.lang.String,%20android.location.Location) location.

Use The setMockLocation Method

To use the method, you must create a LocationManager object as you usually would, and then invoke the method with your coordinates.

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

lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, new LocationListener() {       
            @Override
            public void onStatusChanged(String provider, int status, Bundle extras) {}
            @Override
            public void onProviderEnabled(String provider) {}
            @Override
            public void onProviderDisabled(String provider) {}
            @Override
            public void onLocationChanged(Location location) {}
});
            
/* Set a mock location for debugging purposes */
setMockLocation(15.387653, 73.872585, 500);

Note: You may need to restart the device after using the mock GPS to re-enable the real device GPS

Example Application Usage

Find Me X: This Android application takes in a search query of the form "churches in Vasco Goa" (place_type in locality city) and returns results augmented with their distance from the user. The location in this application is mocked to be BITS - Pilani Goa Campus, Goa, India.

See Also:

Getting Location Information

Download:

The source code for this project can be downloaded from https://github.com/emaadmanzoor/findmex.
No records found.