Android开发,MapBox的使用及部分功能实现(三)----- 重新理解MapBox

来源:互联网 发布:淘宝联盟ios历史版本 编辑:程序博客网 时间:2024/05/20 19:17

如果对于Mapbox的使用已经比较熟练的可以跳过本文。

本文是由于本人长时间没接触Mapbox,重新入手后,发现很多的东西的理解貌似没有以前那么深刻了,有些东西也和以前的理解有了偏差。

就比如我在第一章中提到的styleurl,我解释是地图风格,但是在仔细了解后,我发现并没有这么简单。

本文不讲解具体使用,主要是对各个参数,各个方法的重新理解。

部分内容可能与前面重复:

1.Camera

基础的camera移动---move、animate、ease之前已经提过,就不继续了。

(1)首先看看边界值移动

private static final LatLng Position1 = new LatLng(29.028575,107.225315);  private static final LatLng Position2 = new LatLng(29.5958354385,107.61124945318);LatLngBounds latLngBounds = new LatLngBounds.Builder().include(Position2).include(Position1).build()CameraUpdate update =      CameraUpdateFactory.newLatLngBounds(latLngBounds,        10,        10,        10,        10);    mapboxMap.moveCamera(update);
和普通的移动到某个点不同,该方法,是传入给定点,由地图计算比例尺、中心点、间隔padding,将所有的点都包含在地图的范围内。

(2)zoomin、zoomout、zoomby、zoomto

看着挺搅的。
我们从源码进去看看:


可以看到,这几个方法调用的都是用一个方法:CameraUpdateFactory.ZoomUpdate
    static final class ZoomUpdate implements CameraUpdate {        static final int ZOOM_IN = 0;        static final int ZOOM_OUT = 1;        static final int ZOOM_BY = 2;        static final int ZOOM_TO = 3;        static final int ZOOM_TO_POINT = 4;        private final int type;        private final double zoom;        private float x;        private float y;        ZoomUpdate(int type) {            this.type = type;            this.zoom = 0.0D;        }        ZoomUpdate(int type, double zoom) {            this.type = type;            this.zoom = zoom;        }        ZoomUpdate(double zoom, float x, float y) {            this.type = 4;            this.zoom = zoom;            this.x = x;            this.y = y;        }        public double getZoom() {            return this.zoom;        }        public int getType() {            return this.type;        }        public float getX() {            return this.x;        }        public float getY() {            return this.y;        }        double transformZoom(double currentZoom) {            switch(this.getType()) {            case 0:                ++currentZoom;                break;            case 1:                --currentZoom;                if(currentZoom < 0.0D) {                    currentZoom = 0.0D;                }                break;            case 2:                currentZoom += this.getZoom();                break;            case 3:                currentZoom = this.getZoom();                break;            case 4:                currentZoom += this.getZoom();            }            return currentZoom;        }        public CameraPosition getCameraPosition(@NonNull MapboxMap map) {            CameraPosition cameraPosition = map.getCameraPosition();            return this.getType() != 4?(new Builder(cameraPosition)).zoom(this.transformZoom(cameraPosition.zoom)).build():(new Builder(cameraPosition)).zoom(this.transformZoom(cameraPosition.zoom)).target(ZXMap.getProjection().fromScreenLocation(new PointF(this.getX(), this.getY()))).build();        }    }

可以看到构造方法其实区别不大。
主要看到下面的判断:
double transformZoom(double currentZoom) {            switch(this.getType()) {            case 0:                ++currentZoom;                break;            case 1:                --currentZoom;                if(currentZoom < 0.0D) {                    currentZoom = 0.0D;                }                break;            case 2:                currentZoom += this.getZoom();                break;            case 3:                currentZoom = this.getZoom();                break;            case 4:                currentZoom += this.getZoom();            }            return currentZoom;        }
这里就比较清楚了:
zoomin就是在当前的基础上zoom值+1
zoomout就是在当前的基础上zoom值-1
zoomby就是在当前的基础上加上固定的值,比如zoomby(2)就是连续+2
zoomto就是直接缩放到指定比例尺

2.queryRenderedFeatures

@UiThread    @NonNull    public List<Feature> queryRenderedFeatures(@NonNull PointF coordinates, @Nullable String... layerIds) {        return this.nativeMapView.queryRenderedFeatures(coordinates, layerIds, (Statement)null);    }    @UiThread    @NonNull    public List<Feature> queryRenderedFeatures(@NonNull PointF coordinates, @Nullable Statement filter, @Nullable String... layerIds) {        return this.nativeMapView.queryRenderedFeatures(coordinates, layerIds, filter);    }    @UiThread    @NonNull    public List<Feature> queryRenderedFeatures(@NonNull RectF coordinates, @Nullable String... layerIds) {        return this.nativeMapView.queryRenderedFeatures(coordinates, layerIds, (Statement)null);    }    @UiThread    @NonNull    public List<Feature> queryRenderedFeatures(@NonNull RectF coordinates, @Nullable Statement filter, @Nullable String... layerIds) {        return this.nativeMapView.queryRenderedFeatures(coordinates, layerIds, filter);    }

查询方法有如下四种方式。
可以分为两类,一类是用屏幕点查询,也就是PointF,注意这是屏幕点,不是坐标点
一类是根据范围查询,也就是RectF,用过自定义绘制的可能知道,Rect代表的不仅仅是一个点,而是一个范围,使用这个的好处是,当你想要查询的是一条线的feature的时候,你无法刚好通过Pointf点到那一个点,你就需要用到RectF了。
每一类又可以区分是否使用过滤器,可以过滤部分条件,比如高度大于多少的feature。
另外还需要注意一点就是,这里layerid的传入类型是可变长度类型,String...,也就是:
queryRenderedFeatures(pointf, "123","abc","qwe")
这种可以一直添加的参数类型,但是如果你需要传的参数很多,而且是无法通过一个个传入的方式调用的,这里位置,你可以传入一个String[],因为java规定,String...是可以接收String[]类型的参数的。

查询结果生成一个Feature的集合。

通过这个Feature,你可以获取到该feature的polygon,范围,等信息。


3.SupportMapFragment

SupportMapFragment是mapboxmap用于使用者在fragment中建立mapview(当然我指的是动态建立,而非xml建立,xml建立和activity是一样的)
public class SupportMapFragment extends Fragment {    private MapView map;    private OnMapReadyCallback onMapReadyCallback;    public SupportMapFragment() {    }    public static SupportMapFragment newInstance() {        return new SupportMapFragment();    }    public static SupportMapFragment newInstance(@Nullable MapBoxOptions mapBoxOptions) {        SupportMapFragment mapFragment = new SupportMapFragment();        mapFragment.setArguments(MapFragmentUtils.createFragmentArgs(mapBoxOptions));        return mapFragment;    }    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {        super.onCreateView(inflater, container, savedInstanceState);        Context context = inflater.getContext();        return this.map = new MapView(context, MapFragmentUtils.resolveArgs(context, this.getArguments()));    }    public void onViewCreated(View view, Bundle savedInstanceState) {        super.onViewCreated(view, savedInstanceState);        this.map.onCreate(savedInstanceState);    }     ...    ...        public void getMapAsync(@NonNull OnMapReadyCallback onMapReadyCallback) {        this.onMapReadyCallback = onMapReadyCallback;    }
其实该方法和普通的actvity创建没有太大的区别,我就不具体说了。
使用如下:
MapboxOptions options = new MapboxOptions ();        options.baseStyle("http://zhsq.digitalcq.com/D2CJson/ZHSQDT.json");      options.debugActive(false);      options.compassEnabled(false);      options.attributionEnabled(false);      options.logoEnabled(false);      LatLng dc = new LatLng(29.74352417,106.624274542043);      options.minZoomPreference(9);      options.maxZoomPreference(11);      options.camera(new CameraPosition.Builder()        .target(dc)        .zoom(11)        .build());      mapFragment = SupportMapFragment.newInstance(options);      transaction.add(R.id.fragment_container, mapFragment, "com.zxmap.map");      transaction.commit();



阅读全文
0 0
原创粉丝点击