Thursday, November 20, 2008

GPS and MapView in Android

Having spent an evening hacking around writing an Android application that uses the GPS position together with the MapView component to show both your current location latitude/longitude and visually I spent a lot of time sifting through sites and blogs that used out of date APIs and overlooked crucial steps, so I thought I'd document how to enable GPS access and MapView usage using the Android Eclipse plugin and the emulator. This will then work when running on an Android device.

Step 1: GPS Access
Basic GPS access is relatively straightforward. You can use code like this in your Activity.onCreate() to get the current position and register a listener for position updates:

LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);

List pro = lm.getAllProviders();
Location l = lm.getLastKnownLocation(LOCATION_SERVICE);


Intent i = new Intent(LOCATION_CHANGED);
LocationListener ll = new LocationListener() {
public void onLocationChanged(Location location) {
public void onProviderDisabled(String provider) {
public void onProviderEnabled(String provider) {
public void onStatusChanged(String provider, int status, Bundle extras) {
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 30000, 100, ll);
In addition to this you need to add permissions to your Android manifest file to allow your application access to the location services:

<uses-permission android:name="android.permission.ACCESS_LOCATION/">
<uses-permission android:name="android.permission.ACCESS_GPS"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"/>

For running your application in the Android emulator you will need mock position data, as there is no live GPS data from the emulator.

The easiest way to do this is to launch the emulator from Eclipse and then switch to the DDMS perspective (Window -> Open Perspective -> Other). Your Eclipse should then look something like the picture on the right.

This shows you a device browser on the left, w ith an Emulator Control tab underneath. Scroll down the Emulator Control tab until you see the Location Controls section. This allows you to enter mock location data to use when running the Emulator - the best way is to use a KML file, which you can create from Google Earth, or download a default location file from Once this has been imported click the green play button in the Location Controls (KML) section and your application will receive location updates based on the contents of the KML file.

Step 2: Google MapView component

The Google MapView component allows you to embed the Google map component in your application, so we can combine our live location data with a visual display of that location on a map. Embedding the MapView component has a few "tricky" steps:

Add the MapView component to your Activity:
android:apiKey="your api key"

The apiKey attribute should be the MD5 fingerprint of your Maps API key. In order to deploy your MapView enabled application you need to register for a Google Maps API key, but for debugging you can use the Android SDK's debug key. All the details on Google Maps API keys can be found here, with details on how to obtain the debug SDK fingerprint here /apis/mapkey.html#getdebugfingerprint.

Once you have this, then to enable the MapView component you need to:
  • Change your Activity to extend MapActivity rather than Activity.
  • Add <uses-library android:name=""/> as a child of the element in the Android Manifest file.
  • Enable Internet access for your application so that it can download map data by adding <uses-permission android:name="android.permission.INTERNET"/> to your manifest file.
You can then add code to your application's location listener to update the MapView component to reflect the current position, such as:
MapView mapView = (MapView) findViewById(;
MapController mc = mapView.getController();

GeoPoint p = new GeoPoint((int) (l.getLatitude() * 1E6),
(int) (l.getLongitude() * 1E6));
When you run your application you should see your live MapView control responding to GPS lcoation updates, like below:

Thursday, November 13, 2008

Tracking Java versions of web site visitors

Gili over at 'Where the cows roam free" has a very interesting post about how to combine Google Analytics and the new Java deployment file to track what version of Java visitors to a website are using:

It's trivial to add it to an existing Google Analytics-enabled site, and should hopefully yield some interesting and useful results, so I've added it to this site and Cool Applets too.