2013-05-06 128 views
2

使用OSMDroid lib創建OpenStreetMaps應用程序。我需要LongPress手勢將標記添加到OverlayItem數組中以將它們全部顯示出來,但我無法訪問LongPress。OSMDroid映射上的LongPress不起作用

我已經嘗試過使用GestureListener重寫onTouch事件,使用GestureDetector(現在已棄用),並且找不到此問題的解決方案。

我已經得到了手勢唯一途徑是通過setOnLongClickListener,但我有長按或平移地圖之間作出選擇,我很貪心,我希望兩個:P

任何人有任何想法?

問候!

回答

1

我不得不通過OSMBonusPack庫遇到一個解決方案。

對象MapsEventsReceiver授予您處理地圖手勢的簡單方法。 您必須覆蓋singleTapUpHelperlongPressHelper方法,並使用此處理程序創建新的覆蓋圖。

下面的代碼:

MapEventsReceiver mReceive = new MapEventsReceiver() { 

    @Override 
    public boolean singleTapUpHelper(IGeoPoint arg0) { 
    Log.d("debug", "Single tap helper"); 
    //your onSingleTap logic here 
    return false; 
    } 

    @Override 
    public boolean longPressHelper(IGeoPoint arg0) { 
    Log.d("debug", "LongPressHelper"); 
    //your onLongPress logic here 
    setMarker(arg0); 
    return false; 
    } 
}; 

//Creating a handle overlay to capture the gestures 
MapEventsOverlay OverlayEventos = new MapEventsOverlay(getBaseContext(), mReceive); 
map.getOverlays().add(OverlayEventos); 

//Refreshing the map to draw the new overlay 
map.invalidate(); 


//I use this method to set the marker on touchPoint 
    protected void setMarker(IGeoPoint arg0) { 

touchPoint = (GeoPoint) arg0; 
targetPoint = touchPoint; 
path.addPoint(targetPoint); 

//Listener to handle item's(markers) events 
myOnItemGestureListener = new OnItemGestureListener<OverlayItem>() { 

    @Override 
    public boolean onItemLongPress(int arg0, OverlayItem arg1) { 
    Log.d("debug", "Testing long tap on item"); 
    //your item onLongPress logic here 
     return false; 
    } 

    @Override 
    public boolean onItemSingleTapUp(int index, OverlayItem item) { 
    Log.d("debug", "Testing single tap on item"); 
    //your item onSingleTap logic here 
     return true; 
    } 
}; 

itemarray.add(new OverlayItem("Marker " + cnt, "Info about marker " + cnt, touchPoint)); 

ItemizedOverlayWithFocus<OverlayItem> overlayDeItems = new ItemizedOverlayWithFocus<OverlayItem>(getBaseContext(), itemarray, myOnItemGestureListener); 
map.getOverlays().add(overlayDeItems); 

overlayDeItems.setFocusItemsOnTap(true); 
map.invalidate(); 
itemarray = new ArrayList<OverlayItem>(); 
    cnt++; 
} 
0
private void setupGestureListener() { 
    GestureDetector.SimpleOnGestureListener gestureListener = new GestureDetector.SimpleOnGestureListener() { 

     @Override 
     public void onLongPress(MotionEvent e) { 
      //do something 
     } 
    }; 

    final GestureDetector gestureDetector = new GestureDetector(getContext(), gestureListener); 
    setOnTouchListener(new OnTouchListener() { 
     @Override 
     public boolean onTouch(View view, MotionEvent motionEvent) { 
     return gestureDetector.onTouchEvent(motionEvent); 
     } 
    }); 
} 
+0

我已經GestureDetector和GestureListener受審。我在源代碼上沒有錯誤,但我沒有從logcat的事件得到響應。 – leonis 2013-05-09 08:26:11

+0

你把代碼放在哪裏?我有一個擴展MapView的類。 – manimaul 2013-05-17 17:11:38