繁体   English   中英

Android将多边形从地图保存为所有设备的相同大小的位图

[英]Android save polygon from map as bitmap same size for all devices

我在Google地图上绘制多边形,效果很好。 我需要的是将此多边形保存为固定比例的位图。 我正在使用此代码来执行此操作。

private static List<Point> scalePolygonPoints(List<LatLng> points, float scale, Projection projection) {
    List<Point> scaledPoints = new ArrayList(points.size());

    LatLng polygonCenter = getPolygonCenterPoint(points);
    Point centerPoint = projection.toScreenLocation(polygonCenter);

    for (int i=0; i < points.size(); i++) {
        Point screenPosition = projection.toScreenLocation(points.get(i));
        screenPosition.x = (int) (scale * (screenPosition.x - centerPoint.x) + centerPoint.x);
        screenPosition.y = (int) (scale * (screenPosition.y - centerPoint.y) + centerPoint.y);
        scaledPoints.add(screenPosition);
    }

    return scaledPoints;
}

private static LatLng getPolygonCenterPoint(List<LatLng> polygonPointsList){
    LatLng centerLatLng = null;
    LatLngBounds.Builder builder = new LatLngBounds.Builder();
    for(int i = 0; i < polygonPointsList.size() ; i++) {
        builder.include(polygonPointsList.get(i));
    }
    LatLngBounds bounds = builder.build();
    centerLatLng =  bounds.getCenter();
    return centerLatLng;
}

然后创建位图

private Bitmap createPolylineBitmap(List<Point> scaledPoints) {
            Bitmap bitmap = Bitmap.createBitmap(800, 600, Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(bitmap);

            Paint paint = new Paint();
            paint.setColor(ContextCompat.getColor(this, R.color.black));
            paint.setStrokeWidth(10);
            paint.setDither(true);
            paint.setStyle(Paint.Style.STROKE);
            paint.setStrokeJoin(Paint.Join.ROUND);
            paint.setStrokeCap(Paint.Cap.ROUND);
            paint.setAntiAlias(true);


            for (int i = 0; i < scaledPoints.size(); i++) {
                try {
                    canvas.drawLine(scaledPoints.get(i).x, scaledPoints.get(i).y, scaledPoints.get(i + 1).x, scaledPoints.get(i + 1).y, paint);
                }
                catch(Exception ex){
                    canvas.drawLine(scaledPoints.get(i).x, scaledPoints.get(i).y, scaledPoints.get(0).x, scaledPoints.get(0).y, paint);
                }
            }
            return bitmap;
        }

问题是这种方法使用屏幕投影,当用户更改缩放或拖动地图时,它会改变位图上的多边形位置或甚至超出边界。 如何在不依赖于缩放或相机位置的所有设备上绘制相同尺寸的多边形?

编辑:所以基本上我设法在将多边形缩放地图保存到位图之前获得正确尺寸的多边形缩放地图。 但问题是当我使用具有不同重新分配的设备时,多边形尺寸也在变化。 怎么预防?

一个可能适合您的聪明黑客 - 为什么不在用户试图保存地理围栏时手动缩放相机以适合折线。

 val latLngs = getLatLngs()
 val builder = LatLngBounds.Builder()
 latLngs.forEach {
        builder.include(it.position)
 }
 val padding = 200
 val bounds = builder.build()
 val cameraUpdate = CameraUpdateFactory.newLatLngBounds(bounds, padding)
 map.moveCamera(cameraUpdate)
 //Do your projection operation here...

保存多边形的svg图像。 svg是vectoriel,位图不是

暂无
暂无

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

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