Overlays can be added to the MapView to make it responsive to touch and other events. Overlays can be anything from a pin to a button etc. They exist as a seperate layer on top of MapView layer. ItemizedOverlayis the class for this purpose. Overriding this class gives us control for managing the overlay items.

public void onCreate(Bundle savedInstanceState)
{ super.onCreate(savedInstanceState);
setContentView(R.layout.main);
map =(MapView) findViewById(R.id.maps);
p = new GeoPoint((int)(-33.882063 * 1000000),(int)(151.173549 * 1000000));
q = new GeoPoint((int)(-33.881511 * 1000000),(int)(151.171253 * 1000000));
ZoomControls zoomControls = (ZoomControls) map.getZoomControls();
zoomControls.setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
map.addView(zoomControls);
map.displayZoomControls(true);
Here we use MapView defined in the XML layout file rather than programmatically defining it.
pand qare two points(locations) that we want to show on the map as overlays.
ZoomControlsis the class for handling the +/- zoom controls. These controls appear when you interact with the map by touch or key and are hidden otherwise. The controls' visibility is managed by MapView.
ZoomControls, like any other view, is added to the parent view.
defaultMarker = getResources().getDrawable(R.drawable.bubble); defaultMarker.setBounds(0, 0, defaultMarker.getIntrinsicWidth(), defaultMarker.getIntrinsicHeight()); map.getOverlays().add(new MapOverlay(defaultMarker)); defaultMarkeris a Drawable which stores the image we want to use as a pin. setBounds()specifies a rectangle in which the pin will be drawn.
Now comes the important part of overriding the ItemizedOverlay class. private class MapOverlay extends ItemizedOverlay { private ArrayList mItemList = new ArrayList (); public void draw(Canvas canvas, MapView mapView, boolean shadow) { super.draw(canvas, mapView, shadow); MapOverlay.boundCenterBottom(defaultMarker); } mItemListstores the no. of items we want to show on the map.
draw()draws a marker for each item. The marker will be drawn twice for each item, once in the shadow phase, skewed and darkened, then again in the non-shadow phase. boundCenterBottom()ensures that bases of the pin and its shadow are at the exact point, take a look at the difference,

public MapOverlay(Drawable pMarker) { super(pMarker); mItemList.add(new OverlayItem(p, "Title", "Snippet"));
mItemList.add(new OverlayItem(q, "Title", "Snippet"));
populate(); } protected OverlayItem createItem(int pIndex) { return mItemList.get(pIndex); }
In this contructor, we add all the points that are to be shown on the map.
populate()calls the draw() to process all the items. creatItem()creates the item pointed to by pIndex protected boolean onTap(int pIndex) { switch (pIndex) { case 0: Toast.makeText(Mapping.this,"Zenith on Booth, 37 Booth St, Annandale",Toast.LENGTH_SHORT).show(); return true; case 1: Toast.makeText(Mapping.this, "Crust Gourmet Pizza Bar, 113A Booth St, Annandale", Toast.LENGTH_SHORT).show(); return true; } return false; } onTap()handles tap, touch or trackball click events. pIndexis the item in mItemListthat has been tapped.______________________________
Theo gphone

| < Lùi | Tiếp theo > |
|---|
Modding 



