简体   繁体   English

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

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

I am drawing polygon on Google map and it works great. 我在Google地图上绘制多边形,效果很好。 What I need is to save this polygon as bitmap for fixed scale. 我需要的是将此多边形保存为固定比例的位图。 I am using this code to do that. 我正在使用此代码来执行此操作。

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;
}

And then to create bitmap 然后创建位图

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;
        }

The problem is that this method uses screen projection and when user changes zoom or drags map it changes polygon position on bitmap or its even out of bounds. 问题是这种方法使用屏幕投影,当用户更改缩放或拖动地图时,它会改变位图上的多边形位置或甚至超出边界。 How can I make it to draw the same size polygon on all devices not depending on zoom, or camera position? 如何在不依赖于缩放或相机位置的所有设备上绘制相同尺寸的多边形?

EDIT: So basically I managed to get the right size of polygon zooming map at right position before saving it to bitmap. 编辑:所以基本上我设法在将多边形缩放地图保存到位图之前获得正确尺寸的多边形缩放地图。 But the problem is when I use device with different resoulution the polygon size is also changing. 但问题是当我使用具有不同重新分配的设备时,多边形尺寸也在变化。 How to prevent that? 怎么预防?

A clever hack that might work for you- why not manually zoom the camera to fit the polyline whenever user tries to save the geofence. 一个可能适合您的聪明黑客 - 为什么不在用户试图保存地理围栏时手动缩放相机以适合折线。

 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...

Save an svg image of your polygon. 保存多边形的svg图像。 svg is vectoriel, bitmap is not svg是vectoriel,位图不是

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

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