How to emulate GPS location in the Android Emulator?
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:
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).
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:
- Go to google map, choose a location, then press "Directions" and enter the second location.
- After route is created, copy a link from the browser
- Go to this website: https://mapstogpx.com and paste the link to "Let's Go" box
- 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.
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:
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.
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 & testing. Digital Marketing
Updated on December 25, 2021Comments
-
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 about 14 yearsbut my friend where are the longitude and latitude ??
-
UMAR-MOBITSOLUTIONS about 14 yearsi 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 about 14 yearsAre 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 about 14 yearsusing geo fix i have also set mock latitude and longitude still getting null location.
-
UMAR-MOBITSOLUTIONS about 14 yearsadding 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 almost 13 yearsor 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 about 12 yearsYou don't need to specify a process... Or perhaps DDMS in 2010 requires doing that.
-
Cerin almost 12 yearsThis 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 over 11 yearsThis 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 about 11 yearsBut what about the permissions in the AndroidManifest.xml?
-
Astra over 10 yearsadb emu geo fix 30.219470 -97.745361
-
ottel142 over 10 yearswow, 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 over 10 yearsThis 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 over 10 yearsgreat answer! works with any ide because ddms is part of the sdk (yoursdkfolder/tools/)
-
tiktak about 10 yearsI 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 about 10 yearsI 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 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 about 10 yearsMind the order, lon before lat!
-
crowmagnumb almost 10 yearsThis, 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_PROVIDER);
-
emersonthis over 9 yearsWhen 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 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 almost 9 yearsYou may have to adjust the 5554 to your current emulator listening port.
-
user1911703 almost 9 yearswhat 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 almost 9 yearsMake sure you have downloaded images with Google APIs via the Android SDK manager.
-
user914425 over 8 yearsInside DDMS I found under Emulator Control. Tab below Location Controls.
-
Alberto Gaona over 8 yearsWorks like a charm! Thanks for sharing.
-
Alberto Gaona over 8 yearsMmmmm. 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 over 8 yearsI am unable to do this the locations controls are greyed out
-
David about 8 yearsExactly what I needed.
-
nurettin almost 8 yearsThis 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 almost 8 yearsI 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 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 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
onGeoFixAsyncCallback.onSuccess
instead of concatenating float with with+
-
aroth almost 8 yearsThe
geo fix
command is only available if youauth
first. Might be worth mentioning... -
Olkunmustafa over 7 yearsIn my case, all fileds are non-editable. Do you have an idea ?
-
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 over 7 yearsNot working. The emulator is listening on the port but its not receiving anything.
-
KansaiRobot about 7 yearsI 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 about 7 years@KansaiRobot Well I did need to install google play ( stackoverflow.com/questions/17831990/… ) on the emulator
-
Jeremie about 7 yearsvery 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 over 6 yearsA 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 over 6 yearsyou may set static location value (Latitude & Longitude) which location you need check directly in coding part?
-
marti_ over 6 yearsIt's a great project but it doesn't work for me I can not connect via telnet with my android device
-
dpdearing over 6 years@Osgux Feel free to post a description of your problems over on GitHub
-
Kevan Aghera about 6 yearsin this solution need a play services in emulator?
-
eden about 6 yearsI don’t think so.
-
Kevan Aghera about 6 yearswhen 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 about 6 yearsDid you sign in?
-
eden about 6 yearsplease do so and let us know the outcome
-
xarlymg89 almost 6 yearsI confirm that there's no need to log in in your Google Account to do this. Cool!
-
Oleg Gryb almost 5 yearsThis solution doesn't make much sense after location and GPX files have been added to Emulator's UI. See @eden answer below
-
Nate Whittaker almost 5 yearsI had to restart the emulated OS to get it to recognize the new location.
-
Kareem Jeiroudi almost 5 yearsHow can let the emulator use my real (laptop's) location, without explicitly specifying the longtime and latitude?
-
Aseem over 4 yearsandroid 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 almost 4 yearsso how to get current lat lng ?
-
anotherCoder almost 3 yearshow 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 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. almost 2 yearsWhen I load a multipoint GPX/KML the settings window just spins forever. 😔