示例中心
功能在线体验
menu 导航 closed 关闭

地图截图功能

该示例向您展示如何对包含用户绘制数据的完整地图进行截图。比如包含标注点、覆盖物、信息窗口等信息的地图效果截图。
00:00 / 01:00
扫描二维码在手机端体验功能
体验移动端 扫码体验移动端

使用场景

对MapView以及其上的子view进行截图。

当用户在地图上绘制了标注点、覆盖物、信息窗口等数据时,利用该截屏功能可以实现包含地图和绘制数据的完整效果的截屏。

用到产品

Android 地图 SDK

核心类/接口

接口

说明

版本

AMap

public void getMapScreenShot(AMap.OnMapScreenShotListener listener)

截取当前屏幕设备上的可见地图区域。

V2.1.0版本起

核心难点

/**
     * 组装地图截图和其他View截图,需要注意的是目前提供的方法限定为MapView与其他View在同一个ViewGroup下
     *@param    bitmap             地图截图回调返回的结果
     *@param   viewContainer      MapView和其他要截图的View所在的父容器ViewGroup
     *@param   mapView            MapView控件
     *@param   views              其他想要在截图中显示的控件
     * */
public static Bitmap getMapAndViewScreenShot(Bitmap bitmap, ViewGroup viewContainer, MapView mapView, View...views){
    int width = viewContainer.getWidth();
    int height = viewContainer.getHeight();
    final Bitmap screenBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(screenBitmap);
    canvas.drawBitmap(bitmap, mapView.getLeft(), mapView.getTop(), null);
    for (View view:views){
        view.setDrawingCacheEnabled(true);
        canvas.drawBitmap(view.getDrawingCache(), view.getLeft(), view.getTop(), null);
    }

    return screenBitmap;
}
00:00 / 01:00
扫描二维码在手机端体验功能
体验移动端 扫码体验移动端

使用场景

对MapView以及其上的子view进行截图。

当用户在地图上绘制了标注点、覆盖物、信息窗口等数据时,利用该截屏功能可以实现包含地图和绘制数据的完整效果的截屏。

用到产品

iOS 地图 SDK

核心类/接口

接口

说明

版本

AMapSearchAPI

- (UIImage *)takeSnapshotInRect:(CGRect)rect;

在指定区域内截图(默认会包含该区域内的annotationView)

V4.0.0版本起

核心难点

实现截图:

- (void)captureAction
{
    // 获取mapView截图
    UIImage *mapImage = [self.mapView takeSnapshotInRect:self.mapView.bounds];

    // 对resultView进行截图
    CGSize s = self.resultView.bounds.size;
    UIGraphicsBeginImageContextWithOptions(s, NO, [UIScreen mainScreen].scale);
    [self.resultView.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *resultImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    // union image
    CGSize imageSize = self.mapView.bounds.size;

    UIGraphicsBeginImageContextWithOptions(imageSize, NO, [UIScreen mainScreen].scale);
    [mapImage drawInRect:self.mapView.bounds];
    [resultImage drawInRect:self.resultView.frame];

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    // show the image
    [self transitionToDetailWithImage:image];
}
func captureAction() {
    // map image
    let mapImage = self.mapView.takeSnapshot(in: self.mapView.bounds)
    // result image
    let s = self.resultView.bounds.size
    UIGraphicsBeginImageContextWithOptions(s, false, UIScreen.main.scale)
    self.resultView.layer.render(in: UIGraphicsGetCurrentContext()!)
    let resultImage = UIGraphicsGetImageFromCurrentImageContext()!
    UIGraphicsEndImageContext()
    // union image
    let imageSize = self.mapView.bounds.size
    UIGraphicsBeginImageContextWithOptions(imageSize, false, UIScreen.main.scale)
    mapImage?.draw(in: self.mapView.bounds)
    resultImage.draw(in: self.resultView.frame)
    let image = UIGraphicsGetImageFromCurrentImageContext()!
    UIGraphicsEndImageContext()
    self.transitionToDetail(with: image)
}