Related
I am trying to enter "location controls" for my device in android eclipse. The problem is that the input fields for this is disabled so I can't enter anything.
I HAVE selected my device.
btw. i have tried to enter location controls for a physical device and a virtual device (GenyMotion), but i's the same either way.
I hope anyone have a solution for this
Genymotion provides a shell that enables you to script the modification of sensor states.
gps activate
Activate the GPS sensor (if not already activated)
gps setlatitude
Set latitude (and activate GPS if not allready activated)
gps setlongitude
Set longitude (and activate GPS if not allready activated)
Or you can open the GPS widget that comes with it :
The GPS widget allows real time activation and modifications of the position,accuracy and orientation.
More info here
Try this
open a command shell
conect to the emulator with the command: telnet
localhost
to enter a fixed location execute the command: geo fix <longitude> <latitude> [<altitude>]
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?
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.
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.
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.
If you're using Eclipse, go to Window->Open Perspective->DDMS, then type one in Location Controls and hit Send.
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.
Sorry for the NecroPost, but after following some of the suggestions on this question, I set my location to Alaska. However, my device was still showing to be in Mountain View, California (Google's HQ?). So here's how I did a fix:
1) Go to the location settings:
2) Set your test location. I chose Alaska.
3) Google "My current location" and click on the map circled in the picture.
Note that even though I set location as Alaska, my Virtual Device still thinks it's in Mountain View, California.
4) Click on this location Icon
Your location should now be updated on your device.
You can verify by Googling "My current location" again.
If anyone experienced this same issue, I hope my solution helped you.
Assuming you've got a mapview set up and running:
MapView mapView = (MapView) findViewById(R.id.mapview);
final MyLocationOverlay myLocation = new MyLocationOverlay(this, mapView);
mapView.getOverlays().add(myLocation);
myLocation.enableMyLocation();
myLocation.runOnFirstFix(new Runnable() {
public void run() {
GeoPoint pt = myLocation.getMyLocation();
}
});
You'll need the following permission in your manifest:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
And to send mock coordinates to the emulator from Eclipse, Go to the "Window" menu, select "Show View" > "Other" > "Emulator control", and you can send coordinates from the emulator control pane that appears.
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
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
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!
The following solution worked for me - open command line and write:
adb emu geo fix [longtitude] [latitude]
Using the "geo" command in the emulator console
To send mock location data from the command line:
Launch your application in the Android emulator and open a terminal/console in your SDK's /tools directory.
Connect to the emulator console:
telnet localhost 5555 (Replace 5555 with whatever port your emulator is running on)
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
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()
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}
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.
I use eclipse plug DDMS function to send GPS.
See Obtaining User Location
Look under Providing Mock Location Data. You will find the solution for it.
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
I was trying to set the geo fix through adb for many points and could not get my app to see any GPS data. But when I tried opening DDMS, selecting my app's process and sending coordinates through the emulator control tab it worked right away.
Dalvik Debug Monitor > Select Emulator > Emulator Control Tab > Location Controls.
DDMS -- android_sdk/tools/ddms OR android_sdk/tools/monitor
If you are using eclipse then using Emulator controller you can manually set latitude and longitude and run your map based app in emulator
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
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.
Go to Extended controls in emulate. After You can then set the location for the emulate by searching or dragging the map to the location you want to set.
Finally Click on SET LOCATION button to save.
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>
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.
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.
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/
You can use an emulator like genymotion which gives you the flexibility to emulate your present GPS location, etc.
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
Is it possible to show our current posotion on Android emulator?
If yes how?
Thank You.
You need to set the latitude and longitude in case of emulator because emulator do not have gps detection hardrware to get current location.
telnet localhost 5554
geo fix <longitude value> <latitude value> For ex: geo fix 68.54546 37.5117579
You can fake GPS location data on emulator using DDMS which is explained here: http://developer.android.com/tools/debugging/ddms.html#ops-location and here: http://developer.android.com/guide/topics/location/strategies.html#MockData.
You can interact with an emulator instance through its shell.
Start emulator.
From terminal run telnet localhost 5554, you are now in emulator shell.
Type help,it will show available commands , like faking a call, sms, battery etc.
To know more about a command type help <command name>.
Type help geo, it will print available location commands.
Try geo fix 1 1 .
Try more with other latitude and longitude values.
Using the the Geo Fix command to set the virtual device's location in the Android Emulator works properly for the Maps application. However, when I attempt to view my current location in Google Maps in the virtual device's browser, I receive the "Your location could not be determined" error. Does the Geo Fix command not support the W3C geolocation standard or am I missing something?
(1) First, open the emulator, then run cmd, type telnet localhost 5554
this will appear
Android Console: type 'help' for a list of commands
OK.
(2) geo fix Latitude Longitude
(3) Location loc = LocationManager.getLastKnownLocation("gps");
if it doesn't help,try this:
open eclipse, windows -> open perspective -> DDMS -> Emulator control -> Manual to manually set the latitude and longitude, and press the send button.
good luck~
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?
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.
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.
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.
If you're using Eclipse, go to Window->Open Perspective->DDMS, then type one in Location Controls and hit Send.
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.
Sorry for the NecroPost, but after following some of the suggestions on this question, I set my location to Alaska. However, my device was still showing to be in Mountain View, California (Google's HQ?). So here's how I did a fix:
1) Go to the location settings:
2) Set your test location. I chose Alaska.
3) Google "My current location" and click on the map circled in the picture.
Note that even though I set location as Alaska, my Virtual Device still thinks it's in Mountain View, California.
4) Click on this location Icon
Your location should now be updated on your device.
You can verify by Googling "My current location" again.
If anyone experienced this same issue, I hope my solution helped you.
Assuming you've got a mapview set up and running:
MapView mapView = (MapView) findViewById(R.id.mapview);
final MyLocationOverlay myLocation = new MyLocationOverlay(this, mapView);
mapView.getOverlays().add(myLocation);
myLocation.enableMyLocation();
myLocation.runOnFirstFix(new Runnable() {
public void run() {
GeoPoint pt = myLocation.getMyLocation();
}
});
You'll need the following permission in your manifest:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
And to send mock coordinates to the emulator from Eclipse, Go to the "Window" menu, select "Show View" > "Other" > "Emulator control", and you can send coordinates from the emulator control pane that appears.
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
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
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!
The following solution worked for me - open command line and write:
adb emu geo fix [longtitude] [latitude]
Using the "geo" command in the emulator console
To send mock location data from the command line:
Launch your application in the Android emulator and open a terminal/console in your SDK's /tools directory.
Connect to the emulator console:
telnet localhost 5555 (Replace 5555 with whatever port your emulator is running on)
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
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()
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}
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.
I use eclipse plug DDMS function to send GPS.
See Obtaining User Location
Look under Providing Mock Location Data. You will find the solution for it.
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
I was trying to set the geo fix through adb for many points and could not get my app to see any GPS data. But when I tried opening DDMS, selecting my app's process and sending coordinates through the emulator control tab it worked right away.
Dalvik Debug Monitor > Select Emulator > Emulator Control Tab > Location Controls.
DDMS -- android_sdk/tools/ddms OR android_sdk/tools/monitor
If you are using eclipse then using Emulator controller you can manually set latitude and longitude and run your map based app in emulator
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
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.
Go to Extended controls in emulate. After You can then set the location for the emulate by searching or dragging the map to the location you want to set.
Finally Click on SET LOCATION button to save.
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>
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.
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.
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/
You can use an emulator like genymotion which gives you the flexibility to emulate your present GPS location, etc.
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