2011-11-02 86 views
2

我一直在嘗試實現OnTouchEvent,因此當用戶移動(拖動&拖放)overlayitem時,我可以做出反應。無法使用Osmdroid實現onTouchEvent(拖放)

我在Osmdroid站點發現了類似的東西:http://code.google.com/p/osmdroid/issues/detail?id=225。我在那裏使用代碼作爲參考,但仍然不適合我,然後我的應用程序在碰到hitTest時崩潰。

有什麼建議嗎?謝謝。

public boolean onTouchEvent(MotionEvent event, MapView mapView) { 

    final int action = event.getAction(); 
    final int x = (int) event.getX(); 
    final int y = (int) event.getY(); 

    final Projection pj = mapView.getProjection(); 

    boolean result = false; 
    //Object TAG; 
    //Log.d(TAG, "onTouchEvent entered"); 

    System.out.println("onTouchEvent!");   
    Point p = new Point(0,0); 
    Point t = new Point(0,0); 

    //System.out.print(MotionEvent.ACTION_DOWN); 
    //System.out.print(action); 

    if (action == MotionEvent.ACTION_DOWN) { 

     System.out.println("Action Down!"); 

     for (OverlayItem item : mOverlays) { 

      // Create a new GeoPoint from pixel coordinates (x, y, pointReuse): 
      //pj.fromMapPixels(x, y, t); 

      // Convert the given GeoPoint to onscreen pixel coordinates (GeoPoint, pointOut): 
      pj.toPixels(item.getPoint(), p); 

      //System.out.println(t.x); 
      //System.out.println(p.x); 

      //System.out.println(t.y); 
      //System.out.println(p.y); 

      defaultMarker = item.getDrawable(); 

      if (hitTest(item, defaultMarker, x - p.x, y - p.y)) { 
       System.out.println("Action Down -> IF!"); 
       result = true; 
       inDrag = item; 
       mOverlays.remove(inDrag); 
       populate(); 

       xDragTouchOffset = 0; 
       yDragTouchOffset = 0; 

       setDragImagePosition(x, y); 
       dragImage.setVisibility(View.VISIBLE); 

       xDragTouchOffset = t.x - p.x; 
       yDragTouchOffset = t.y - p.y; 

       break; 
      } 
     } 

    } 

    else if (action == MotionEvent.ACTION_MOVE && inDrag != null) { 
     //dragImage.setVisibility(View.VISIBLE); 
     setDragImagePosition(x, y); 
     System.out.println("Action Move!"); 
     result = true; 
    } 

    else if (action == MotionEvent.ACTION_UP && inDrag != null) { 
     dragImage.setVisibility(View.GONE); 

     GeoPoint pt = (GeoPoint) pj.fromPixels(x - xDragTouchOffset, y - yDragTouchOffset); 
     OverlayItem toDrop = new OverlayItem(inDrag.getTitle(), 
       inDrag.getSnippet(), pt); 

     mOverlays.add(toDrop); 
     populate(); 

     inDrag = null; 
     result = true; 

     pj.fromMapPixels(x, y, t); 

     if((t.x - p.x) == xDragTouchOffset && (t.y - p.y) == yDragTouchOffset) { 
      System.out.println ("Do something here if desired because we didn't move item " + toDrop.getTitle()); 
     } 

     System.out.println("Action Up!"); 

    } 
    System.out.print(inDrag); 
    return (result || super.onTouchEvent(event, mapView)); 
} 

private void setDragImagePosition(int x, int y) { 
    RelativeLayout.LayoutParams lp= 
      (RelativeLayout.LayoutParams)dragImage.getLayoutParams(); 
    lp.setMargins(x-xDragImageOffset-xDragTouchOffset, 
      y-yDragImageOffset-yDragTouchOffset, 0, 0); 
    dragImage.setLayoutParams(lp); 
    } 

回答

2

我得到了上面的代碼工作。我只注意到我的工作代碼和你的兩件事情不同。一個是你要發送的則hitTest PARAMATERS,嘗試改變

if (hitTest(item, defaultMarker, x - p.x, y - p.y)) {

if (hitTest(item, defaultMarker, t.x - p.x, t.y - p.y)) {

第二個是,你應該取消註釋pj.fromMapPixels(x, y, t);

還你或許應該取消註釋dragImage.setVisibility(View.VISIBLE);如果您想要在拖動時看到圖像