Login     Sign Up
Tugadar (@admin)
19 days ago
14 Views

Introducing MyLocationOverlay

The MyLocationOverlay class is a special Overlay designed to show your current location and orienta-tion on a MapView.

To use the My Location Overlay, you need to create a new instance, passing in the application Context and target Map View, and add it to the MapView’s Overlay list, as shown below:

List<Overlay> overlays = mapView.getOverlays();

MyLocationOverlay myLocationOverlay = new MyLocationOverlay(this, mapView);

overlays.add(myLocationOverlay);

You can use the My Location Overlay to display both your current location (represented as a flashing blue marker) and orientation (shown as a compass on the map display).

The following snippet shows how to enable both the compass and marker; in this instance, the Map View’s MapController is also passed in, allowing the overlay to automatically scroll the map if the marker moves off screen.

myLocationOverlay.enableCompass(); myLocationOverlay.enableMyLocation(mapView.getMapController());

Introducing ItemizedOverlays and OverlayItems

OverlayItems are used to supply simple maker functionality to your MapViews using the ItemizedOverlay class.

You can create your own Overlays that draw markers onto a map, but ItemizedOverlays provide a convenient shortcut, letting you assign a marker image and associated text to a particular geographical position. The ItemizedOverlay instance handles the drawing, placement, click handling, focus con-trol, and layout optimization of each OverlayItem marker for you.

At the time of going to print, the ItemizedOverlay/OverlayItem functionality was not fully supported. While it was possible to implement each required class, the markers were not displayed on the map.

To add an ItemizedOverlay marker layer to your map, start by creating a new class that extends

ItemizedOverlay<OverlayItem>, as shown in the skeleton code below:

import android.graphics.drawable.Drawable; import com.google.android.maps.GeoPoint; import com.google.android.maps.ItemizedOverlay; import com.google.android.maps.OverlayItem;

public class MyItemizedOverlay extends ItemizedOverlay<OverlayItem> {

public MyItemizedOverlay(Drawable defaultMarker) { super(defaultMarker);

Create each of the overlay items included in this layer. populate();

}

@Override

protected OverlayItem createItem(int index) {

switch (index) {

case 1:

Double lat = 37.422006*1E6;

Double lng = -122.084095*1E6;

GeoPoint point = new GeoPoint(lat.intValue(), lng.intValue());

OverlayItem oi;

oi = new OverlayItem(point, “Marker”, “Marker Text”); return oi;

}

return null;

}

@Override

public int size() {

Return the number of markers in the collection return 1;

}

}

ItemizedOverlay is a generic class that lets you create extensions based on any OverlayItem-derived subclass.

Within the implementation, override size to return the number of markers to display and createItem to create a new item based on the index of each marker. You will also need to make a call to populate within the class’s constructor. This call is a requirement and is used to trigger the creation of each OverlayItem; it must be called as soon as you have the data required to create all the items.

To add an ItemizedOverlay implementation to your map, create a new instance (passing in the default drawable marker image to use), and add it to the map’s Overlay list, as shown in the following snippet:

List<Overlay> overlays = mapView.getOverlays();

MyItemizedOverlay markrs = new MyItemizedOverlay(r.getDrawable(R.drawable.marker)); overlays.add(markrs);