android - 在 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
如何从这些坐标创建折线位图?
解决方案
您当前正在从视图中获取坐标。但是,我认为您正在寻找地图的实际坐标。
您可以使用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
推荐阅读
- javafx - 如何在方法中启用 Platform.runlater
- python - python中tkinter中的'width'和'padx'有什么区别?
- css - Chrome DevTools 在使用 Inspect Element 编辑 CSS 后将 CSS Link Files 更改为“Constructed Stylesheet”,如何解决?
- elasticsearch - 如何在 Kibana 中将自定义字段视为标准类型而不是未知类型?
- javascript - 在源卡上条纹 V3 持卡人姓名
- python-3.x - 在执行下一个代码块之前,如何告诉我的 python 脚本等待“另存为”对话框在 chrome 中弹出?
- python - Python - 在循环中将数据导出到 CSV 仅保存最后一行输出
- ios - Flutter:在权限处理程序中添加IOS上的位置权限
- android - 如何在登录 Android 时更新 Firebase 用户个人资料?
- android - 用依赖注入注入公共类的好方法吗?