Hello, Android

Page 171

B IRD ’ S -E YE V IEW public class MyMap extends MapActivity { private MapView map; private MapController controller; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); initMapView(); initMyLocation(); } @Override protected boolean isRouteDisplayed() { // Required by MapActivity return false; } }

The most important part is that your activity has to extend MapActivity. The MapActivity class spins up the background threads, connects to the Internet for tile data, handles caching, does animations, takes care of the life cycle, and much more. All you need to do is properly set it up and let it go.

Getting Ready The first thing we need to do is call findViewById( ) to get access to the MapView and its container. We can do that in the initMapView( ) method: Download MyMap/src/org/example/mymap/MyMap.java

/** Find and initialize the map view. */ private void initMapView() { map = (MapView) findViewById(R.id.map); controller = map.getController(); map.setSatellite(true); map.setBuiltInZoomControls(true); }

The getController( ) method returns a MapController that we’ll use to position and zoom the map. setSatellite( ) switches the map into satellite mode, and setBuiltInZoomControls( )8 turns on the standard zoom controls. The MapView class will take care of making the controls visible when the user pans the map and fading them out slowly when panning stops. The last step is to tell the MapView to follow your position in the initMyLocation( ) method: 8.

Introduced in Android 1.5.

Report erratum

Prepared exclusively for Trieu Nguyen

this copy is (P1.5 printing, July 21, 2009)

171


Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.