MapboxMap目前有2中方式对地图截图
第一种:
mapboxMap.snapshot(new MapboxMap.SnapshotReadyCallback() {
@Override
public void onSnapshotReady(@NonNull Bitmap snapshot) {
}
});
这个Bitmap是对当前MapView的快照,也就是包含添加的layer等的截图
第二种 : 使用MapSnapshotter
看下MapSnapshotter的初始化方法
/**
* 创建地图快照程序,但不开始渲染或正在加载。
*
* @param context 上下文
* @param options 用于快照的选项 ,这个options是它的内部类,包含各种比较细化的配置
*/
public MapSnapshotter(@NonNull Context context, @NonNull Options options) {
}
//调用开始方法
mapSnapshotter.start(new MapSnapshotter.SnapshotReadyCallback() {
@Override
public void onSnapshotReady(MapSnapshot snapshot) {
Bitmap bitmapOfMapSnapshotImage = snapshot.getBitmap();
}
});
下面演示2种对添加一个Layer地图的截图
第一种:
第二种:
源码:
SnapshotMapActivity
public class SnapshotMapActivity extends AppCompatActivity {
private final static String GEO_SOURCE_ID = "geo_source_id";
private final static String LAYER_ID = "layer_id";
private final static String SINGLE_IMAGE_ID = "single_image_id";
private ImageView iv_snapshot;
private Button btn_clear;
private Button buttonOld;
private Button buttonNew;
private MapView mapView;
private MapboxMap mapboxMap;
private MapSnapshotter mapSnapshotter;
private boolean hasStartedSnapshotGeneration;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Mapbox.getInstance(this, getString(R.string.mapbox_access_token));
setContentView(R.layout.activity_snapshot);
hasStartedSnapshotGeneration = false;
iv_snapshot = findViewById(R.id.iv_snapshot);
btn_clear = findViewById(R.id.btn_clear);
buttonOld = findViewById(R.id.btn_old);
buttonNew = findViewById(R.id.btn_new);
mapView = findViewById(R.id.mapView);
mapView.onCreate(savedInstanceState);
mapView.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(@NonNull final MapboxMap mapboxMap) {
SnapshotMapActivity.this.mapboxMap = mapboxMap;
mapboxMap.setStyle(Style.MAPBOX_STREETS, new Style.OnStyleLoaded() {
@Override
public void onStyleLoaded(@NonNull Style style) {
//添加一个layer
addSingleLayer(style, mapboxMap);
//当用户点击按钮时,清除截图
btn_clear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!hasStartedSnapshotGeneration) {
if (iv_snapshot != null) {
iv_snapshot.setImageDrawable(new ColorDrawable(Color.TRANSPARENT));
}
}
}
});
//当用户点击按钮时,拍摄地图快照
buttonOld.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!hasStartedSnapshotGeneration) {
hasStartedSnapshotGeneration = true;
Toast.makeText(SnapshotMapActivity.this, "loading old api...", Toast.LENGTH_LONG).show();
startOldSnapShot();
}
}
});
//当用户单击按钮时,使用给定的参数开始快照过程
buttonNew.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (!hasStartedSnapshotGeneration) {
hasStartedSnapshotGeneration = true;
Toast.makeText(SnapshotMapActivity.this, "loading new api...", Toast.LENGTH_LONG).show();
startNewSnapShot(
mapboxMap.getProjection().getVisibleRegion().latLngBounds,
mapView.getMeasuredHeight(),
mapView.getMeasuredWidth());
}
}
});
}
});
}
});
}
private void addSingleLayer(@NonNull Style style, @NonNull MapboxMap mapboxMap) {
GeoJsonSource geoJsonSource = new GeoJsonSource(GEO_SOURCE_ID);
LatLng target = mapboxMap.getCameraPosition().target;
geoJsonSource.setGeoJson(Point.fromLngLat(target.getLongitude(), target.getLatitude()));
style.addSource(geoJsonSource);
style.addImage(SINGLE_IMAGE_ID, getResources().getDrawable(R.drawable.mapbox_marker_icon_default));
style.addLayer(new SymbolLayer(LAYER_ID, GEO_SOURCE_ID)
.withProperties(
iconImage(SINGLE_IMAGE_ID),
iconAnchor(Property.ICON_ANCHOR_BOTTOM),
iconIgnorePlacement(true),
iconAllowOverlap(true)
));
}
/**
* 拍摄地图快照
*/
private void startOldSnapShot() {
mapboxMap.snapshot(new MapboxMap.SnapshotReadyCallback() {
@Override
public void onSnapshotReady(@NonNull Bitmap snapshot) {
if (iv_snapshot != null) {
iv_snapshot.setImageBitmap(snapshot);
}
hasStartedSnapshotGeneration = false;
}
});
}
/**
* 根据给定的参数创建位图,并使用该位图创建通知
*
* @param latLngBounds of map
* @param height of map
* @param width of map
*/
private void startNewSnapShot(LatLngBounds latLngBounds, int height, int width) {
mapboxMap.getStyle(new Style.OnStyleLoaded() {
@Override
public void onStyleLoaded(@NonNull Style style) {
if (mapSnapshotter == null) {
//使用地图尺寸和给定范围初始化快照程序
MapSnapshotter.Options options =
new MapSnapshotter.Options(width, height)
.withRegion(latLngBounds)
.withCameraPosition(mapboxMap.getCameraPosition())
.withStyle(style.getUri());
mapSnapshotter = new MapSnapshotter(SnapshotMapActivity.this, options);
} else {
//重复使用现有的MapSnapshotter实例
mapSnapshotter.setSize(width, height);
mapSnapshotter.setRegion(latLngBounds);
mapSnapshotter.setCameraPosition(mapboxMap.getCameraPosition());
}
mapSnapshotter.start(new MapSnapshotter.SnapshotReadyCallback() {
@Override
public void onSnapshotReady(MapSnapshot snapshot) {
Bitmap bitmapOfMapSnapshotImage = snapshot.getBitmap();
if (iv_snapshot != null) {
iv_snapshot.setImageBitmap(bitmapOfMapSnapshotImage);
}
hasStartedSnapshotGeneration = false;
}
});
}
});
}
@Override
public void onResume() {
super.onResume();
mapView.onResume();
}
@Override
protected void onStart() {
super.onStart();
mapView.onStart();
}
@Override
protected void onStop() {
super.onStop();
mapView.onStop();
}
@Override
public void onPause() {
super.onPause();
mapView.onPause();
//如果存在快照,将其暂停
if (mapSnapshotter != null) {
mapSnapshotter.cancel();
}
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mapView.onSaveInstanceState(outState);
}
@Override
public void onLowMemory() {
super.onLowMemory();
mapView.onLowMemory();
}
@Override
protected void onDestroy() {
super.onDestroy();
mapView.onDestroy();
}
}
activity_snapshot.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:mapbox="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.mapbox.mapboxsdk.maps.MapView
android:id="@+id/mapView"
android:layout_width="match_parent"
android:layout_height="match_parent"
mapbox:mapbox_cameraTargetLat="37.536172"
mapbox:mapbox_cameraTargetLng="126.916954"
mapbox:mapbox_cameraTilt="60"
mapbox:mapbox_cameraZoom="9"
/>
<ImageView
android:id="@+id/iv_snapshot"
android:layout_width="160dp"
android:layout_height="200dp"
mapbox:layout_constraintBottom_toBottomOf="parent"
mapbox:layout_constraintStart_toStartOf="parent"/>
<Button
android:id="@+id/btn_old"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="截图Api旧"
android:layout_marginBottom="20dp"
mapbox:layout_constraintBottom_toTopOf="@+id/btn_new"
mapbox:layout_constraintEnd_toEndOf="parent" />
<Button
android:id="@+id/btn_new"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="截图Api新"
mapbox:layout_constraintBottom_toBottomOf="parent"
mapbox:layout_constraintEnd_toEndOf="parent" />
<Button
android:id="@+id/btn_clear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="清除截图"
android:layout_marginBottom="20dp"
mapbox:layout_constraintBottom_toTopOf="@+id/btn_old"
mapbox:layout_constraintEnd_toEndOf="parent" />
</android.support.constraint.ConstraintLayout>