154

How can I mock my location on a physical device (Nexus One)?

I know you can do this with the emulator in the Emulator Control panel, but this doesn't work for a physical device.

5
  • For desktop computers you can create/install drivers that emulate a GPS device/serial port device for the purpose of doing this kind of testing. Perhaps you could create or find something similar for android.
    – AaronLS
    Commented Mar 31, 2010 at 21:14
  • 6
    There are several applications in Market which can emulate GPS location. For example "Location spoofer".
    – Vlad
    Commented Dec 3, 2010 at 7:32
  • There is a lot of apps in the store that do this.. You can try this - play.google.com/store/apps/… or this - play.google.com/store/apps/…
    – Yehuda
    Commented Aug 6, 2015 at 22:19
  • 1
    There is also an app in the store that supports "geo fix" and "geo nmea" commands just like the emulator! play.google.com/store/apps/…
    – luv
    Commented Jul 30, 2016 at 19:27
  • @Iuv more over it is open source github.com/luv/mockgeofix
    – Vlad
    Commented Nov 5, 2019 at 8:31

21 Answers 21

168
+275

It seems the only way to do is to use a mock location provider.

You have to enable mock locations in the development panel in your settings and add

   <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" /> 

to your manifest.

Now you can go in your code and create your own mock location provider and set the location of this provider.

5
  • 85
    There is an application on the market called "Fake GPS". If you enable Mock Locations on your device, it offers a nice GUI for setting your location. I realise this isn't a do-it-yourself solution, but certainly a worthy solution for quick testing.
    – Tim Green
    Commented Aug 30, 2011 at 22:16
  • 2
    this might be the link - same date, and relevant content. Commented Apr 18, 2012 at 22:40
  • Actually the "fake gps" application is not the best because it can only set locations and randomly move around. Their new version can route but it only iterates over way points so huge jumps in the location. Janusz answer is indeed correct that you need to create your own mock provider. We recently did that and if anyone needs it or if this question still needs help, I'll post the code.
    – vkinra
    Commented Jun 11, 2013 at 15:50
  • 7
    The statement "go in your code and create your own mock location provider" should be explained in more detail.
    – Dims
    Commented Feb 16, 2017 at 10:35
  • Now, Studio Android 2.3.2 appear msg :"Mock locations should only be requested in a test or debug-specific manifest file (typically src/debug/AndroidManifest.xml)" --> Anyone, how to build it with release mode? Commented Jun 8, 2017 at 0:46
102

If you use this phone only in development lab, there is a chance you can solder away GPS chip and feed serial port directly with NMEA sequences from other device.

4
  • 12
    If you have trouble figuring out how to generate realistic NMEA sequences into your unsoldered device, there's an awesome option: solder there an actual external GPS device and move the whole development lab on an actual track, on top of a team of runners/bikers/cars/planes/whatever depending on the speed you want to simulate. Don't forget to open a hole in the roof of the development lab so that the device can receive radio signals from satellites. You're welcome. ;-) Commented Jun 8, 2016 at 8:42
  • @StéphaneGourichon better yet, solder them all together into one unholy ball of metal and use it as an antenna for your phone's gps. It is bound to pick up some gps signals.
    – nurettin
    Commented Jun 19, 2016 at 6:27
  • 2
    Such a shame this is often the same IC as the GSM/HSPA/EDGE/LTE/CDMA. A reason to hate SOC design ;)
    – ewanm89
    Commented Jul 13, 2016 at 21:59
  • 4
    If you are not able to unsolder the chip, you could always build yourself a drone squadron equipped with radios tuned at the same frequency as the GPS satellites, emitting with high power in order to jam the real signals, then trivially calculate the time offset to fake depending on the desired mocked location. Mind that every drone has to emit in the same 3 channels as your closest sateillites, and that those channels change with your position. Also,be aware that airspace restrictions may apply in your area.
    – rupps
    Commented Jul 21, 2016 at 0:16
32

I wish I had my cable handy. I know you can telnet to the emulator to change its location

$ telnet localhost 5554
Android Console: type 'help' for a list of commands
OK
geo fix -82.411629 28.054553
OK

I cannot remember if you can telnet to your device, but I think you can. I hope this helps.

You'll need adb (android debugging bridge) for this (CLI).

8
  • you get a permission denied to everything you send as a command to the console of your device. Maybe it is possible with a terminal application but I think that requires root access
    – Janusz
    Commented Apr 6, 2010 at 18:49
  • Just so we're on the same page: you're not talking about "adb shell" are you? What I'm talking about is different, this is something setup that allows you to set android-specific parameters. I know, for example, you can setup port forwarding (i.e. localhost:1234 goes to phone:1234) without root access, so you can setup a SOCKS proxy and tunnel via a cable w/o root.
    – Tim Green
    Commented Apr 6, 2010 at 18:58
  • You can use adb forward tcp:6000 tcp:23 to forwward port 6000 on your machine to port 23 on the device but I wasn't able to connect even if the port is shown as open. Using a telnet client on the device and connecting to localhost also fails
    – Janusz
    Commented Apr 6, 2010 at 19:28
  • 17
    This works fine for the emulator but doesn't work for an actual device. So it doesn't answer OP's question at all :( Commented Nov 28, 2013 at 0:25
  • 4
    @TimGreen Your solution is only for emulator. The question was about real device. Commented Jan 22, 2015 at 21:23
29

You can use the Location Services permission to mock location...

"android.permission.ACCESS_MOCK_LOCATION"

and then in your java code,

// Set location by setting the latitude, longitude and may be the altitude...
String[] MockLoc = str.split(",");
Location location = new Location(mocLocationProvider);            
Double lat = Double.valueOf(MockLoc[0]);
location.setLatitude(lat);
Double longi = Double.valueOf(MockLoc[1]);
location.setLongitude(longi);
Double alti = Double.valueOf(MockLoc[2]);
location.setAltitude(alti);
2
  • 10
    what is in the value of mocLocationProvider?
    – Victor
    Commented Oct 9, 2015 at 21:32
  • 2
    e.g. LocationManager.GPS_PROVIDER
    – enigma969
    Commented Nov 12, 2016 at 11:27
25

I've had success with the following code. Albeit it got me a single lock for some reason (even if I've tried different LatLng pairs), it worked for me. mLocationManager is a LocationManager which is hooked up to a LocationListener:

private void getMockLocation()
{
    mLocationManager.removeTestProvider(LocationManager.GPS_PROVIDER);
    mLocationManager.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 (/* TODO: Set Some Lat */);
    newLocation.setLongitude(/* TODO: Set Some Lng */);

    newLocation.setAccuracy(500);

    mLocationManager.setTestProviderEnabled
    (
      LocationManager.GPS_PROVIDER, 
      true
    );

    mLocationManager.setTestProviderStatus
    (
       LocationManager.GPS_PROVIDER,
       LocationProvider.AVAILABLE,
       null,
       System.currentTimeMillis()
    );      

    mLocationManager.setTestProviderLocation
    (
      LocationManager.GPS_PROVIDER, 
      newLocation
    );      
}
4
  • As far as I remember, setTestProviderStatus is used here to notify/post to/schedule an event with the LocationManager that the GPS provider has found a fix "now" so that processing of the said event can occur.
    – Dr1Ku
    Commented Apr 10, 2015 at 21:36
  • What is the meaning of "mLocationManager is a LocationManager which is hooked up to a LocationListener"? how can I do this? Thank you..
    – Yehuda
    Commented Jul 25, 2015 at 18:36
  • Here's one article explaining how to check for a location change using the LocationManager service and a LocationListener: blog.doityourselfandroid.com/2010/12/25/…
    – Dr1Ku
    Commented Jul 28, 2015 at 6:49
  • 2
    plus 1 for the way you represented the true(s) and false(s) in the method addTestProvider
    – A.Alqadomi
    Commented Dec 19, 2016 at 9:27
16

What Dr1Ku posted works. Used the code today but needed to add more locs. So here are some improvements:

Optional: Instead of using the LocationManager.GPS_PROVIDER String, you might want to define your own constat PROVIDER_NAME and use it. When registering for location updates, pick a provider via criteria instead of directly specifying it in as a string.

First: Instead of calling removeTestProvider, first check if there is a provider to be removed (to avoid IllegalArgumentException):

if (mLocationManager.getProvider(PROVIDER_NAME) != null) {
  mLocationManager.removeTestProvider(PROVIDER_NAME);
}

Second: To publish more than one location, you have to set the time for the location:

newLocation.setTime(System.currentTimeMillis());
...
mLocationManager.setTestProviderLocation(PROVIDER_NAME, newLocation);

There also seems to be a google Test that uses MockLocationProviders: http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/1.5_r4/android/location/LocationManagerProximityTest.java

Another good working example can be found at: http://pedroassuncao.com/blog/2009/11/12/android-location-provider-mock/

Another good article is: http://ballardhack.wordpress.com/2010/09/23/location-gps-and-automated-testing-on-android/#comment-1358 You'll also find some code that actually works for me on the emulator.

4
  • Cool suggestion about defining your own Provider, the thing with using the GPS_PROVIDER is that you (apparently) cannot get rid of the mock provider (more exactly, you cannot switch to normal GPS) until rebooting. Awesome post as well, I'll check out the references !
    – Dr1Ku
    Commented Jan 26, 2011 at 16:55
  • @Dr1KU Is it still the case that you cannot switch back to normal GPS? Is that specified somewhere?
    – Kaz Dragon
    Commented Nov 21, 2013 at 10:24
  • @KazDragon That was the case back then. From what I can recall, after turning the Mock Provider on, the normal GPS wouldn't work, I couldn't get a fix (I was testing directly on a device). This may not be the case since a lot has happened in Android since.
    – Dr1Ku
    Commented Nov 22, 2013 at 10:10
  • @Dr1Ku Ah, well, I'm doing something very similar now and receiving similar results to those you described. I was hoping to know whether this was a restriction built into the platform or that I was just missing a function call somewhere. I should probably make a question out of this.
    – Kaz Dragon
    Commented Nov 22, 2013 at 10:13
9

There are apps available in the Android Market that allow you to specify a "Mock GPS Location" for your device.

I searched https://market.android.com and found an app called "My Fake Location" that works for me.

The Mock GPS Provider mentioned by Paul above (at http://www.cowlumbus.nl/forum/MockGpsProvider.zip) is another example that includes source code -- although I wasn't able to install the provided APK (it says Failure [INSTALL_FAILED_OLDER_SDK] and may just need a recompile)

In order to use GPS mock locations you need to enable it in your device settings. Go to Settings -> Applications -> Development and check "Allow mock locations"

You can then use an app like the ones described above to set GPS coordinates and Google maps and other apps will use the mock GPS location you specify.

1
  • Fake GPS is simpler. I'm just wondering why they are asking for caller id in the app permission :S Commented Apr 23, 2013 at 18:54
8

I've created a simple Handler simulating a moving position from an initial position.

Start it in your connection callback :

private final GoogleApiClient.ConnectionCallbacks mConnectionCallbacks = new GoogleApiClient.ConnectionCallbacks() {
    @Override
    public void onConnected(Bundle bundle) {
        if (BuildConfig.USE_MOCK_LOCATION) {
            LocationServices.FusedLocationApi.setMockMode(mGoogleApiClient, true);
            new MockLocationMovingHandler(mGoogleApiClient).start(48.873399, 2.342911);
        }
    }

    @Override
    public void onConnectionSuspended(int i) {

    }
};

The Handler class :

   private static class MockLocationMovingHandler extends Handler {

    private final static int SET_MOCK_LOCATION = 0x000001;
    private final static double STEP_LATITUDE =  -0.00005;
    private final static double STEP_LONGITUDE = 0.00002;
    private final static long FREQUENCY_MS = 1000;
    private GoogleApiClient mGoogleApiClient;
    private double mLatitude;
    private double mLongitude;

    public MockLocationMovingHandler(final GoogleApiClient googleApiClient) {
        super(Looper.getMainLooper());
        mGoogleApiClient = googleApiClient;
    }

    public void start(final double initLatitude, final double initLongitude) {
        if (hasMessages(SET_MOCK_LOCATION)) {
            removeMessages(SET_MOCK_LOCATION);
        }
        mLatitude = initLatitude;
        mLongitude = initLongitude;
        sendEmptyMessage(SET_MOCK_LOCATION);
    }

    public void stop() {
        if (hasMessages(SET_MOCK_LOCATION)) {
            removeMessages(SET_MOCK_LOCATION);
        }
    }

    @Override
    public void handleMessage(Message message) {
        switch (message.what) {
            case SET_MOCK_LOCATION:
                Location location = new Location("network");
                location.setLatitude(mLatitude);
                location.setLongitude(mLongitude);
                location.setTime(System.currentTimeMillis());
                location.setAccuracy(3.0f);
                location.setElapsedRealtimeNanos(System.nanoTime());
                LocationServices.FusedLocationApi.setMockLocation(mGoogleApiClient, location);

                mLatitude += STEP_LATITUDE;
                mLongitude += STEP_LONGITUDE;
                sendEmptyMessageDelayed(SET_MOCK_LOCATION, FREQUENCY_MS);
                break;
        }
    }
}

Hope it can help..

8

This worked for me (Android Studio):

Disable GPS and WiFi tracking on the phone. On Android 5.1.1 and below, select "enable mock locations" in Developer Options.

Make a copy of your manifest in the src/debug directory. Add the following to it (outside of the "application" tag):

uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"

Set up a map Fragment called "map". Include the following code in onCreate():

lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
ll = new MyLocationListener();
if (lm.getProvider("Test") == null) {
    lm.addTestProvider("Test", false, false, false, false, false, false, false, 0, 1);
}
lm.setTestProviderEnabled("Test", true);
lm.requestLocationUpdates("Test", 0, 0, ll);

map.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
    @Override
    public void onMapClick(LatLng l) {
        Location loc = new Location("Test");
        loc.setLatitude(l.latitude);
        loc.setLongitude(l.longitude);
        loc.setAltitude(0); 
        loc.setAccuracy(10f);
        loc.setElapsedRealtimeNanos(System.nanoTime());
        loc.setTime(System.currentTimeMillis()); 
        lm.setTestProviderLocation("Test", loc);
    }
};

Note that you may have to temporarily increase "minSdkVersion" in your module gradle file to 17 in order to use the "setElapsedRealtimeNanos" method.

Include the following code inside the main activity class:

private class MyLocationListener implements LocationListener {
    @Override
    public void onLocationChanged(Location location) {
        // do whatever you want, scroll the map, etc.
    }
}

Run your app with AS. On Android 6.0 and above you will get a security exception. Now go to Developer Options in Settings and select "Select mock location app". Select your app from the list.

Now when you tap on the map, onLocationChanged() will fire with the coordinates of your tap.

I just figured this out so now I don't have to tramp around the neighborhood with phones in hand.

1
  • i think it work on android 6 if you put this code in service
    – Hamza
    Commented Aug 15, 2017 at 22:26
7

Add to your manifest

<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"
    tools:ignore="MockLocation,ProtectedPermissions" />

Mock location function

void setMockLocation() {
    LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
    locationManager.addTestProvider(LocationManager.GPS_PROVIDER, false, false,
            false, false, true, true, true, 0, 5);
    locationManager.setTestProviderEnabled(LocationManager.GPS_PROVIDER, true);

    Location mockLocation = new Location(LocationManager.GPS_PROVIDER);
    mockLocation.setLatitude(-33.852);  // Sydney
    mockLocation.setLongitude(151.211);
    mockLocation.setAltitude(10);
    mockLocation.setAccuracy(5);
    mockLocation.setTime(System.currentTimeMillis());
    mockLocation.setElapsedRealtimeNanos(System.nanoTime());
    locationManager.setTestProviderLocation(LocationManager.GPS_PROVIDER, mockLocation);
}

You'll also need to enable mock locations in your devices developer settings. If that's not available, set the "mock location application" to your application once the above has been implemented.

0
6

The solution mentioned by icyerasor and provided by Pedro at http://pedroassuncao.com/blog/2009/11/12/android-location-provider-mock/ worked very well for me. However, it does not offer support for properly starting, stopping and restarting the mock GPS provider.

I have changed his code a bit and rewritten the class to be an AsyncTask instead of a Thread. This allows us to communicate with the UI Thread, so we can restart the provider at the point where we were when we stopped it. This comes in handy when the screen orientation changes.

The code, along with a sample project for Eclipse, can be found on GitHub: https://github.com/paulhoux/Android-MockProviderGPS

All credit should go to Pedro for doing most of the hard work.

5

The above solutions did not work for me because I was testing on an Android device with the latest Google Play Services version which utilizes the FusedLocationProviderClient. After setting the mock location permission in the app manifest and the app as the specified mock location app in the developer settings (as mentioned in the previous answers), I then added the Kotlin code below which successfully mocked the location.

locationProvider = FusedLocationProviderClient(context)
locationProvider.setMockMode(true)

val loc = Location(providerName)
val mockLocation = Location(providerName) // a string
mockLocation.latitude = latitude  // double
mockLocation.longitude = longitude
mockLocation.altitude = loc.altitude
mockLocation.time = System.currentTimeMillis()
mockLocation.accuracy = 1f
mockLocation.elapsedRealtimeNanos = SystemClock.elapsedRealtimeNanos()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    mockLocation.bearingAccuracyDegrees = 0.1f
    mockLocation.verticalAccuracyMeters = 0.1f
    mockLocation.speedAccuracyMetersPerSecond = 0.01f
}
//        locationManager.setTestProviderLocation(providerName, mockLocation)
locationProvider.setMockLocation(mockLocation)
2
  • Where did you place this code? I just placed it in my MainActivity's onCreate but it didn't work... I added all the needed permissions and selected my app as mock location in Settings.
    – user19729496
    Commented Jan 20, 2023 at 19:30
  • Does the code above mock the location for other apps such as Google Maps or Uber? I managed to mock the location in a similar fashion, however as soon as I change to Google Maps, my location seems to be back to the real one, as if the mock location has never been set.
    – superpav
    Commented Jun 4 at 15:26
3

Fake GPS app from google play did the trick for me. Just make sure you read all the directions in the app description. You have to disable other location services as well as start your app after you enable "Fake GPS". Worked great for what I needed.

Here is the link to the app on GooglePlay: Fake GPS

2

The Google tutorial for doing this can be found here, it provides code examples and explains the process.

http://developer.android.com/training/location/location-testing.html#SendMockLocations

2

I wrote an App that runs a WebServer (REST-Like) on your Android Phone, so you can set the GPS position remotely. The website provides an Map on which you can click to set a new position, or use the "wasd" keys to move in any direction. The app was a quick solution so there is nearly no UI nor Documentation, but the implementation is straight forward and you can look everything up in the (only four) classes.

Project repository: https://github.com/juliusmh/RemoteGeoFix

1

If your device is plugged into your computer and your trying to changed send GPS cords Via the Emulator control, it will not work.
This is an EMULATOR control for a reason.
Just set it up to update you on GPS change.

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

    ll = new LocationListener() {        
        public void onLocationChanged(Location location) {  
          // Called when a new location is found by the network location provider.  
            onGPSLocationChanged(location); 
        }

        public void onStatusChanged(String provider, int status, Bundle extras) {       
            bigInfo.setText("Changed "+ status);  
        }

        public void onProviderEnabled(String provider) {
            bigInfo.setText("Enabled "+ provider);
        }

        public void onProviderDisabled(String provider) {
            bigInfo.setText("Disabled "+ provider);
        }
      };

When GPS is updated rewrite the following method to do what you want it to;

public void onGPSLocationChanged(Location location){  
if(location != null){  
    double pLong = location.getLongitude();  
    double pLat = location.getLatitude();  
    textLat.setText(Double.toString(pLat));  
    textLong.setText(Double.toString(pLong));  
    if(autoSave){  
        saveGPS();  
        }
    }
}

Dont forget to put these in the manifest
android.permission.ACCESS_FINE_LOCATION
android.permission.ACCESS_MOCK_LOCATION

1

Install Fake GPS app https://play.google.com/store/apps/details?id=com.incorporateapps.fakegps.fre&hl=en

Developer options -> Select mock location app(It's mean, Fake location app selected).

Fake GPS app:

Double tab on the map to add -> click the play button -> Show the toast "Fake location stopped"

finally check with google map apps.

0

I wonder if you need the elaborate Mock Location setup. In my case once I got a fix location I was calling a function to do something with that new location. In a timer create a mock location. And call the function with that location instead. Knowing all along that in a short while GPS would come up with a real current location. Which is OK. If you have the update time set sufficiently long.

0

Maybe it's not 'programmer' approach, but if you want save your time and get working solution instant try one of the apps which are dedicated to mock location available in Google Play:

Fake GPS Location Spoofer

Mock Locations

Fake GPS location

3
  • 1
    Did you try them? Did they work as requested in the question?
    – arekolek
    Commented Feb 2, 2017 at 17:56
  • Any preference? Or does one need all of them? :P
    – arekolek
    Commented Mar 7, 2018 at 13:23
  • Fake GPS Location Spoofer is good enough, but Mock Locations allows to set a route which is a nice feature.
    – klimat
    Commented Mar 7, 2018 at 13:29
0

Make use of the very convenient and free interactive location simulator for Android phones and tablets (named CATLES). It mocks the GPS-location on a system-wide level (even within the Google Maps or Facebook apps) and it works on physical as well as virtual devices:

Website: http://ubicom.snet.tu-berlin.de/catles/index.html

Video: https://www.youtube.com/watch?v=0WSwH5gK7yg

0

Use this permission in manifest file

<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION">

android studio will recommend that "Mock location should only be requested in a test or debug-specific manifest file (typically src/debug/AndroidManifest.xml)" just disable the inspection

Now make sure you have checked the "Allow mock locations" in developer setting of your phone

Use LocationManager

locationManager.addTestProvider(mocLocationProvider, false, false,
                false, false, true, true, true, 0, 5);
locationManager.setTestProviderEnabled(mocLocationProvider, true);

Now set the location wherever you want

Location mockLocation = new Location(mocLocationProvider); 
mockLocation.setLatitude(lat); 
mockLocation.setLongitude(lng); 
mockLocation.setAltitude(alt); 
mockLocation.setTime(System.currentTimeMillis()); 
locationManager.setTestProviderLocation( mocLocationProvider, mockLocation); 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.