简体   繁体   English

Mapbox,MapSnapshotter.callback并非总是被调用

[英]Mapbox, MapSnapshotter.callback is not always invoked

I'm mainly using mapbox in my projet, but in one instance, I need to display the map in the recyclerView. 我主要在我的projet中使用mapbox,但是在一种情况下,我需要在recyclerView中显示地图。 To do so, I thought of using MapSnapshotter instead of the static Map Api since the user may not have connection. 为此,我考虑使用MapSnapshotter而不是静态Map Api,因为用户可能没有连接。

Unfortunately, when doing my testing, I can't get the MapSnapshotter.callback working properly. 不幸的是,在进行测试时,我无法正常运行MapSnapshotter.callback。 Sometime the image is loaded/created and other time it's not, and it does feel to be random. 有时加载/创建图像,有时则不加载,并且确实感觉是随机的。

 Mapbox.getInstance(this, MyMapbox.getToken());
 mapView.onCreate(savedInstanceState);
 mapView.getMapAsync(new OnMapReadyCallback() {
        @Override
        public void onMapReady(MapboxMap mapboxMap) {
            map = mapboxMap;
            MapSnapshotter.Options options = new MapSnapshotter.Options(mapView.getMeasuredWidth(),mapView.getMeasuredHeight());
            options.withRegion(mapboxMap.getProjection().getVisibleRegion().latLngBounds);
            options.withStyle(mapboxMap.getStyleUrl());

            MapSnapshotter mapSnapshotter = new MapSnapshotter(getContext(), options);

            mapSnapshotter.start(new MapSnapshotter.SnapshotReadyCallback() {
                @Override
                public void onSnapshotReady(MapSnapshot snapshot) {
                    Log.i(LOG_TAG, "onSnapshotReady");
                    Bitmap bitmap = snapshot.getBitmap();
                    imageview.setImageBitmap(bitmap);
                }

            }, new MapSnapshotter.ErrorHandler() {
                @Override
                public void onError(String error) {
                    Log.i(LOG_TAG, error);
                }
            });
        }
    });

So after some twisting, I finally figure it out. 因此,经过一些曲折,我终于弄清楚了。
The issue, was that MapSnapshotter.start is a Async task and since the phone is loading 3 items in the recycler view when first launched and in turn, each one of the item is calling the MapSnapshotter.start in the same thread before the previous one is done, so canceling it. 问题在于,MapSnapshotter.start是一个异步任务,由于手机在首次启动时会在回收器视图中加载3个项目,因此,每个项目都在上一个项目之前的同一线程中调用MapSnapshotter.start。完成,因此取消它。 This explain why only the last item has is image loaded. 这就解释了为什么只有最后一项具有图像加载。

A way to solve this issue is to make the Async task become Sync, but I don't recommande this solution. 解决此问题的一种方法是使Async任务变为Sync,但我不建议使用此解决方案。

The others way is add a property MapSnapshotter in your Adapter. 另一种方法是在适配器中添加属性MapSnapshotter。 Doing so, each of yours items will have is own MapSnapshotter. 这样做,您的每个项目都将拥有自己的MapSnapshotter。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM