Map View and MyLocation Overlay 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 again 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); // get the map controller final MapController mc = mvMap.getController();

mMyLocationOverlay = new MyLocationOverlay(this, mvMap); mMyLocationOverlay.runOnFirstFix( new Runnable() {

mc.animateTo(mMyLocationOverlay.getMyLocation()); mc.setZoom(l6);

Drawable marker = getResources().getDrawable(R.drawable.android_tiny_image);© marker.setBounds(0, 0, marker.getIntrinsicWidth(), marker.getIntrinsicHeight()); mvMap.getOverlays().add(new MJJobsOverlay(marker));

mvMap.setClickable(true);© mvMap.setEnabled(true);

mvMap.setSatellite(false);

mvMap.setTraffic(false);

mvMap.setStreetView(false);

// start out with a general zoom mc.setZoom(16);0

* Required method to indicate whether we display routes */

@Override protected boolean isRouteDisplayed() { return false; }

Here are some of the highlights of the code:

O We first find the MapView in the main.xml layout file, the same way we find any other view, and assign it to the variable mvMap of type MapView, so we can refer to it when we need to.

© We also get a handle on the MapController associated with MapView. We'll use that to pan (animate) the map, zoom in, zoom out, change views, etc.

© To use MyLocationOverlay, we create a new instantiation, giving it the highly creative name mMyLocationOverlay.

O The first thing we do with mMyLocationOverlay is define a method that Android will call when we receive our first location fix from the location provider.

© This runOnFirstFix method moves the map to the current location (given by mMyLocationOverlay.getMyLocation()) and zooms to a reasonable level for us to see nearby job prospects.

© Next, we identify a marker that we've decided to use on mMyLocationOverlay to mark available jobs. We use an image that's stored in our res/drawable directory, called android_tiny_image. It's a picture of a little Android robot. We define the bounds of the Drawable marker, as described in Chapter 12, and add the marker overlay to the list of overlays for the MapView mvMap.

O Now we'd like to set some initial attributes for mvMap, described later in this section. We'll allow the user to change most of these through menu buttons.

© Then, following a belt-and-suspenders philosophy, just in case there isn't a location provider to trigger runOnFirstFix, we'll set the zoom level again here.

© Finally, MapView requires us to override the isRouteDisplayed() method to indicate whether we are displaying route information on our map. We are not, so we return false.

MyLocationOverlay encapsulates a wealth of location and mapping code. In our single call to the constructor we:

• Ask Android to figure out what location providers are available in our environment (GPS, Cell ID, triangulation).

• Connect to the "best" of those location providers.

• Ask the location provider to send us periodic location updates as our handset moves.

• Link to routines that will automatically move our map as needed to track any changes in location.

MyLocationOverlay also allows us to place a compass rose on the MapView and have that updated as well, but we won't be using that in MJAndroid.

The map attributes set by the code are: setClickable

We want users to be able to click (tap) on a job to cause MJAndroid to display more detail about that job, so we set this to true.

setEnabled

This method is actually inherited from android.view.View. Google doesn't tell us exactly what this means in the case of a MapView, but presumably it enables the standard map functions (zooming, panning, etc.). setSatellite

Setting this flag adds a satellite view from the composite map, whereas clearing the flag removes the view. To start with, we don't want the satellite information on the map.

setTraffic

Similarly, setting or clearing this flag adds or removes current traffic information from the map, respectively. Again, we don't want to start with traffic information on the map. setStreetView

We don't want street views right now either, although we'll let the user enable them later.

Zooming in Android Maps

Android maps come already equipped with support for zooming in and out. The "i" key zooms in on the map, and the "o" key zooms out. Maps can also zoom in and out under program control, through the MapController.

There are several methods defined for zooming, all via the MapController. Android defines 21 zoom levels for maps. At zoom level 1, the equator of the earth is 256 pixels long. Every step up in zoom level multiplies that by 2. Google warns that the higherresolution maps are not available worldwide. All of the zoom methods clamp the zoom level to the range 1 through 21 if you ask MapController to go beyond those limits.

The methods that control zoom, along with their parameters, are:

zoomIn

Zooms in one level.

zoomOut

Zooms out one level.

setZoom(int zoomlevel)

Zooms to the given level, restricting it to the range 1 to 21.

zoomInFixing(int xpixel, int ypixel), zoomOutFixing(int xpixel, int ypixel)

Zoom in one level, but keep the given point fixed on the screen. Normally when you zoom in and out, the center of the screen is the only point that stays fixed. These routines let you pick any point on the map to be the fixed point.

zoomToSpan(int latSpanE6, int longSpanE6)

Attempts to zoom so the given span is displayed on the map. What it actually does is select the zoom level that is the closest match for the span requested. The latitude and longitude span parameters are expressed as integers with a value 106 times the actual value in degrees. For instance, a latitude/longitude span of 2.5 degrees by 1.0 degrees would be expressed as zoomToSpan(2500000, 1000000).

0 0

Responses

  • Sinit
    Why isRouteDisplayed() method is required in mapview?
    7 years ago
  • hyt
    How to control my location overlay zoom level android mapview?
    7 years ago

Post a comment