安卓:ItemizedOverlay的onTouchEvent和重叠的onTap [英] Android: ItemizedOverlay onTouchEvent and onTap overlapping

查看:173
本文介绍了安卓:ItemizedOverlay的onTouchEvent和重叠的onTap的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想,当用户选择绘制的地方在地图上叠加一个标记,然后present的对话框。问题是事件似乎重叠。当我点击地图上的标记画,和Ontap立即触发之后,因为我刚才所描绘的标记,它是直接和Ontap事件下,所以我的对话框始终闪光。有没有人对如何使这些事件互相排斥任何建议?

下面是code的地图活动:

 公共类SelectGameLocation扩展MapActivity实现GestureDetector.OnGestureListener,GestureDetector.OnDoubleTapListener {

    私有图形页面图形页面= NULL;
    私人SelectGameLocationItemizedOverlay selectLocationOverlay = NULL;
    私人名单,其中,叠加> mapOverlays = NULL;
    私人GestureDetector gestureDetector = NULL;

    @覆盖
    保护无效的onCreate(捆绑为arg0){
        super.onCreate(为arg0);

        //设置布局
        的setContentView(R.layout.activity_select_game_location);

        //为双击配置活动
        gestureDetector =新GestureDetector(本);
        gestureDetector.setOnDoubleTapListener(本);

        //创建和配置图形页面
        图形页面=(图形页面)findViewById(R.id.selectGameLocation);
        mapView.setBuiltInZoomControls(真正的);
        mapView.setHapticFeedbackEnabled(真正的);

        //配置覆盖绘制地图上的图标
        mapOverlays =调用MapView.getOverlays();
        可绘制绘制= this.getResources()getDrawable(R.drawable.map_icon)。
        selectLocationOverlay =新SelectGameLocationItemizedOverlay(绘制,这一点);
        mapOverlays.add(selectLocationOverlay);
    }

    @覆盖
    公共布尔dispatchTouchEvent(MotionEvent EV){
        gestureDetector.onTouchEvent(EV);
        返回super.dispatchTouchEvent(EV);
    }

    @覆盖
    公共布尔onDoubleTap(MotionEvent我){
        的GeoPoint P = GeoPointHelper.getPointClicked(箱,图形页面);
        。mapView.getController()animateTo(对);
        。mapView.getController()zoomIn();
        返回true;
    }

    //覆盖的方法,但不使用
    @覆盖
    公共布尔onDoubleTapEvent(MotionEvent我){
        返回false;
    }

    @覆盖
    公共布尔onDown(MotionEvent我){
        返回false;
    }

    @覆盖
    保护的布尔isRouteDisplayed(){
        返回false;
    }

    @覆盖
    公共无效OnShow中preSS(MotionEvent E){
    }

    @覆盖
    公共布尔onSingleTapConfirmed(MotionEvent我){
        返回false;
    }

    @覆盖
    公共布尔onScroll(MotionEvent E1,E2 MotionEvent,浮distanceX,浮distanceY){
        返回false;
    }

    @覆盖
    公共无效onLong preSS(MotionEvent E){
    }

    @覆盖
    公共布尔onFling(MotionEvent E1,E2 MotionEvent,浮velocityX,浮velocityY){
        返回false;
    }

    @覆盖
    公共布尔onSingleTapUp(MotionEvent我){
        返回false;
    }
}
 

和$ C $下覆盖类:

 公共类SelectGameLocationItemizedOverlay扩展ItemizedOverlay {

    私人上下文的背景下= NULL;
    私人列表< OverlayItem>覆盖=新的ArrayList< OverlayItem>();

    公共SelectGameLocationItemizedOverlay(可绘制标记,上下文语境){
        超级(boundCenterBottom(标记));
        this.context =背景;
    }

    @覆盖
    受保护的布尔中的onTap(INT指数){

        OverlayItem itemClicked = overlays.get(指数);
        AlertDialog.Builder对话框=新AlertDialog.Builder(上下文);
        dialog.setTitle(itemClicked.getTitle());
        dialog.setMessage(itemClicked.getSnippet());
        dialog.setCancelable(真正的);
        dialog.setPositiveButton(R.string.yes,新DialogInterface.OnClickListener(){
            公共无效的onClick(DialogInterface对话框,INT ID){
                Log.i(this.getClass()的getName(),选择是要添加位置);
                ((SelectGameLocation)上下文).finish();
            }
        });
        dialog.setNegativeButton(R.string.no,新DialogInterface.OnClickListener(){
            公共无效的onClick(DialogInterface对话框,INT ID){
                Log.i(this.getClass()的getName(),没有选定要添加位置);
                dialog.cancel();
            }
        });
        dialog.show();
        返回true;
    }

    @覆盖
    公共布尔的onTouchEvent(MotionEvent我,图形页面图形页面){
        drawMarker(GeoPointHelper.getPointClicked(我的MapView));
        返回super.onTouchEvent(箱,图形页面);
    }

    私人OverlayItem drawMarker(GeoPoint对象P){
            OverlayItem overlayitem =新OverlayItem(P,选择作为游戏的位置?,你想在这个位置可被添加为游戏中的位置?);
            getOverlays()清()。
            addOverlay(overlayitem);
            返回overlayitem;
    }

    公开名单< OverlayItem> getOverlays(){
        返回覆盖;
    }

    公共无效addOverlay(OverlayItem叠加){
        overlays.add(叠加);
        填充();
    }

    @覆盖
    保护OverlayItem createItem中(int i)以{
        返回overlays.get(ⅰ);
    }

    @覆盖
    公众诠释大小(){
        返回overlays.size();
    }

}
 

解决方案

我有完全一样的问题,我的onTouchEvent添加标记到地图,并希望它显示有关该标记信息,当用户点击它,它工作,但同时也增加了一个标记在它的上面。

所以,我所做的是无法使用的onTouchEvent而是尽一切中​​的onTap(INT指数)和中的onTap(GeoPoint对象磷,图形页面图形页面),像这样:

 公共布尔中的onTap(最终的GeoPoint磷,最终的MapView MapView类){
    布尔拍了拍= super.onTap(P,图形页面);
    如果(抽头){
        //你想要做什么,当你打一个项目
    }
    其他{
        //做你想做的事,当你不打一个项目是什么
        }
    返回true;
}

//返回true,以说,听众已采取正确的情况下考虑
@覆盖
受保护的布尔中的onTap(INT指数){
    返回true;
}
 

问候,

汤姆

I am trying to place a marker on a map overlay and then present a dialog when the user selects the drawable. The problem is the events seem to overlap. After I click the map and the marker is drawn, the onTap fires immediately afterwards, and because I have just drawn the marker, it is directly under the onTap event, so my dialog always fires. Does anyone have any suggestions on how to make these events mutually exclusive?

Here is the code for the map activity:

public class SelectGameLocation extends MapActivity implements GestureDetector.OnGestureListener, GestureDetector.OnDoubleTapListener  {

    private MapView mapView = null;
    private SelectGameLocationItemizedOverlay selectLocationOverlay = null;
    private List<Overlay> mapOverlays = null;
    private GestureDetector gestureDetector = null;

    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);

        //set the layout
        setContentView(R.layout.activity_select_game_location);

        //configure activity for double clicks
        gestureDetector = new GestureDetector(this);
        gestureDetector.setOnDoubleTapListener(this);

        //create and configure mapview
        mapView = (MapView) findViewById(R.id.selectGameLocation);
        mapView.setBuiltInZoomControls(true);
        mapView.setHapticFeedbackEnabled(true);

        //configure the overlay to draw the icons on the map
        mapOverlays = mapView.getOverlays();
        Drawable drawable = this.getResources().getDrawable(R.drawable.map_icon);
        selectLocationOverlay = new SelectGameLocationItemizedOverlay(drawable, this);
        mapOverlays.add(selectLocationOverlay);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        gestureDetector.onTouchEvent(ev);
        return super.dispatchTouchEvent(ev);
    }

    @Override
    public boolean onDoubleTap(MotionEvent me) {
        GeoPoint p = GeoPointHelper.getPointClicked(me, mapView);
        mapView.getController().animateTo(p);
        mapView.getController().zoomIn();
        return true;
    }

    //Overridden methods but not used
    @Override
    public boolean onDoubleTapEvent(MotionEvent me) {
        return false;
    }

    @Override
    public boolean onDown(MotionEvent me) {
        return false;
    }

    @Override
    protected boolean isRouteDisplayed() {
        return false;
    }

    @Override
    public void onShowPress(MotionEvent e) {
    }

    @Override
    public boolean onSingleTapConfirmed(MotionEvent me) {
        return false;
    }

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
        return false;
    }

    @Override
    public void onLongPress(MotionEvent e) {
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        return false;
    }

    @Override
    public boolean onSingleTapUp(MotionEvent me) {
        return false;
    }
}

and the code for the overlay class:

public class SelectGameLocationItemizedOverlay extends ItemizedOverlay {

    private Context context = null;
    private List<OverlayItem> overlays = new ArrayList<OverlayItem>();

    public SelectGameLocationItemizedOverlay(Drawable marker, Context context) {
        super(boundCenterBottom(marker));
        this.context = context;
    }

    @Override
    protected boolean onTap(int index) {

        OverlayItem itemClicked = overlays.get(index);
        AlertDialog.Builder dialog = new AlertDialog.Builder(context);
        dialog.setTitle(itemClicked.getTitle());
        dialog.setMessage(itemClicked.getSnippet());
        dialog.setCancelable(true);
        dialog.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                Log.i(this.getClass().getName(), "Selected Yes To Add Location");
                ((SelectGameLocation) context).finish();
            }
        });
        dialog.setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                Log.i(this.getClass().getName(), "Selected No To Add Location");
                dialog.cancel();
            }
        });
        dialog.show();
        return true;
    }

    @Override
    public boolean onTouchEvent(MotionEvent me, MapView mapView) {
        drawMarker(GeoPointHelper.getPointClicked(me, mapView));
        return super.onTouchEvent(me, mapView);
    }

    private OverlayItem drawMarker(GeoPoint p) {        
            OverlayItem overlayitem = new OverlayItem(p, "Select As Game Location?", "Do you want this location to be added as the location for the game?");
            getOverlays().clear();
            addOverlay(overlayitem);
            return overlayitem;
    }

    public List<OverlayItem> getOverlays() {
        return overlays;
    }

    public void addOverlay(OverlayItem overlay) {
        overlays.add(overlay);
        populate();
    }

    @Override
    protected OverlayItem createItem(int i) {
        return overlays.get(i);
    }

    @Override
    public int size() {
        return overlays.size();
    }

}

解决方案

I had exactly the same issue, my onTouchEvent added a marker to a map, and wanted it to show information about that marker when the user clicks on it, it worked but it also added another marker on top of it.

So what I did was not use onTouchEvent but do everything in onTap(int index) and onTap(Geopoint p, MapView mapView) like so:

public boolean onTap (final GeoPoint p, final MapView mapView){
    boolean tapped = super.onTap(p, mapView);
    if (tapped){            
        //do what you want to do when you hit an item           
    }           
    else{
        //do what you want to do when you DON'T hit an item
        }                   
    return true;
}

//Return true in order to say that the listener has correctly taken the event into account 
@Override
protected boolean onTap(int index) {
    return true;
}

Regards,

Tom

这篇关于安卓:ItemizedOverlay的onTouchEvent和重叠的onTap的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆