O'Reilly logo

Programming Android by Zigurd Mednieks, G. Blake Meike, Masumi Nakamura, Laird Dornin

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

MapView and MyLocationOverlay Initialization

The map in MicroJobs has two modes:

  • At startup, and when we select Current Location from the Spinner, we want to display a map of our current location, and we want that map to track us as we move around. For this map, we will use the MyLocationOverlay class.

  • When we select a specific location from the Spinner, we want to display a map of that location, turn off location updates, and not track movement.

Let’s look at the code in MicroJobs.java that initializes the MapView and the MyLocationOverlay that tracks our current location:

@Override
 public void onCreate(Bundle savedInstanceState) {

...

     mvMap = (MapView) findViewById(R.id.mapmain);1

     // get the map controller
     final MapController mc = mvMap.getController();2

     mMyLocationOverlay = new MyLocationOverlay(this, mvMap);3
     mMyLocationOverlay.runOnFirstFix(4
         new Runnable() {
             public void run() {
                 mc.animateTo(mMyLocationOverlay.getMyLocation());5 mc.setZoom(16); } }); Drawable marker = getResources().getDrawable(R.drawable.android_tiny_image); ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required