Using KML on DDMS:
The easiest way to send a path of GPS points to the emulator is through the Location Controls in the DDMS for Android emulator. Just need a KML file for storing the path, and use DDMS to send this path of locations to the emulator at a certain interval.
1. Start the Google Earth desktop application and click on "Add" and then "Path" to draw the path:
2. "Cltr+S" to export the route as a kml file on to your disk;
3. Use a web tool (e.g.ge2adt) to convert that the Goolge Earth route into the Eclipse ADT compatible kml format; (here is the sample kml route file after the conversion)
Or, if you don't want to use Google Earth, the KML file format for the Android emulator is like:
4. Start the Android emulator and your GPS location app; Click on the "Load KML..." on the Location Controls field in the DDMS to load the path kml:
Using the MockLocationBuilder:
The following small app is to send the mock locations that a user has specified to the emulator (or real device). The app works like this:
1. Start the MockLocationBuilder and draw the path;The following small app is to send the mock locations that a user has specified to the emulator (or real device). The app works like this:
2. Start the test GPS;
3. Start the target app that use the test GPS's updates (e.g. Google Map on Android).
You can download the code from here.
The app supports:
1. Define the path
2. Generate the mock GPS updates loop around the path at regular interval
3. Work in the background (allow you to start the target app)
About the Code:
The MockLocationBuilder class extends the MapActivity class to display and manage the map:
public class MockLoctionBuilderActivity extends MapActivity
Then specify the location provider's name:
static private String mTestProvider = LocationManager.GPS_PROVIDER;
To add the mock location provider, you can use this function:
private void addMockLocationManager() {
mLocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if (mLocManager.getProvider(mTestProvider) == null)
{
mLocManager.addTestProvider(mTestProvider, false, false,
false, false, false, false, false,
android.location.Criteria.POWER_HIGH,
android.location.Criteria.ACCURACY_FINE);
}
mLocManager.setTestProviderEnabled(mTestProvider, true);
}
Note that you can only specify the provider to be LocationManager.GPS_PROVIDER on the emulator but not on a real device, because the addTestProvider method will throw the error for the provider named "gps". To use on the device, you can specify the mTestProvider as some other strings.
And set the mock location at a regular interval through this method:
private void setMockLocation(double latitude, double longitude) {
Location newLocation = new Location(mTestProvider);
newLocation.setLatitude(latitude);
newLocation.setLongitude(longitude);
newLocation.setTime(System.currentTimeMillis());
mLocManager.setTestProviderLocation(mTestProvider,
newLocation);
}
Note that the newLocation.setTime method set the timestamp when the new location is generated, which makes the location listener takes in it as an updated location.
Release the provider after the running by:
if (mLocManager.getProvider(mTestProvider) != null)
mLocManager.removeTestProvider(mTestProvider);
PS: 1. On the device, remember that the system might setup the LocationManager.GPS_PROVIDER as the default provider, so that you cannot call the addTestProvider method on it. However, you can specify the test provider to be some other name and use the same test provider in the target app for location updates;
2. You may want to change the frequency of the GPS updates.
Ref:
This post provides more detailed discription of the important methods:
http://ballardhack.wordpress.com/2010/09/23/location-gps-and-automated-testing-on-android/
Thanks for your article! really like it http://www.enterra-inc.com/techzone/gps-development-issues-on-android/ may be also helpful
ReplyDeleteHi there! If you need a free online conversion tool, I recommend to have a look at http://gpx2kml.com/ to turn gpx into kml and vice versa, when needed. Simple and easy to use, no installation required, try it and see the results. It might be useful, thank you!!
ReplyDelete