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

Comments

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

    thanks!

    ReplyDelete

Post a Comment

Popular posts from this blog

MurMurHash3, an ultra fast hash algorithm for C# / .NET

Quick select algorithm - find the Kth element in a list in linear time

ESP32 - send a push notification from the Arduino ESP32 device to your phone