Providing GPS Coordinates Via the Eclipse Android Simulator
In most Android devices, your location-based or map applications can determine the current geolocation using either GPS (Global Positioning System), cell tower triangulation, or wifi networks. With regards to GPS, Eclipse provides a tool to simulate coordinates in a way that mimics real-life usage. It's called the GPS Emulator Control. The tool features a Dalvik Debug Monitor Server (DDMS) that can be utilized to send your device or emulator a mock location, which is useful for testing different aspects of your application's location specific features without physically moving. In today's article, we're going to learn how to utilize the Emulator Control to provide GPS coordinates to your Android device or the built-in simulator using several different methods.
Geo Positioning in Eclipse
The android.location package contains the classes required to determine the current geo position. To use them, all you need to do is integrate and import the GPS modules in your application:
The DDMS debugging tool comes fully integrated into the Android Development Tools (ADT) plugin for Eclipse. It provides a lot of functionality, including port-forwarding services, screen capture on the device, thread and heap usage of processes, memory allocation of objects, emulation of device's file system, incoming call and SMS spoofing, and last but not least, location data spoofing. It's located in the "tools/" directory of the SDK. DDMS works with both the emulator and a connected device. If both are connected and running simultaneously, DDMS defaults to the emulator.
Eclipse's Emulator Control Panel contains location controls for each type. To open it, click Window > Open Perspective > Other… > DDMS from the Eclipse menu.
It supports the following three geolocation data types:
- Manual: For setting the location manually by specifying longitude and latitude values.
- GPX: GPS eXchange file
- KML: Keyhole Markup Language file
Obtaining Latitude and Longitude Data For Manual Entry
You can get latitude and longitude numbers from websites such as Get Lat Lon. All you have to do is zoom the map to the location you want and line it up in the center cross hairs. Here's centre-town Ottawa below:
You can then copy & paste the coordinates into the Eclipse Location Control and send them to your device or emulator.
GPX - also known as the GPS Exchange Format - is a light-weight, open source XML data format for the interchange of GPS data between applications and Web services over the Internet. It can be used to describe waypoints, tracks, and routes as well as store location, elevation, and time. Programs that support GPX allow users to view their tracks, project their tracks on satellite images or other maps, annotate maps, and tag photographs with the geolocation in the Exif metadata.
<?xml version="1.0"?> <gpx xmlns="http://www.topografix.com/GPX/1/0" xsi:schemaLocation="http://www.topografix.com/GPX/1/0 http://www.topografix.com/GPX/1/0/gpx.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" creator="ExpertGPS 1.1 - http://www.topografix.com" version="1.0"> <time>2002-02-27T17:18:33Z</time> <bounds maxlon="-71.102973" maxlat="42.468655" minlon="-71.126602" minlat="42.401051"/> <wpt lon="-71.119277" lat="42.438878"> <ele>44.586548</ele> <time>2001-11-28T21:05:28Z</time> <name>5066</name> <desc> <![CDATA]> </desc> <sym>Crossing</sym> <type> <![CDATA[Crossing]]> </type> </wpt> <wpt lon="-71.119689" lat="42.439227"> <ele>57.607200</ele> <time>2001-06-02T03:26:55Z</time> <name>5067</name> <desc> <![CDATA]> </desc> <sym>Dot</sym> <type> <![CDATA[Intersection]]> </type> </wpt> <wpt lon="-71.116146" lat="42.438917"> <ele>44.826904</ele> <time>2001-11-16T23:03:38Z</time> <name>5096</name> <desc> <![CDATA]> </desc> <sym>Dot</sym> <type> <![CDATA[Dot]]> </type> </wpt> ...etc... <rte> <name>BELLEVUE</name> <desc> <![CDATA[Bike Loop Bellevue]]> </desc> <number>1</number> <rtept lon="-71.109942" lat="42.434980"> <ele>45.307495</ele> <time>2001-11-07T23:53:41Z</time> <name>PANTHRCAVE</name> <desc> <![CDATA[Panther Cave]]> </desc> <sym>Tunnel</sym> <type> <![CDATA[Tunnel]]> </type> </rtept> <rtept lon="-71.109236" lat="42.431240"> <ele>26.561890</ele> <time>2001-11-07T23:53:41Z</time> <name>GATE6</name> <desc> <![CDATA[Gate 6]]> </desc> <sym>Trailhead</sym> <type> <![CDATA[Trail Head]]> </type> </rtept> </rte> </gpx>
Here is a portion of a GPX file outlining the Mountain bike loop at Middlesex Fells reservation:
2002-02-27T17:18:33Z 44.586548 2001-11-28T21:05:28Z 5066 Crossing 57.607200 2001-06-02T03:26:55Z 5067 Dot 44.826904 2001-11-16T23:03:38Z 5096 Dot ...etc… BELLEVUE 1 45.307495 2001-11-07T23:53:41Z PANTHRCAVE Tunnel 26.561890 2001-11-07T23:53:41Z GATE6 Trailhead
To use GPX in Eclipse:
- Switch to DDMS view.
- Find the Location Controls in the Emulator Control tab.
- Click the GPX tab and click Load GPX.
- Locate and select the GPX file.
- Click Play to begin sending coordinates to the Emulator.
KML is a file format used to display geographic data in an Earth browser, such as Google Earth, Google Maps, and Google Maps for mobile. You can create KML files to pinpoint locations, add image overlays, and display rich data. KML is an international standard maintained by the Open Geospatial Consortium, Inc. (OGC).
A Placemark is one of the most commonly used features in Google Earth. It marks a position on the Earth's surface, using a yellow pushpin as the icon. The simplest Placemark includes only aelement, which specifies the location of the Placemark. You can specify a name and a custom icon for the Placemark, and you can also add other geometry elements to it.
There are three different types of placemark: simple, floating, and extruded. The KML code for a simple placemark looks as follows:
Placemark Example St. John's, NL St. John's, NL is home to the Memorial University of Newfoundland. -52.701129,47.576727,0
Making your Android apps location-aware can really add a lot of punch to them. It's worth knowing how to test them properly - a process which is both fairly straight forward and crucial to the smooth operation of your applications.
If you enjoyed this article, please contribute to Rob's rock star aspirations by purchasing one of Rob's cover or original songs from iTunes.com for only 0.99 cents each.
Rob Gravelle resides in Ottawa, Canada, and is the founder of GravelleWebDesign.com. Rob has built systems for Intelligence-related organizations such as Canada Border Services, CSIS as well as for numerous commercial businesses. EmailRob to receive a free estimate on your software project.
In his spare time, Rob has become an accomplished guitar player, and has released several CDs. His former band, Ivory Knight, was rated as one Canada's top hard rock and metal groups by Brave Words magazine (issue #92).
Rob uses and recommends MochaHost, which provides Web Hosting at $3.10 per month, 2 LifeTime Free Domains, and 6 Months Free!