Mapping Where Am I

In the following code example the Where Am I? project is extended again. This time you'll add mapping functionality by transforming it into a Map Activity. As the device location changes, the map will automatically re-center on the new position.

1. Start by adding the <uses-permission> tag for Internet access to the application manifest. Also import the Android maps library within the application tag.

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="" package="com.paad.whereami"> <application android:icon="@drawable/icon">

<uses-library android:name=""/>

<activity android:name=".WhereAmI"



<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application>

<uses-permission android:name="android.permission.INTERNET"/>

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> </manifest>

2. Change the inheritance of WhereAmI to descend from MapActivity instead of Activity. You'll also need to include an override for theisRouteDisplayed method. Because this Activity won't show routing directions, you can returifalse.

public class WhereAmI extends MapActivity { ©Override protected boolean isRouteDisplayed() { return false;

3. Modify the main.xml layout resource to include a MapView using the fully qualified class name. You will need to obtain a maps API key to include within the android:apikey attribute of the node.

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parentM> <TextView android:id="@+id/myLocationText" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello"

< android:id="@+id/myMapView" android:layout_width="fill_parent" android:layout_height="fill_parent" android:enabled="true" android:clickable="true" android:apiKey="myMapKey"


4. Running the application now should display the original geolocation text with a MapView beneath it, as shown in Figure 8-5.

5. Configure the Map View and store a reference to its MapController as an instance variable. Set up the Map View display options to show the satellite and Streetview and zoom in for a closer look.

MapController mapController;

©Override public void onCreate(Bundle savedlnstance State) {

super.onCreate(savedlnstanceState); setContentView(R.layout.main);

// Get a reference to the MapView MapView myMapView = (MapView)findViewByld(; // Get the Map View's controller mapController = myMapView.getController();

// Configure the map display options myMapView.setSatellite(true); myMapView.setStreetView(true); myMapView.displayZoomControls(false);

// Zoom in mapController.setZoom(17);

LocationManager locationManager;

String context = Context.LOCATION_SERVICE;

locationManager = (LocationManager)getSystemService(context); Criteria criteria = new Criteria();


criteria.setAccuracy(Criteria.ACCURACY_FINE); criteria.setAltitudeRequired(false); criteria.setBearingRequired(false); criteria.setCostAllowed(true);


String provider = locationManager.getBestProvider(criteria, true);

Location location =



locationManager.requestLocationUpdates(provider, 2000, 10, locationListener);

6. The final step is to modify the updateWithNewLocation method to re-center the map on the current location using the Map Controller.

private void updateWithNewLocation(Location location) { String latLongString; TextView myLocationText;

myLocationText = (TextView)findViewById(; String addressString = "No address found";

// Update the map location. Double geoLat = location.getLatitude()*1E6; Double geoLng = location.getLongitude()*1E6; GeoPoint point = new GeoPoint(geoLat.intValue(), geoLng.intValue());


double lat = location.getLatitude(); double lng = location.getLongitude(); latLongString = "Lat:" + lat + "\nLong:" + lng;

double latitude = location.getLatitude(); double longitude = location.getLongitude();

Geocoder gc = new Geocoder(this, Locale.getDefault()); try {

List<Address> addresses = gc.getFromLocation(latitude, longitude, 1); StringBuilder sb = new StringBuilder(); if (addresses.size() > 0) {

Address address = addresses.get(0);

for (int i = 0; i < address.getMaxAddressLineIndex(); i++) sb.append(address.getAddressLine(i)).append("\n");

sb.append(address.getLocality()).append("\n"); sb.append(address.getPostalCode()).append("\n"); sb.append(address.getCountryName());

addressString = sb.toString(); } catch (IOException e) {} } else {

latLongString = "No location found";

myLocationText.setText("Your Current Position is:\n" +

latLongString + "\n" + addressString);

All code snippets in this example are part of the Chapter 8 Where Am I? project, available for download at

Mobile Apps Made Easy

Mobile Apps Made Easy

Quick start guide to skyrocket your offline and online business success with mobile apps. If you know anything about mobile devices, you’ve probably heard that famous phrase coined by one of the mobile device’s most prolific creators proclaiming that there’s an app for pretty much everything.

Get My Free Training Guide

Post a comment