Using ItemizedOverlay to add marker on MapView
Reference last exercise MapView and MapActivity, we are going to add overlay of ItemizedOverlay, to add marker on our MapView.
The main.xml and AndroidManifest.xml are kept as in last postMapView and MapActivity.
Add a new class MyItemizedOverlay.java extends ItemizedOverlay.
Modify our main class AndroidMapViewActivity.java to add ItemizedOverlay on ur MapView.
The main.xml and AndroidManifest.xml are kept as in last postMapView and MapActivity.
Add a new class MyItemizedOverlay.java extends ItemizedOverlay.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
| package com.AndroidMapView; import java.util.ArrayList; import android.graphics.Canvas; import android.graphics.drawable.Drawable; import com.google.android.maps.GeoPoint; import com.google.android.maps.ItemizedOverlay; import com.google.android.maps.MapView; import com.google.android.maps.OverlayItem; public class MyItemizedOverlay extends ItemizedOverlay<OverlayItem>{ private ArrayList<OverlayItem> overlayItemList = new ArrayList<OverlayItem>(); public MyItemizedOverlay(Drawable marker) { super (boundCenterBottom(marker)); // TODO Auto-generated constructor stub populate(); } public void addItem(GeoPoint p, String title, String snippet){ OverlayItem newItem = new OverlayItem(p, title, snippet); overlayItemList.add(newItem); populate(); } @Override protected OverlayItem createItem( int i) { // TODO Auto-generated method stub return overlayItemList.get(i); } @Override public int size() { // TODO Auto-generated method stub return overlayItemList.size(); } @Override public void draw(Canvas canvas, MapView mapView, boolean shadow) { // TODO Auto-generated method stub super .draw(canvas, mapView, shadow); //boundCenterBottom(marker); } } |
Modify our main class AndroidMapViewActivity.java to add ItemizedOverlay on ur MapView.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
| package com.AndroidMapView; import com.google.android.maps.GeoPoint; import com.google.android.maps.MapActivity; import com.google.android.maps.MapView; import android.graphics.drawable.Drawable; import android.os.Bundle; public class AndroidMapViewActivity extends MapActivity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.main); MapView mapView = (MapView) findViewById(R.id.mapview); mapView.setBuiltInZoomControls( true ); Drawable marker=getResources().getDrawable(android.R.drawable.star_big_on); int markerWidth = marker.getIntrinsicWidth(); int markerHeight = marker.getIntrinsicHeight(); marker.setBounds( 0 , markerHeight, markerWidth, 0 ); MyItemizedOverlay myItemizedOverlay = new MyItemizedOverlay(marker); mapView.getOverlays().add(myItemizedOverlay); GeoPoint myPoint1 = new GeoPoint( 0 * 1000000 , 0 * 1000000 ); myItemizedOverlay.addItem(myPoint1, "myPoint1" , "myPoint1" ); GeoPoint myPoint2 = new GeoPoint( 50 * 1000000 , 50 * 1000000 ); myItemizedOverlay.addItem(myPoint2, "myPoint2" , "myPoint2" ); } @Override protected boolean isLocationDisplayed() { // TODO Auto-generated method stub return false ; } @Override protected boolean isRouteDisplayed() { // TODO Auto-generated method stub return false ; } }
akm www.cdacians.com |
No comments:
Post a Comment