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.
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.
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; } }