首页 > 解决方案 > 在 Android 上缩放谷歌地图并创建折线

问题描述

我创建了 Android 应用程序,您可以在其中在地图上添加点,然后从这些点创建多边形。看起来像:

在此处输入图像描述

然后我像这样保存折线的图像:

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

此代码为我创建了如下所示的图像:

在此处输入图像描述

我需要的是这个折线图像相对于谷歌地图缩放到 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

如何从这些坐标创建折线位图?

标签: androiddictionaryscalepolyline

解决方案


您当前正在从视图中获取坐标。但是,我认为您正在寻找地图的实际坐标。

您可以使用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


推荐阅读