Fix onStyleImageMissing not fired in Snapshotter (#317)

* Modify example to trigger OnStyleMissing callback

* Fix onStyleImageMissing callback not works in snapshotter.

* Update gl-native

* Update doc

* Fix android check
This commit is contained in:
Kevin Li
2020-04-14 17:58:23 +08:00
committed by GitHub
parent 414e155af3
commit 7ca1bcf821
4 changed files with 42 additions and 12 deletions

View File

@@ -108,9 +108,18 @@ public class MapSnapshotter {
public interface Observer {
/**
* Called when snapshotter finishes loading it's style.
* Called when snapshotter finishes loading its style.
*/
void onDidFinishLoadingStyle();
/**
* Called when the map is missing an icon.The icon should be added synchronously with
* {@link MapSnapshotter#addImage(String, Bitmap, boolean)} to be rendered on the current zoom level.
* When loading icons asynchronously, you can load a placeholder image and replace it when you icon has loaded.
*
* @param imageName the id of the icon that is missing
*/
void onStyleImageMissing(String imageName);
}
/**
@@ -513,7 +522,7 @@ public class MapSnapshotter {
* @param bitmap the pre-multiplied Bitmap
* @param sdf the flag indicating image is an SDF or template image
*/
private void addImage(@NonNull final String name, @NonNull Bitmap bitmap, boolean sdf) {
public void addImage(@NonNull final String name, @NonNull Bitmap bitmap, boolean sdf) {
nativeAddImages(new Image[] {toImage(new Style.Builder.ImageWrapper(name, bitmap, sdf))});
}
@@ -797,7 +806,9 @@ public class MapSnapshotter {
*/
@Keep
protected void onStyleImageMissing(String imageName) {
onSnapshotFailed("style image is missing: " + imageName);
if (observer != null) {
observer.onStyleImageMissing(imageName);
}
}
private void checkThread() {

View File

@@ -138,7 +138,8 @@ public class MapSnapshotterActivity extends AppCompatActivity {
// marker source
FeatureCollection markerCollection = FeatureCollection.fromFeatures(new Feature[] {
Feature.fromGeometry(Point.fromLngLat(4.91638, 52.34673), featureProperties("2", "Car"))
Feature.fromGeometry(Point.fromLngLat(4.91638, 52.35673), featureProperties("1", "Android")),
Feature.fromGeometry(Point.fromLngLat(4.91638, 12.34673), featureProperties("2", "Car"))
});
Source markerSource = new GeoJsonSource(MARKER_SOURCE, markerCollection);
@@ -156,17 +157,32 @@ public class MapSnapshotterActivity extends AppCompatActivity {
builder.withImage("Car", Objects.requireNonNull(carBitmap), false)
.withSources(markerSource)
.withLayers(markerSymbolLayer);
options.withCameraPosition(new CameraPosition.Builder()
.target(new LatLng(5.537109374999999,
52.07950600379697))
.zoom(1)
.padding(1, 1, 1, 1)
.build()
);
options
.withRegion(null)
.withCameraPosition(new CameraPosition.Builder()
.target(new LatLng(5.537109374999999,
52.07950600379697))
.zoom(1)
.padding(1, 1, 1, 1)
.build()
);
}
options.withStyleBuilder(builder);
MapSnapshotter snapshotter = new MapSnapshotter(MapSnapshotterActivity.this, options);
snapshotter.setObserver(new MapSnapshotter.Observer() {
@Override
public void onDidFinishLoadingStyle() {
Timber.i("onDidFinishLoadingStyle");
}
@Override
public void onStyleImageMissing(String imageName) {
Bitmap androidIcon = BitmapUtils.getBitmapFromDrawable(getResources().getDrawable(R.drawable.ic_android_2));
snapshotter.addImage(imageName, androidIcon, false);
}
});
snapshotter.start(snapshot -> {
Timber.i("Got the snapshot");
ImageView imageView = new ImageView(MapSnapshotterActivity.this);

View File

@@ -48,6 +48,9 @@ class MapSnapshotterWithinExpression : AppCompatActivity() {
}
private val snapshotterObserver = object : MapSnapshotter.Observer {
override fun onStyleImageMissing(imageName: String?) {
}
override fun onDidFinishLoadingStyle() {
// Show only POI labels inside geometry using within expression
(snapshotter.getLayer("poi-label") as SymbolLayer).setFilter(