Mapbox 创建可拖动marker

Mapbox 创建可拖动marker(Android)

mapbox的SDK没提供可拖动的marker,可把我坑惨了,网上找了许多都不能用,应该是因为sdk升级导致的,所以只能自己动手写一个了,这里用的版本是6.1.2:
关于MapView的初始化sdk里介绍的都很详细,这里就不讲了,进入主题:
我们在 mapView.getMapAsync()里:
1.保存MapboxMap实例
2.初始化Projection(用于获得坐标点和屏幕像素的映射关系)
3.设置mapView的触摸监听

//手机像素密度
float screenDensity = context.getResources().getDisplayMetrics().density;
 mapView.getMapAsync(new OnMapReadyCallback() {
            @Override
            public void onMapReady(MapboxMap mapboxMap) {
                //保存MapboxMap实例
                map = mapboxMap;
                //.初始化Projection
                pj = map.getProjection();
                //设置mapView的触摸监听
                mapView.setOnTouchListener(new View.OnTouchListener() {
                    @Override
                    public boolean onTouch(View view, MotionEvent motionEvent) {
                        if(motionEvent!=null){
                            //手机触摸的位置
                            PointF tapPoint = new PointF(motionEvent.getX(), motionEvent.getY());
                            //左右偏移量
                            float toleranceSides = 4 * screenDensity;
                            //上下偏移量
                            float toleranceTopBottom = 10 * screenDensity;
                            //marker图标的长宽
                            float averageIconWidth = 60;
                            float averageIconHeight = 60;
                            float left = tapPoint.x - averageIconWidth / 2 - toleranceSides;
                            float top = tapPoint.y - averageIconHeight / 2 - toleranceTopBottom;
                            float right = tapPoint.x + averageIconWidth / 2 + toleranceSides;
                            float bottom = tapPoint.y + averageIconHeight / 2 + toleranceTopBottom;
                            //基于触摸位置生成一个矩形
                            RectF tapRect = new RectF(left , top, right , bottom );
                            //判断是否有marker在这个矩形内
                            boolean b = touchDeal(motionEvent, tapRect);
                            return b;
                        }
                        return false;

                    }
                });

            }
        });

touchDeal(),处理触摸事件,算了,看注释吧

    List allmarkers = new ArrayList<>();
    Projection pj;
    Marker moveMarker = null;
    List routeCoordinates = new ArrayList<>();
 private boolean touchDeal(MotionEvent event,RectF rectf){
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
            //获取地图上所有marker
                allmarkers = map.getMarkers();
                for (Marker marker:allmarkers) {
                    //获取marker坐标对应的像素位置
                    PointF pointF = pj.toScreenLocation(marker.getPosition());
                    //marker是否在矩形内
                    boolean contains = rectf.contains(pointF.x, pointF.y);
                    //在矩形内保存此marker,并消费触摸事件
                    if(contains){
                        moveMarker = marker;
                        return true;
                    }
                }
                moveMarker = null;
                break;
            case MotionEvent.ACTION_MOVE:
            //如果保存需要移动的marker容器不为空,消费触摸事件
                if(moveMarker!=null){
                    //获取当前触摸像素点
                    PointF tapPoint = new PointF(event.getX(), event.getY());
                    //将像素点转换为坐标
                    LatLng latLng = pj.fromScreenLocation(tapPoint);
                    //改变marker的位置
                    moveMarker.setPosition(latLng);
                    return true;
                }
                break;
            case MotionEvent.ACTION_UP:
                moveMarker = null;
                break;
            case MotionEvent.ACTION_CANCEL:
                moveMarker = null;
                break;

        }
        return false;
    }

至此完成,严格来说不是创建了可拖动marker,只是监听地图的触摸事件,如果触摸的地方有marker,就消费触摸事件,接下来的滑动就不断改变marker的位置。
附:小菜不知道如何使用mapbox画虚线,而且是一条可以随时刷新的虚线,用LineLayer画的虚线好像不能刷新,望有知道的大佬评论留言,感谢

你可能感兴趣的:(MapBox,mapbox,dragablemarker)