package Hello.marker;import com.google.android.maps.*;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.graphics.drawable.Drawable;
import android.location.*;
import android.view.GestureDetector;
public class Main extends MapActivity {
 
 private GestureDetector mGestureDetector;
/*
 @Override
 public boolean onTouchEvent(MotionEvent event) {
  // TODO Auto-generated method stub
     Log.d("touch", "onTouchEvent");
        return mGestureDetector.onTouchEvent(event);
  
 }
*/
 /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Log.d("start", "start");
        
        MapView mapView = (MapView) findViewById(R.id.mapview); 
        mapView.setBuiltInZoomControls(true);
        GeoPoint point = new GeoPoint(37497618,127031039);
//      mapView.setSatellite(true);
  mapView.getController().animateTo(point);
  mapView.getController().setZoom(15);
  
  
  Drawable logo = getResources().getDrawable(R.drawable.android);
  
  logo.setBounds(0, 0, logo.getIntrinsicWidth(),
    logo.getIntrinsicHeight());
  
  Marker itemizedOverlay = new Marker(logo,this,mapView);
  OverlayItem overlayitem = new OverlayItem(point, "안녕", "서울");
  itemizedOverlay.addOverlay(overlayitem);
  mapView.getOverlays().add((itemizedOverlay));
  
    }

 @Override
 protected boolean isRouteDisplayed() {
  // TODO Auto-generated method stub
  return false;
 }
}

////////////////////////////////////////////////////////////////////////////////////////////////////

package Hello.marker;import java.util.ArrayList;
import android.app.AlertDialog;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
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 Marker extends ItemizedOverlay<OverlayItem> implements OnGestureListener{
 private Context mContext;
 private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
 private GestureDetector mGestureDetector; 
 private GeoPoint markpoint = new GeoPoint(0,0);
 private MapView view;
 private Drawable marker;
 private boolean longTouch = false;
 
 public Marker(Drawable defaultMarker, Context context, MapView mapview) {
  super(boundCenterBottom(defaultMarker));
  // TODO Auto-generated constructor stub
  marker = defaultMarker;
  mContext = context;
  mGestureDetector = new GestureDetector(this);
  view = mapview;
//  populate();
 }
 
 public void addOverlay(OverlayItem overlay)
 {
  mOverlays.add(overlay);
  populate();
  view.invalidate(); 
 }
 
 @Override
 protected boolean onTap(int index) {
   // TODO Auto-generated method stub
   Log.d("Item Remove", String.valueOf(index));
  
   mOverlays.remove(getItem(index));
//   view.getOverlays().remove(getItem(index));
//   view.invalidate();
  return true;
 }
 
 @Override
 public boolean onTap(GeoPoint p, MapView mapView) {
  // TODO Auto-generated method stub
  boolean tapped = super.onTap(p, mapView); 
  
  if(!tapped)
  {
  markpoint = p;
  
   if(longTouch == true)
   {
    Drawable logo = mContext.getResources().getDrawable(R.drawable.android);
    
    logo.setBounds(0, 0, logo.getIntrinsicWidth(),
      logo.getIntrinsicHeight());
    
    
    OverlayItem overlayitem = new OverlayItem(p, "안녕", "서울");
  
    addOverlay(overlayitem);
    Log.d("touch", "add marker");
    longTouch = false;
   }
  }
  return true;
 }
 @Override
 protected OverlayItem createItem(int i) {
  // TODO Auto-generated method stub
  return mOverlays.get(i); }

 @Override
 public int size() {
  // TODO Auto-generated method stub
  return mOverlays.size(); 
 }
 @Override
 public boolean onDown(MotionEvent e) {
  // TODO Auto-generated method stub
  return false;
 }
 @Override
 public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
   float velocityY) {
  // TODO Auto-generated method stub
  return false;
 }
 @Override
 public void onLongPress(MotionEvent e) {
  // TODO Auto-generated method stub
  Log.d("touch", "onShowPress");
  /*
  Drawable logo = mContext.getResources().getDrawable(R.drawable.android);
  
  logo.setBounds(0, 0, logo.getIntrinsicWidth(),
    logo.getIntrinsicHeight());
  
  
  OverlayItem overlayitem = new OverlayItem(markpoint, "안녕", "서울");  addOverlay(overlayitem);
  */
  longTouch = true;
 }
 @Override
 public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
   float distanceY) {
  // TODO Auto-generated method stub
  return false;
 }
 @Override
 public void onShowPress(MotionEvent e) {
  // TODO Auto-generated method stub }
 @Override
 public boolean onSingleTapUp(MotionEvent e) {
  // TODO Auto-generated method stub
  return false;
 }
 @Override
 public boolean onTouchEvent(MotionEvent event, MapView mapView) {
  // TODO Auto-generated method stub  return mGestureDetector.onTouchEvent(event);
 }
}

 

ItemizedOverlay을 상속해서 제스쳐를 이용해서 롱터치시 마커를 찍고
마커를 클릭하면 ontap(int)에서 해당 마커를 지우는 프로그램입니다.

문제점이 마커를 삭제할때 엉뚱한 마커가 화면상에서 제거가 됩니다.
다시 롱터치로 다른마커를 생성하면 정확히 다시 보입니다.

예를 들면 그림에 맨위 마커를 제거 하면 가운데 마커가 제거 됩니다.
이상황에서 다른곳에 마커를 생성하면 맨위 마커가 제거되고, 새로운 마커가 그려집니다.

해결방법을 파악하기 어려워서 이렇게 글을 남겨봅니다.
device.png