How to emulate GPS location in the Android Emulator?

465,318

Solution 1

You can connect to the Emulator via Telnet. You then have a Emulator console that lets you enter certain data like geo fixes, network etc.

How to use the console is extensively explained here. To connect to the console open a command line and type

telnet localhost 5554

You then can use the geo command to set a latitude, longitude and if needed altitude on the device that is passed to all programs using the gps location provider. See the link above for further instructions.

The specific command to run in the console is

geo fix <longitude value> <latitude value>

I found this site useful for finding a realistic lat/lng: http://itouchmap.com/latlong.html

If you need more then one coordinate you can use a kml file with a route as well it is a little bit described in this article. I can't find a better source at the moment.

Solution 2

No one here mentioned the built in solution of the emulator itself, so for future visitors, I'd like to share it with visuals.

First, run your Android Emulator and click on the menu button (3 dots) shown below:

emulator with menu button

Then from the left pane, select Location and change the coordinates according to your needs. After pressing Send button, changes will immediately take effect (I recommend you to open up Google Maps for better understanding).

enter image description here

Android Studio Version: 2.3.3

In addition, to make your different locations coming to your application in real time, you can use GPX file. It's very easy to create this file from Google Map direction link:

  1. Go to google map, choose a location, then press "Directions" and enter the second location.
  2. After route is created, copy a link from the browser
  3. Go to this website: https://mapstogpx.com and paste the link to "Let's Go" box
  4. Press the "Let's Go" button and GPX file will be downloaded

Use "Load GPS/KML" button to load the created file to your emulator, choose speed, and press green play button on the bottom. Locations will be sent in real time as shown on the picture below.

enter image description here

Solution 3

I was looking for a better way to set the emulator's GPS coordinates than using geo fix and manually determining the specific latitude and longitude coordinates.

Unable to find anything, I put together a little program that uses GWT and the Google Maps API to launch a browser-based map tool to set the GPS location in the emulator:

android-gps-emulator

Hopefully it can be of use to help others who will undoubtedly stumble across this difficulty/question as well.

Solution 4

If you're using Eclipse, go to Window->Open Perspective->DDMS, then type one in Location Controls and hit Send.

Solution 5

For Android Studio users:

run the emulator,

Then, go to Tools -> Android ->Android device monitor

open the Emulator Control Tab, and use the location controls group.

Share:
465,318
UMAR-MOBITSOLUTIONS
Author by

UMAR-MOBITSOLUTIONS

I am associated with A UK Based Software Development Company! - Web Design and Development Company in UK Our Company provides: Website Development | Mobile Apps Development | Games Development | Ui and UX | Consultancy | API | Digital Marketing My Expertise in: Asp.net (1.1,2.0,3.5,4.0,4.5) Android programming. PHP,WordPress. android,iPhone application development. Unity games development Project management Software quality assurance &amp; testing. Digital Marketing

Updated on December 25, 2021

Comments

  • UMAR-MOBITSOLUTIONS
    UMAR-MOBITSOLUTIONS over 2 years

    I want to get longitude and latitude in Android emulator for testing.

    Can any one guide me how to achieve this?

    How do I set the location of the emulator to a test position?

  • UMAR-MOBITSOLUTIONS
    UMAR-MOBITSOLUTIONS about 14 years
    but my friend where are the longitude and latitude ??
  • UMAR-MOBITSOLUTIONS
    UMAR-MOBITSOLUTIONS about 14 years
    i have tried using this ans sending mock values to emulator control but when i press send button it does not do any thing no message nothing appears to confirm values sent or not. seocnd thing GeoPoint g= myLocation.getMyLocation(); is returning null value.
  • David Hedlund
    David Hedlund about 14 years
    Are you sure that you're using it within runOnFirstFix? Because that's the event that's raised when the device first receives GPS position, so if that's ever raised, getMyLocation should definitely return a value.
  • UMAR-MOBITSOLUTIONS
    UMAR-MOBITSOLUTIONS about 14 years
    using geo fix i have also set mock latitude and longitude still getting null location.
  • UMAR-MOBITSOLUTIONS
    UMAR-MOBITSOLUTIONS about 14 years
    adding many values through telnet now it is working.. i dont know why entering two or three values it was not working.. it works untill i close the emulator when i close the emulator i have to enter mock values again.
  • UMAR-MOBITSOLUTIONS
    UMAR-MOBITSOLUTIONS almost 13 years
    or simple way turn on gps in emulator and go to emulator control give dummy lat,long values and send you will get those values in emulator as a gps location.
  • Admin
    Admin about 12 years
    You don't need to specify a process... Or perhaps DDMS in 2010 requires doing that.
  • Cerin
    Cerin almost 12 years
    This doesn't seem to work. I have a GPS logger running in the emulator, which prints a debug statement in the onLocationChanged handler, and setting the lat/long via geo shows no output...
  • Steven Magana-Zook
    Steven Magana-Zook over 11 years
    This is by far the easiest method i have seen. Make sure you set up you AVD with GPS support, then in the DDMS perspective click on the emulator that is running under Devices, choose the Emulator Control tab on the center-right pane then at the bottom is the Location Controls section SteveCav mentioned (Using Eclipse Juno 4.2 for Java Developers with the latest ADT). My Location Controls panel was even preloaded with location data.
  • MrBr
    MrBr about 11 years
    But what about the permissions in the AndroidManifest.xml?
  • Astra
    Astra over 10 years
    adb emu geo fix 30.219470 -97.745361
  • ottel142
    ottel142 over 10 years
    wow, thank you! setup worked just fine, but I wasn't able to manage a connection to the emulator -> log prints java.net.ConnectionRefused. then i tried telnet localhost 5554 (from terminal) and it worked.. has anyone else tested your emulator successfully on mac?
  • Eric Barr
    Eric Barr over 10 years
    This first option worked for me, with one small change. I couldn't open the DDMS window from Eclipse, I just got a blank gray page. But while I had the emulator running, if I opened up DDMS from a command prompt (C:\Program Files\Android\android-sdk\tools\ddms) then I could see the window you described with Devices on left and "Emulator Control" tab on right. Thanks!
  • Zarokka
    Zarokka over 10 years
    great answer! works with any ide because ddms is part of the sdk (yoursdkfolder/tools/)
  • tiktak
    tiktak about 10 years
    I noticed you need to make your application watch for a position, THEN enter coordinates. It won't take the last position in account if you're registered for "location updates". (That seems logical.)
  • tiktak
    tiktak about 10 years
    I didn't test it, but this looks so awesome I think Google should tell the world about it. Simple ideas are always the best. Why did no one think of this?
  • dpdearing
    dpdearing about 10 years
    @ottel142 - Yes, I am actually running on a mac without that problem (using Chrome on Mavericks). I so see that ConnectionRefused message if I launch the GPS emulator before starting the Android virtual device. At the top of my browser it also says "Error making connection on port 5554 ..." Everything works fine if I click the "Change Emulator Port" button to reconnect
  • Christoph
    Christoph about 10 years
    Mind the order, lon before lat!
  • crowmagnumb
    crowmagnumb almost 10 years
    This, nor the telnet solution above, was working for me. I am trying to get my location like this but it's still null. LocationManager mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); Location location = mlocManager.getLastKnownLocation(LocationManager.NETWORK_PRO‌​VIDER);
  • emersonthis
    emersonthis over 9 years
    When I use "Location Controls" in DDMS I get the following error when trying to send Unable to send command to the emulator. Any idea why it isn't working? I'm using the default Nexus 4 AVD
  • dwanderson
    dwanderson about 9 years
    @zarroka - I'm using the Android SDK and Window doesn't have an Open Perspective (or DDMS) option. What did you mean it works on any SDK?
  • Alberto Gaona
    Alberto Gaona almost 9 years
    You may have to adjust the 5554 to your current emulator listening port.
  • user1911703
    user1911703 almost 9 years
    what do you mean by Image and while creating AVD how would i select an image with Google APIs? there are no such options while create AVD in eclipse.
  • dvim
    dvim almost 9 years
    Make sure you have downloaded images with Google APIs via the Android SDK manager.
  • user914425
    user914425 over 8 years
    Inside DDMS I found under Emulator Control. Tab below Location Controls.
  • Alberto Gaona
    Alberto Gaona over 8 years
    Works like a charm! Thanks for sharing.
  • Alberto Gaona
    Alberto Gaona over 8 years
    Mmmmm. I think there's a slight error: As far as I know the geo fix message gets Longitude and Latitude, in that order: You are sending them the other way around. See answers above.
  • Utsav Gupta
    Utsav Gupta over 8 years
    I am unable to do this the locations controls are greyed out
  • David
    David about 8 years
    Exactly what I needed.
  • nurettin
    nurettin almost 8 years
    This is a good program, but system locality changes the way floats work. On my system it has to be like geo fix 42,42 42,42 42 commas instead of dots.
  • luv
    luv almost 8 years
    I have wrote an app MockGeoFix (source code) that provides the same interface as the emulator so you can telnet to your phone on port 5554 and use "geo fix" or "geo nmea" the same way as with the emulator.
  • dpdearing
    dpdearing almost 8 years
    @nurettin Thanks for the feedback. I will create an Issue for this. If you have a fix, I'd love you to create a PR
  • nurettin
    nurettin almost 8 years
    @dpdearing I would happily test different ways but time is scant. My first try would be to use String.format("geo fix %f %f on GeoFixAsyncCallback.onSuccess instead of concatenating float with with +
  • aroth
    aroth almost 8 years
    The geo fix command is only available if you auth first. Might be worth mentioning...
  • Olkunmustafa
    Olkunmustafa over 7 years
    In my case, all fileds are non-editable. Do you have an idea ?
  • Justin
    Justin over 7 years
    @MustafaOlkun It probably means the emulator itself has those controls on it. Look for a "..." next to the emulator.
  • Durga Swaroop
    Durga Swaroop over 7 years
    Not working. The emulator is listening on the port but its not receiving anything.
  • KansaiRobot
    KansaiRobot about 7 years
    I have the same problem. They are non editable. and yes there is the "...". However when I put anything there and Send, nothing changes on the application....
  • Neil B
    Neil B about 7 years
    @KansaiRobot Well I did need to install google play ( stackoverflow.com/questions/17831990/… ) on the emulator
  • Jeremie
    Jeremie about 7 years
    very useful one liner! in my case I had to remove the spaces between \n: token=$(cat ~/.emulator_console_auth_token); cat <(echo -e "auth $token\ngeo fix 96.0290791 19.9041016\nexit") - | nc localhost 5554
  • ezze
    ezze over 6 years
    A command like geo fix 40 57.5555 0 12 gives me an error: KO: argument '.5555 0 12' is not a number. Though, the following works: geo fix 40 57,5555 0 12. Probably this is some locale related issue.
  • Premkumar Manipillai
    Premkumar Manipillai over 6 years
    you may set static location value (Latitude & Longitude) which location you need check directly in coding part?
  • marti_
    marti_ over 6 years
    It's a great project but it doesn't work for me I can not connect via telnet with my android device
  • dpdearing
    dpdearing over 6 years
    @Osgux Feel free to post a description of your problems over on GitHub
  • Kevan Aghera
    Kevan Aghera about 6 years
    in this solution need a play services in emulator?
  • eden
    eden about 6 years
    I don’t think so.
  • Kevan Aghera
    Kevan Aghera about 6 years
    when i open app in emulator google api client null so not able to load map in emulator. and mobile device there is ok for me.
  • eden
    eden about 6 years
    Did you sign in?
  • eden
    eden about 6 years
    please do so and let us know the outcome
  • xarlymg89
    xarlymg89 almost 6 years
    I confirm that there's no need to log in in your Google Account to do this. Cool!
  • Oleg Gryb
    Oleg Gryb almost 5 years
    This solution doesn't make much sense after location and GPX files have been added to Emulator's UI. See @eden answer below
  • Nate Whittaker
    Nate Whittaker almost 5 years
    I had to restart the emulated OS to get it to recognize the new location.
  • Kareem Jeiroudi
    Kareem Jeiroudi almost 5 years
    How can let the emulator use my real (laptop's) location, without explicitly specifying the longtime and latitude?
  • Aseem
    Aseem over 4 years
    android emulator which came with android studio 3.5.1 does not have any send option in location tab. there is a set location button but it doesnt change the location when read using geolocator package
  • Mahmoud Ayman
    Mahmoud Ayman almost 4 years
    so how to get current lat lng ?
  • anotherCoder
    anotherCoder almost 3 years
    how did you find the value for different cities? I am trying to copy the value from google maps and it not in the range 90 to - 90.
  • Fre Timmerman
    Fre Timmerman over 2 years
    changes will immediately take effect while this is true, i actually needed to open up GoogleMaps once to be able to update the location. (GMaps doesn't need to stay open after this) without opening GMaps once, my app didn't get the updated locations...
  • Josh M.
    Josh M. almost 2 years
    When I load a multipoint GPX/KML the settings window just spins forever. 😔