Issue
I'm mainly using mapbox in my projet, but in one instance, I need to display the map in the recyclerView. To do so, I thought of using MapSnapshotter instead of the static Map Api since the user may not have connection.
Unfortunately, when doing my testing, I can't get the MapSnapshotter.callback working properly. 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);
}
});
}
});
Solution
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. 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.
The others way is add a property MapSnapshotter in your Adapter. Doing so, each of yours items will have is own MapSnapshotter.
Answered By - Franck CHEN
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.