5

でAndroidアプリを作成してい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

onTouchEvents のプロシージャを on touch に追加し、MapView をグローバル変数として設定すると、オーバーライドされた onTOuch Event で Mapview 変数を呼び出すことができると思います。

私が見た限りでは、onTouchEvents を開始するための接続はありません。

于 2012-09-29T01:21:39.087 に答える