5

我正在使用MapView. 我需要对用户的触摸事件做出反应。我尝试在 onTouchEvent使用中处理一些操作dispatchTouchEvent。但这onTouchEvent不叫!当我添加此代码时dispatchTouchEvent

      MarkerOverlay myMarker = new MarkerOverlay(marker,edTxtAlarmClockName.getText().toString());        
      mapView.getOverlays().add(myMarker);
      mapView.invalidate();

onTouchEvent叫做。为什么会这样以及如何描述overlay处理程序?

public class MainActivity extends MapActivity implements OnTouchListener{

private MapController mapController;
private MapView mapView;
private LocationManager locationManager;
private MyLocationOverlay myLocationOverlay;
private Context context;
public GeoPoint lastClickedPoint;
Dialog dialog;
EditText edTxtAlarmClockName;
ToggleButton tbOnOffAlarmClock;
boolean reClick ;
private int lastTouchX, lastTouchY; 
private boolean hasMoved = false; 
Button btnOk;
Button btnCancelDelete;
Button btnAlarmClockStop;
public int distance=50;
private int indexOfSelectedOverlay;

public void onCreate(Bundle bundle) {
  super.onCreate(bundle);

  setContentView(R.layout.activity_main);   
  context = MainActivity.this;

  // Configure the Map
  mapView = (MapView) findViewById(R.id.mapview);
  mapView.setBuiltInZoomControls(true);
  mapView.setStreetView(true);
  mapView.setSatellite(true);
  mapView.setOnTouchListener(this);

  mapController = mapView.getController();
  mapController.setZoom(15); 

  locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
  locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 60000, 10, geoupdate);

  myLocationOverlay = new MyLocationOverlay(this, mapView);
  myLocationOverlay.disableCompass();
  myLocationOverlay.enableMyLocation();
  mapView.getOverlays().add(myLocationOverlay);
  myLocationOverlay.runOnFirstFix(new Runnable() {
  public void run() {
    mapView.getController().animateTo(myLocationOverlay.getMyLocation());
    mapView.setBuiltInZoomControls(true);
  }
});


final Drawable drawable = this.getResources().getDrawable(R.drawable.ic_launcher);
dialog  = new Dialog(context);
dialog.setContentView(R.layout.custom1);
dialog.setTitle(R.string.str_new_alarm);
tbOnOffAlarmClock=(ToggleButton)dialog.findViewById(R.id.toggleButton1);
edTxtAlarmClockName = (EditText) dialog.findViewById(R.id.textView1);
edTxtAlarmClockName.setText(R.string.str_name);
btnOk = (Button) dialog.findViewById(R.id.btn_ok);
btnCancelDelete = (Button) dialog.findViewById(R.id.btn_cancel);
btnOk.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
         MarkerOverlay myMarker;
        if (reClick) //click on the overlay
        {//search and rename
             myMarker=(MarkerOverlay)mapView.getOverlays().get(indexOfSelectedOverlay);             
             mapView.getOverlays().remove(indexOfSelectedOverlay);
             myMarker.setName(edTxtAlarmClockName.getText().toString());
             mapView.getOverlays().add(myMarker);  
             mapView.invalidate();
        }
        else
        {//creat overlay
         myMarker = new MarkerOverlay(drawable,edTxtAlarmClockName.getText().toString()); 
         myMarker.setName(edTxtAlarmClockName.getText().toString());
         if (tbOnOffAlarmClock.isChecked()) {
             myMarker.active=true;
         } else {
             myMarker.active=false;
         }
         mapView.getOverlays().add(myMarker);  
         mapView.invalidate();  
        }
        dialog.dismiss();           
    }
});


btnCancelDelete.setOnClickListener(new View.OnClickListener() {     
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
         OverlayItem item; 
         Point itemPoint,currentPoint;
         MarkerOverlay myOverlay;
        if (reClick) //delete marker
        {
            Log.d("debug","delete");
             mapView.getOverlays().remove(indexOfSelectedOverlay);
             mapView.invalidate();

        }
        //else CANCEL
        dialog.dismiss();   
    }
});

}


@Override
protected boolean isRouteDisplayed() {
  return false;
}

@Override
protected void onDestroy() {
    super.onDestroy();
    locationManager.removeUpdates(geoupdate);
}


@Override
protected void onResume() {
   super.onResume();
   myLocationOverlay.enableMyLocation();
   myLocationOverlay.enableCompass();
}

@Override
protected void onPause() {
  super.onPause();
  myLocationOverlay.disableMyLocation();
  myLocationOverlay.disableCompass();
}

public class MarkerOverlay extends ItemizedOverlay<OverlayItem>
{

  private Drawable marker = null; 
  private boolean active=true;
  private GeoPoint pointOfOverlay;
  private String customName;
  public  List<OverlayItem> items=new ArrayList<OverlayItem>();;
  public MarkerOverlay(Drawable marker,String name) 
  {
      super(boundCenterBottom(marker));
      this.marker=marker;                 
      OverlayItem item = new OverlayItem(lastClickedPoint, name,"");
      this.pointOfOverlay=lastClickedPoint;
      this.customName=name;
      items.add(item);
      populate();

  }


  public String getName()
  {
      return customName;
  }

  public void setName(String name)
  {
      customName=name;
  }
  public GeoPoint getPoint()
  {
      return pointOfOverlay;
  }

  @Override
  protected OverlayItem createItem(int i) 
  {
    return(items.get(i));
  }

  @Override
  public void draw(Canvas canvas, MapView mapView, boolean shadow) 
  {
    super.draw(canvas, mapView, shadow);
    boundCenterBottom(marker);   
  }

  @Override
  public int size() 
  {
    return(items.size());
  }
 @Override
    public boolean onTouchEvent(MotionEvent motionEvent, MapView mapView) { 
        boolean consumed = super.onTouchEvent(motionEvent,mapView); 
          int action = motionEvent.getAction();
          int size;
            switch (action) { 
            case MotionEvent.ACTION_DOWN: 
                lastTouchX =(int) motionEvent.getX(); 
                lastTouchY =(int) motionEvent.getY(); 
                hasMoved = false; 
                break; 
            case MotionEvent.ACTION_MOVE: 
                hasMoved = moved(motionEvent); 

                break; 
            case MotionEvent.ACTION_UP:         
                if (!moved(motionEvent)) 
                {
                 size = items.size();
                  if (size > 0)
                  {
                    OverlayItem item = items.get(size - 1);
                    Point p1 = mapView.getProjection().toPixels(item.getPoint(), null);
                      if (hitTest(item, marker, lastTouchX - p1.x, lastTouchY - p1.y))
                     {
                          dialog.setTitle(item.getTitle());
                          btnCancelDelete.setText(R.string.str_delete);
                          dialog.show();
                          reClick=true;
                     }
                     else
                     {
                          edTxtAlarmClockName.setText(R.string.str_name);
                          btnCancelDelete.setText(R.string.str_cancel);
                          dialog.show();                             
                          lastClickedPoint = mapView.getProjection().fromPixels((int) motionEvent.getX(), (int) motionEvent.getY());
                          reClick=false;
                     }
                     populate();
                    return true;
                  }                        
                  else
                  {
                      edTxtAlarmClockName.setText(R.string.str_name);
                      dialog.setTitle(R.string.str_new_alarm);
                      btnCancelDelete.setText(R.string.str_cancel);
                      dialog.show();                             
                      lastClickedPoint = mapView.getProjection().fromPixels((int) motionEvent.getX(), (int) motionEvent.getY());
                      reClick=false;
                  }
                  populate();
                  return true;
                }
            } 

            return false||consumed;
    } 


    private boolean moved(MotionEvent evt) { 
        Log.d("debug", "ontouchevent move");
        return hasMoved || 
            Math.abs(evt.getX() - lastTouchX) > 20.0 || 
            Math.abs(evt.getY() - lastTouchY) > 20.0; 
    } 
}


@Override
public boolean onTouch (View v, MotionEvent event)
{
      int action = event.getAction();

      if (action == MotionEvent.ACTION_UP)
      { 
          lastClickedPoint= mapView.getProjection().fromPixels( (int) event.getX(), (int) event.getY());
          return true;
      }
      return false;  

}
@Override
public boolean dispatchTouchEvent(MotionEvent motionEvent) {
 int action = motionEvent.getAction();
 Drawable marker = getResources().getDrawable(R.drawable.ic_launcher);
 switch(action) {
   case MotionEvent.ACTION_UP:
      MarkerOverlay myMarker = new MarkerOverlay(marker,edTxtAlarmClockName.getText().toString());        
      mapView.getOverlays().add(myMarker);
      mapView.invalidate();
   break;
  }
  return super.dispatchTouchEvent(motionEvent);
  }

}
4

1 回答 1

0

我认为您应该在将 MapView 作为全局变量的同时将 onTouchEvents 的过程添加到 on touch 中,然后您可以在覆盖的 onTOuch 事件中调用 Mapview 变量。

据我所见,没有启动 onTouchEvents 的连接

于 2012-09-29T01:21:39.087 回答