在Android上缩放Google Map并创建折线

时间:2018-12-01 21:05:51

标签: android dictionary scale polyline

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

enter image description here

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

Scanner scan = new Scanner (System.in);
String input = null;

if (scan.hasNextLine() && !(input = scan.nextLine()).isEmpty()) {
  char mainMenu = input.charAt(0);
  // do something else
} else {
  System.err.println("Nothing was entered");
}

System.out.println("Hello " + input);

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

enter image description here

我需要的是此折线图像,要相对于Google地图缩放为1:10000或1:5000。而且我需要这张图片的尺寸约为400x400,因为我需要将其用于比例非常重要的PDF文档中。

如何将Google地图设置为这些比例尺?

如何将折线图像转换为这些比例尺?

编辑: 我认为我做错了是使用视图中的坐标,如下面的哈维尔·德尔加多所说。现在,我正在尝试使用地图中的坐标,但是如何将其转换为屏幕坐标?

编辑: 这些是在上方创建多边形的真实地图坐标

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

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

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:1)

无论如何,您可以使用GoogleMap.getProjection().toScreenLocation()方法将LatLng坐标转换为“平面”屏幕x,y坐标,然后在屏幕x,y上围绕中心坐标执行scaling多边形。

要围绕多边形中心缩放,您应该执行更多仿射变换:将多边形/矩形的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
...