OSMDroid地图上的LongPress无法正常工作

时间:2013-05-06 16:11:26

标签: java android openstreetmap osmdroid

我正在使用OSMDroid lib创建一个OpenStreetMaps应用程序。我需要LongPress手势将标记添加到OverlayItem数组中以显示所有内容,但无论如何我都无法访问LongPress。

我尝试使用GestureListener,使用GestureDetector(现在已弃用)覆盖了onTouch事件,我找不到解决此问题的方法。

我获得手势的唯一方法是通过setOnLongClickListener,但我必须在长按或平移地图之间做出选择,我很贪婪,我想要两个:p

有任何想法吗?

问候!

2 个答案:

答案 0 :(得分: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++;
}

答案 1 :(得分: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);
        }
    });
}