2

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

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

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

有人有什么想法吗?

问候!

4

2 回答 2

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++;
}
于 2013-05-09T08:28:49.983 回答
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);
        }
    });
}
于 2013-05-08T18:34:59.423 回答