Wednesday, August 15, 2012

Automatically setting the GPS location in Android emulator

The Android emulator is quite good at working with geolocation, but it’s a bit tedious to set the current latitude and longitude coordinates using Eclipse, especially if you want to test with many different points. I’ve created a simple Python script to do it for me.

In Eclipse the emulator’s GPS coordinates can be set by changing to DDMS view (Window->Open Perspective->DDMS), then setting the location in the Location Controls.

As the emulator is listening on localhost:5554 for a telnet-like connection, you can set the coordinates manually using the simple telnet application:

telnet localhost 5554
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
Android Console: type 'help' for a list of commands
OK
geo fix 151.195733 -33.866745

Watch out for the latitude-longitude order, as it is different in Google Maps: Maps will give you -33.866745,151.195733 so you have to change the order before entering it into the emulator!

However, manually typing into telnet isn’t too convenient either, so I’ve written a simple python script that does it for me. It will keep setting the coordinates every second, emulating the real GPS hardware, which would do the same.

Feel free to modify to suit your needs:

import sys
import telnetlib
import time

HOST = "localhost"

tn = telnetlib.Telnet(HOST,5554)

print "Setting location, press CTRL+C to stop"

while True:
 tn.write("geo fix 151.195733 -33.866745\n") # GOOG HQ SYD
 time.sleep(1)

tn.close()

1 comment:

  1. great tip ... helped me a lot to solve my problem using emulator

    thanks!

    ReplyDelete