Login     Sign Up
Tugadar (@admin)
14 days ago

You use the Map Controller to pan and zoom a MapView. You can get a reference to a MapView’s control-ler using getController, as shown in the following code snippet:

MapController mapController = myMapView.getController();

Map locations in the Android mapping classes are represented by GeoPoint objects, which contain lati-tude and longitude measured in microdegrees (i.e., degrees multiplied by 1E6 [or 1,000,000]).

Before you can use the latitude and longitude values stored in the Location objects used by the location-based services, you’ll need to convert them to microdegrees and store them as GeoPoints, as shown in the following code snippet:

Double lat = 37.422006*1E6;
Double lng = -122.084095*1E6;
GeoPoint point = new GeoPoint(lat.intValue(), lng.intValue());

Re-center and zoom the MapView using the setCenter and setZoom methods available on the

MapView’s MapController, as shown in the snippet below:



When using setZoom, 1 represents the widest (or furthest away) zoom and 21 the tightest (nearest) view.

The actual zoom level available for a specific location depends on the resolution of Google’s maps and imagery for that area. You can also use zoomIn and zoomOut to change the zoom level by one step.

The setCenter method will “jump” to a new location; to show a smooth transition, use animateTo as shown in the code below:


Mapping “Where Am I?”

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

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=”http://schemas.android.com/apk/res/android” package=”com.paad.whereami”>







<action android:name=”android.intent.action.MAIN” /> <category android:name=”android.intent.category.LAUNCHER” />



<uses-library android:name=”com.google.android.maps”/> </application>

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


Change the inheritance of WhereAmI to descend from MapActivity instead of Activity. You’ll also need to include an override for the isRouteDisplayed method. Because this Activity won’t show routing directions, you can return false.

public class WhereAmI extends MapActivity {


protected boolean isRouteDisplayed() {

return false;


[ ... existing Activity code ... ]


Modify the main.xml layout resource to include a MapView using the fully qualified class name. Be sure to include an android:apikey attribute within the com.android.MapView node. If you have an Android maps API key, use it here.

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





















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

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;


public void onCreate(Bundle icicle) {



// Get a reference to the MapView

MapView myMapView = (MapView)findViewById(R.id.myMapView);

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


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); criteria.setPowerRequirement(Criteria.POWER_LOW);

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

Location location = locationManager.getLastKnownLocation(provider);


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


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

private void updateWithNewLocation(Location location) { String latLongString;

TextView myLocationText;

myLocationText = (TextView)findViewById(R.id.myLocationText);

String addressString = “No address found”;

if (location !=
null) {

// 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.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);