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

In Chapter: Location and Map Applications
Author: Emaad Manzoor ('emaadmanzoor')
Published? true
FormatLanguage: WikiFormat


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.


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



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" == "",

    Location newLocation = new Location(LocationManager.GPS_PROVIDER);


    lm.setTestProviderEnabled(LocationManager.GPS_PROVIDER, true);

    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() {       
            public void onStatusChanged(String provider, int status, Bundle extras) {}
            public void onProviderEnabled(String provider) {}
            public void onProviderDisabled(String provider) {}
            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


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


If you found this recipe useful, why not buy the book and have the whole collection always at hand?"