在Android上缩放谷歌地图并创建折线

3
我创建了一个Android应用程序,您可以在地图上添加点,然后它会从这些点创建多边形。效果如下图所示:enter image description here 然后我将折线的图像保存如下:
private Bitmap createPolylineBitmap() {
    Bitmap bitmap = Bitmap.createBitmap(((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getView().getWidth(), ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getView().getHeight(), Bitmap.Config.ARGB_8888);
    //Bitmap bitmap = Bitmap.createBitmap(((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getView().getWidth(), ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getView().getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);

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

    ArrayList<LatLng> coordinates = Mainigie.PievienotasKoordinates;

    for (int i = 0; i < coordinates.size(); i++) {
        try {
            LatLng latLng1 = new LatLng(coordinates.get(i).latitude, coordinates.get(i).longitude);
            LatLng latLng2 = new LatLng(coordinates.get(i + 1).latitude, coordinates.get(i + 1).longitude);
            canvas.drawLine((LatLngToPoint(latLng1).x), ((LatLngToPoint(latLng1).y)), (LatLngToPoint(latLng2).x), (LatLngToPoint(latLng2).y), paint);
            canvas.drawCircle((LatLngToPoint(latLng1).x),(LatLngToPoint(latLng1).y),5, paint);
        }
        catch(Exception ex){
        }
    }
    return bitmap;
}

这段代码生成了一个如下图所示的图片:

enter image description here

我需要将这个折线图像相对于Google地图按照1:10000或1:5000进行缩放,并且我需要这个图像大约为400x400,因为我需要在PDF文档中使用它,而比例尺非常重要。
如何将Google地图设置为这些比例尺?
如何将折线图像转换为这些比例尺? 编辑: 我认为我做错了的是使用来自视图的坐标,就像Javier Delgado所说的那样。现在我正在尝试使用来自地图的坐标,但如何将它们转换为屏幕坐标? 编辑: 这些是创建上述多边形的真实地图坐标。
    X:         Y:
    57.567177, 25.383375
    57.567391, 25.384218
    57.568717, 25.382321
    57.568159, 25.382033

如何从这些坐标创建折线位图?
2个回答

1
你目前从视图中获取坐标。然而,我认为你正在寻找地图的实际坐标。
你可以使用 getCameraPosition(), getMyLocation(), getProjection(), getUiSettings() 和其他一些函数来获取有关地图而非视图的信息。

https://developers.google.com/android/reference/com/google/android/gms/maps/GoogleMap

例如,CameraPosition对象将为您提供用户当前可见的地图区域的坐标:
bearing - Direction that the camera is pointing in, in degrees clockwise from north.
target - The location that the camera is pointing at.
tilt - The angle, in degrees, of the camera angle from the nadir (directly facing the Earth).
Zoom - level near the center of the screen.

https://developers.google.com/android/reference/com/google/android/gms/maps/model/CameraPosition

VisibleRegion

farLeft - LatLng object that defines the far left corner of the camera.
farRight - LatLng object that defines the far right corner of the camera.
latLngBounds - The smallest bounding box that includes the visible region defined in this class.
nearLeft - LatLng object that defines the bottom left corner of the camera.
nearRight - LatLng object that defines the bottom right corner of the camera.

https://developers.google.com/android/reference/com/google/android/gms/maps/model/VisibleRegion

然后您可以使用这些信息进行计算,以满足位图的需求。

https://developers.google.com/android/reference/com/google/android/gms/maps/model/CameraPosition


如果我有地图的实际坐标,那么创建我需要比例尺的折线位图的计算是什么?这是我第一次使用地图并尝试进行任何计算。 - WhizBoy
有一些工具可以转换地理数据的距离和大小。您需要操作相机的地图位置和用户选择的点。 - Javier Delgado

1
无论如何,您都可以使用GoogleMap.getProjection().toScreenLocation()方法将LatLng坐标转换为“平面”屏幕x,y坐标,然后在多边形中心周围对屏幕x,y坐标进行缩放

要在多边形中心周围进行缩放,您应该实现一些更多的仿射变换:将多边形/矩形的LatLon坐标转换为屏幕坐标,将多边形中心移动到屏幕坐标(0,0),将屏幕坐标乘以比例系数。您可以使用类似以下内容的代码:

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

使用方法:

...
scaleFactor = 500.0f;
Projection projection = mGoogleMap.getProjection();
List<Point> scaledPoints = scalePolygonPoints(mPolygon.getPoints(), scaleFactor , projection);
// draw scaledPoints on Bitmap canvas
...

如果多边形超出屏幕范围,则在位图上不可见。而且,如果我移动相机或缩放坐标,它们也会改变。我该如何使其在所有设备上绘制相同大小的多边形,而不取决于缩放或相机位置? - WhizBoy
@WhizBoy 请创建单独的问题。 - Andrii Omelchenko
@ Andrii Omelchenko https://dev59.com/ebDma4cB1Zd3GeqPDu7A - WhizBoy

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接