我在谷歌地图绘制多边形和它的伟大工程。我需要的是拯救这个多边形作为位固定比例。我使用此代码来做到这一点。
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;
}
然后创建位图
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;
}
问题是,该方法使用屏幕投影,并且当用户改变变焦或拖动地图它位图改变多边形的位置或它的甚至出界。我怎样才能使它绘制相同大小的多边形上的所有设备不依赖于变焦或相机的位置?
编辑:所以基本上我设法将其保存为位图之前得到的在正确的位置多边形缩放地图的大小合适。但问题是,当我使用设备不同resoulution多边形大小也在变化。如何预防呢?
一个聪明的黑客可能会为你 - 工作,为什么不手动变焦摄像机到适合折线每当用户试图保存在地理围栏。
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...
保存多边形的SVG图像。 SVG是vectoriel,位图是不是