By UMAR-MOBITSOLUTIONS


2010-02-17 09:55:51 8 Comments

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?

30 comments

@dpdearing 2010-10-15 01:04:30

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.

@ottel142 2013-09-12 09:54:36

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?

@tiktak 2014-03-28 19:24:59

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 2014-04-06 16:21:52

@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

@David 2016-03-30 02:47:25

Exactly what I needed.

@nurettin 2016-06-19 05:01:43

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.

@dpdearing 2016-06-22 22:35:56

@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 2016-06-23 05:57:50

@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 +

@marti_ 2018-01-07 17:37:55

It's a great project but it doesn't work for me I can not connect via telnet with my android device

@dpdearing 2018-01-08 20:31:16

@Osgux Feel free to post a description of your problems over on GitHub

@Aseem 2019-11-10 17:51:49

I had the same problem on Android studio 3.5.1 When I clicked on three dots to the right of android emulator and selected location tab, I dont see a send option. I tried to set a location point but that did not change my default cupertino location. Open google maps and it will autolocate you and your cupertino location will change.

@Smile2Life 2019-10-27 19:50:19

Finally with the latest release of Android Studio 4 and his new Emulator update 10/23/2019 it become easier. Start your emulator and go to emulator parameters ... > in "Routes" tab you can choose two points on the map from/to and start a new route with an adjustable playback speed that can go to more than 1000km/h!

enter image description here

@Mahmoud Ayman 2020-06-12 23:34:30

so how to get current lat lng ?

@eden 2017-07-14 09:00:38

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

@Kevan Aghera 2018-04-06 04:54:10

in this solution need a play services in emulator?

@eden 2018-04-06 05:04:20

I don’t think so.

@Kevan Aghera 2018-04-06 05:05:39

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 2018-04-06 05:06:19

Did you sign in?

@eden 2018-04-06 05:32:53

please do so and let us know the outcome

@xarlymg89 2018-06-06 08:31:39

I confirm that there's no need to log in in your Google Account to do this. Cool!

@Nate Whittaker 2019-05-20 20:01:53

I had to restart the emulated OS to get it to recognize the new location.

@Kareem Jeiroudi 2019-07-09 01:17:22

How can let the emulator use my real (laptop's) location, without explicitly specifying the longtime and latitude?

@Aseem 2019-11-10 03:29:51

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

@Premkumar Manipillai 2017-11-07 10:33:50

Open Android studio->Tools menu->Android-> Android Device Monitor->Emulator Tab->Location control-> Set your required latitude and longitude and check your project as per your need

@Premkumar Manipillai 2017-12-11 08:00:07

you may set static location value (Latitude & Longitude) which location you need check directly in coding part?

@Janusz 2010-02-17 10:24:36

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.

@UMAR-MOBITSOLUTIONS 2010-02-17 11:03:30

using geo fix i have also set mock latitude and longitude still getting null location.

@UMAR-MOBITSOLUTIONS 2010-02-17 11:53:18

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 2011-05-23 11:10:50

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.

@Cerin 2012-07-16 06:23:29

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...

@MrBr 2013-04-04 15:36:12

But what about the permissions in the AndroidManifest.xml?

@Bryan Rehbein 2013-08-08 19:00:54

adb emu geo fix 30.219470 -97.745361

@tiktak 2014-03-28 19:21:39

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.)

@Christoph 2014-04-14 21:48:12

Mind the order, lon before lat!

@luv 2016-06-20 09:49:25

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.

@aroth 2016-07-26 03:12:52

The geo fix command is only available if you auth first. Might be worth mentioning...

@ezze 2017-10-16 14:34:14

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.

@Oleg Gryb 2019-05-18 21:14:11

This solution doesn't make much sense after location and GPX files have been added to Emulator's UI. See @eden answer below

@BoD 2017-08-21 13:27:30

I have made a little script similar to one of the previous answers, but using expect instead of python - because it is a bit simpler (expect was invented for this).

#!/usr/bin/expect

set port [lindex $argv 0]
set lon [lindex $argv 1]
set lat [lindex $argv 2]

set timeout 1
spawn telnet localhost $port
expect_after eof { exit 0 }

## interact
expect OK

set fp [open "~/.emulator_console_auth_token" r]
if {[gets $fp line] != -1} {
  send "auth $line\r"
}

send "geo fix $lon $lat\r"
expect OK
send "exit\r"

Exemple usage: sendloc 5554 2.35607 48.8263

@Kyaw Tun 2017-01-26 04:06:22

Just make Alberto Gaona's answer into one line

token=$(cat ~/.emulator_console_auth_token); cat <(echo -e "auth $token \n  geo fix 96.0290791 16.9041016  \n exit") - | nc localhost 5554

5554 is the emulator port number shown in adb devices.

It would have been better if adb emu work.

@Jeremie 2017-03-23 14:36:29

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

@Asaf Pinhassi 2016-10-26 05:20:05

The following solution worked for me - open command line and write:

adb emu geo fix [longtitude] [latitude]

@Muhammad Yousaf Sulahria 2012-11-06 10:43:05

1. Android Studio users.

After running the emulator goto Tools->Android->Android device monitor

Click the Emulator Control Tab change from the location controls group.

2. Eclipse users.

First In Eclipse In Menu Select "Window" then Select "Open Perspective" then Select "DDMS". i.e Window->Open Prespective->DDMS.

You will see on Left Side Devices Panel and on Right Side you will see different tabs. Select "Emulator Control" Tab.

At bottom you will see Location Controls Panel. Select "Manual" Tab.

Enter Longitude and Latitude in Textboxs then Click Send Button. It will send the position to you emulator and the application.

3. Using telnet.

In the run command type this.

telnet localhost 5554

If you are not using windows you can use any telnet client.

After connecting with telnet use the following command to send your position to emulator.

geo fix long lat    
geo fix -121.45356 46.51119 4392

4. Use the browser based Google maps tool

There is a 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

@Eric Barr 2013-11-12 02:34:07

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!

@draekko 2016-10-11 14:06:50

Can't comment yet, so updating @ectomorphs answer here, which when telneting now requires to have an auth token. In linux that's under /home/username/.emulator_console_auth_token

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import telnetlib
from time import sleep
import random

FILE = open('/home/username/.emulator_console_auth_token', 'r')
AUTH_TOKEN = FILE.read()
FILE.close()

HOST = "127.0.0.1"
PORT = 5554
TIMEOUT = 10
LAT_SRC = 52.5243700
LNG_SRC = 13.4105300
LAT_DST = 53.5753200
LNG_DST = 10.0153400
SECONDS = 120

LAT_MAX_STEP = ((max(LAT_DST, LAT_SRC) - min(LAT_DST, LAT_SRC)) / SECONDS) * 2
LNG_MAX_STEP = ((max(LNG_DST, LNG_SRC) - min(LNG_DST, LNG_SRC)) / SECONDS) * 2

DIRECTION_LAT = 1 if LAT_DST - LAT_SRC > 0 else -1
DIRECTION_LNG = 1 if LNG_DST - LNG_SRC > 0 else -1

lat = LAT_SRC
lng = LNG_SRC

tn = telnetlib.Telnet(HOST, PORT, TIMEOUT)
tn.set_debuglevel(9)
tn.read_until("OK", 5)

tn.write("auth {0}\n".format(AUTH_TOKEN))
tn.read_until("OK", 5)

tn.read_until("OK", 5)

tn.write("geo fix {0} {1}\n".format(LNG_SRC, LAT_SRC))
#tn.write("exit\n")

for i in range(SECONDS):
    lat += round(random.uniform(0, LAT_MAX_STEP), 7) * DIRECTION_LAT
    lng += round(random.uniform(0, LNG_MAX_STEP), 7) * DIRECTION_LNG

    #tn.read_until("OK", 5)
    tn.write("geo fix {0} {1}\n".format(lng, lat))
    #tn.write("exit\n")
    sleep(1)

tn.write("geo fix {0} {1}\n".format(LNG_DST, LAT_DST))
tn.write("exit\n")

print tn.read_all()

From a shell script one can set the coorinate like so

#!/usr/bin/env bash
export ATOKEN=`cat ~/.emulator_console_auth_token`
echo -ne "auth $ATOKEN\ngeo fix -99.133333 19.43333 2202\n"  | nc localhost 5554

@ofer 2015-02-23 18:26:07

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.

@Olkunmustafa 2016-10-05 06:49:47

In my case, all fileds are non-editable. Do you have an idea ?

@Justin 2016-11-01 23:53:08

@MustafaOlkun It probably means the emulator itself has those controls on it. Look for a "..." next to the emulator.

@KansaiRobot 2017-02-01 13:44:20

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....

@Dan Zhao 2011-04-12 07:58:57

Using the "geo" command in the emulator console

To send mock location data from the command line:

  1. Launch your application in the Android emulator and open a terminal/console in your SDK's /tools directory.

  2. Connect to the emulator console:

    telnet localhost 5555 (Replace 5555 with whatever port your emulator is running on)

  3. Send the location data: * geo fix to send a fixed geo-location.

    This command accepts a longitude and latitude in decimal degrees, and an optional altitude in meters. For example:

    geo fix -121.45356 46.51119 4392
    

@Jesús Amieiro 2016-07-18 07:28:42

There is a plugin for Android Studio called “Mock Location Plugin”. You can emulate multiple points with this plugin. You can find a detailed manual of use in this link: Android Studio. Simulate multiple GPS points with Mock Location Plugin

@Neil B 2016-06-06 11:43:00

You can use an emulator like genymotion which gives you the flexibility to emulate your present GPS location, etc.

@Neil B 2017-02-03 10:14:43

@KansaiRobot Well I did need to install google play ( stackoverflow.com/questions/17831990/… ) on the emulator

@f.khantsis 2016-05-02 10:30:14

For the new emulator:

http://developer.android.com/tools/devices/emulator.html#extended

Basically, click on the three dots button in the emulator controls (to the right of the emulator) and it will open up a menu which will allow you to control the emulator including location

@user937284 2015-07-15 14:12:48

I wrote a python script to push gps locations to the emulator via telnet. It defines a source and a destination location. There is also a time offset which lets you control how long coordinates will be pushed to the device. One location is beeing pushed once a second.

In the example below the script moves from Berlin to Hamburg in 120 seconds. One step/gps location per second with random distances.

#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
import telnetlib
from time import sleep
import random

HOST = "127.0.0.1"
PORT = 5554
TIMEOUT = 10
LAT_SRC = 52.5243700
LNG_SRC = 13.4105300
LAT_DST = 53.5753200
LNG_DST = 10.0153400
SECONDS = 120

LAT_MAX_STEP = ((max(LAT_DST, LAT_SRC) - min(LAT_DST, LAT_SRC)) / SECONDS) * 2
LNG_MAX_STEP = ((max(LNG_DST, LNG_SRC) - min(LNG_DST, LNG_SRC)) / SECONDS) * 2

DIRECTION_LAT = 1 if LAT_DST - LAT_SRC > 0 else -1
DIRECTION_LNG = 1 if LNG_DST - LNG_SRC > 0 else -1

lat = LAT_SRC
lng = LNG_SRC

tn = telnetlib.Telnet(HOST, PORT, TIMEOUT)
tn.set_debuglevel(9)
tn.read_until("OK", 5)

tn.write("geo fix {0} {1}\n".format(LNG_SRC, LAT_SRC))
#tn.write("exit\n")

for i in range(SECONDS):
    lat += round(random.uniform(0, LAT_MAX_STEP), 7) * DIRECTION_LAT
    lng += round(random.uniform(0, LNG_MAX_STEP), 7) * DIRECTION_LNG

    #tn.read_until("OK", 5)
    tn.write("geo fix {0} {1}\n".format(lng, lat))
    #tn.write("exit\n")
    sleep(1)

tn.write("geo fix {0} {1}\n".format(LNG_DST, LAT_DST))
tn.write("exit\n")

print tn.read_all()

@Alberto Gaona 2015-08-20 21:06:58

Works like a charm! Thanks for sharing.

@Alberto Gaona 2015-08-20 21:29:06

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.

@Durga Swaroop 2016-12-04 14:23:17

Not working. The emulator is listening on the port but its not receiving anything.

@electrobabe 2015-08-06 12:47:54

If you're using Android Studio (1.3):

  • Click on Menu "Tools"
  • "Android"
  • "Android device monitor"
  • click on your current Emulator
  • Tab "Emulator Control"
  • go to "Location Controls" and enter Lat and Lon

@Alberto Gaona 2015-06-03 20:57:49

In Mac, Linux or Cygwin:

echo 'geo fix -99.133333 19.43333 2202' | nc localhost 5554

That will put you in Mexico City. Change your longitude/latitude/altitude accordingly. That should be enough if you are not interested in nmea.

@Alberto Gaona 2015-06-03 21:00:00

You may have to adjust the 5554 to your current emulator listening port.

@Gerhard Wesp 2015-03-01 23:08:38

For a project of my own, I developed an online service which can provide simulated location to the Android emulator.

It uses geo nmea rather than geo fix which allows it to set speed, course, precise time etc. in addition to just lat/lon.

The service requires the nc (netcat) command line utility and nothing else.

http://www.kisstech.ch/nmea/

@Droid Teahouse 2015-01-06 08:35:18

In eclipse:

You may have to drag the DDMS window down. 'Location Controls' is located under 'Telephony Actions' and may be hidden by a normally sized console view ( the bar with console, LogCat etc may be covering it!)

~

@user914425 2015-08-11 00:55:35

Inside DDMS I found under Emulator Control. Tab below Location Controls.

@Swapnil 2014-01-27 16:44:27

If you are using eclipse then using Emulator controller you can manually set latitude and longitude and run your map based app in emulator

@abhishek singh 2013-12-17 08:07:57

In Linux where communication ports are blocked. navigate the terminal to platform-tools folder inside android sdk and fire this command:

./adb -s #{device_name} emu geo fix #{longitude} #{latitude}

@SteveCav 2012-01-23 03:15:33

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

@Steven Magana-Zook 2012-11-17 21:03:19

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.

@Zarokka 2013-12-05 13:52:16

great answer! works with any ide because ddms is part of the sdk (yoursdkfolder/tools/)

@crowmagnumb 2014-07-31 18:13:45

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 2014-09-20 20:24:30

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 2015-04-09 22:12:30

@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?

@Utsav Gupta 2015-10-09 17:15:27

I am unable to do this the locations controls are greyed out

@Maik 2013-10-24 07:25:21

The already mentioned multiple times answer to use the shell command "geo fix..." is the correct answer. But in case you use LocationClient.getLastLocation() to retrieve your data it is worth to mention that it will not work at first. The LocationClient class uses the Google Play Service to retrieve the coordinates. For me this started working after running the emulators maps app once. During the first start you are asked to allow google apps access to your location, which I guess does the trick.

@boiledwater 2013-06-17 09:39:29

I use eclipse plug DDMS function to send GPS. enter image description here

@sachin pareek 2012-12-13 07:00:45

First go in DDMS section in your eclipse Than open emulator Control .... Go To Manual Section set lat and long and then press Send Button

@Dan 2012-12-11 18:13:43

Dalvik Debug Monitor > Select Emulator > Emulator Control Tab > Location Controls.

DDMS -- android_sdk/tools/ddms OR android_sdk/tools/monitor

@dvim 2012-10-17 05:10:37

I was unable to get a GPS fix on the emulator when emulator was running Android image without Google APIs. As soon as I changed the image to contain Google APIs all of the here mentioned ways to get a GPS fix worked.

Make sure you select an image with Google APIs when creating AVD.

@user1911703 2015-07-28 03:09:07

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 2015-07-28 06:57:09

Make sure you have downloaded images with Google APIs via the Android SDK manager.

@ssgao 2012-10-08 04:03:05

If the above solutions don't work. Try this:

Inside your android Manifest.xml, add the following two links OUTSIDE of the application tag, but inside your manifest tag of course

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

Related Questions

Sponsored Content

106 Answered Questions

40 Answered Questions

[SOLVED] How to get screen dimensions as pixels in Android

28 Answered Questions

53 Answered Questions

[SOLVED] How do I pass data between Activities in Android application?

23 Answered Questions

[SOLVED] How do I get the current GPS location programmatically in Android?

52 Answered Questions

11 Answered Questions

[SOLVED] Proper use cases for Android UserManager.isUserAGoat()?

33 Answered Questions

[SOLVED] How do you install an APK file in the Android emulator?

Sponsored Content