Joe Asks

^ Why Is MapView in the com.google.android.maps Package and Not android.maps?

Any code in the android.* packages is part of the Android core. It's open source and available on every Android device. By contrast, maps are proprietary to Google and to the data providers that Google paid for the geological information and imagery. Google provides the API free of charge as long as you agree to certain conditions.* If you're not happy with the restrictions, you can roll your own views and find your own data sources, but it's not going to be easy or cheap.

*. http://code.google.com/apis/maps/terms.html

Download MyMap/src/org/example/mymap/MyMap.java

private void initMyLocation() {

final MyLocationOverlay overlay = new MyLocationOverlay(this, map); overlay.enableMyLocation();

//overlay.enableCompass(); // does not work in emulator overlay.runOnFirstFix(new Runnable() { public void run() {

// Zoom in to current location controller.setZoom(8);

controller.animateTo(overlay.getMyLocation());

map.getOverlays().add(overlay);

Android provides a MyLocationOverlay class that does most of the heavy lifting. An overlay is just something that is drawn on top of the map, which in this case is a pulsing dot showing your current location. You call enableMyLocation() to tell the overlay to start listening to location updates and call enableCompass() to tell it to start listening to updates from the compass.

The runOnFirstFix( ) method tells the overlay what to do the first time it gets a position reading from the location provider. In this case, we set the zoom level and then start an animation that moves the map from wherever it's pointing now to where you are located.

If you run the program now, you should see something like Figure 8.3, on page 173. Touch and drag the screen to move around the map, and use the zoom buttons to get a closer look. When you walk around carrying the phone, the dot on the map should follow you.

0 0

Post a comment