我能够在Google地图中绘制多边形。现在,当我在地图上绘制完该区域的多边形后,我想用相同的图标填充该多边形。我可以用颜色填充多边形,但是我想用图标填充边框,并在边框处填充一些图标,并在图标之间指定一定的间隔。我该怎么办?
答案 0 :(得分:2)
对于Google Maps Android SDK Polygon
,没有使用模式填充(仅使用实心填充)的“开箱即用”解决方案,但是您可以使用一些解决方法。
TLDR;
假设您需要填充公园多边形:
final List<LatLng> polygonPoints = new ArrayList<>();
polygonPoints.add(new LatLng(50.444477, 30.498976));
polygonPoints.add(new LatLng(50.445290, 30.500864));
polygonPoints.add(new LatLng(50.443958, 30.508921));
polygonPoints.add(new LatLng(50.443247, 30.508642));
polygonPoints.add(new LatLng(50.442830, 30.508878));
polygonPoints.add(new LatLng(50.440965, 30.508256));
polygonPoints.add(new LatLng(50.441949, 30.501443));
final Polygon polygon = mGoogleMap.addPolygon(new PolygonOptions()
.addAll(polygonPoints)
.strokeColor(Color.BLUE)
.fillColor(Color.argb(20, 0, 255, 0)));
带有树形图标(R.drawable.ic_forest
):
您可以通过以下方式实现:
1)用带有自定义(树)图标的标记“填充”。为此,您需要找到多边形边界矩形,并在循环中添加标记作为方形贪婪。要仅在多边形内部添加标记,可以使用PolyUtil.containsLocation()
中的Google Maps Android API Utility Library:
LatLngBounds bounds = getPolygonBounds(polygon.getPoints());
BitmapDescriptor icon = BitmapDescriptorFactory.fromResource(R.drawable.ic_forest);
double boundHeight = bounds.northeast.latitude - bounds.southwest.latitude;
double boundWidth = bounds.northeast.longitude - bounds.southwest.longitude;
double gridCntLat = 5; // 5 icons vertically
double gridCntLon = 5; // 5 icons horizontally
double dLat = (bounds.northeast.latitude - bounds.southwest.latitude) / gridCntLat;
double dLng = (bounds.northeast.longitude - bounds.southwest.longitude) / gridCntLon;
double lat = dLat + bounds.southwest.latitude;
while (lat < bounds.northeast.latitude) {
double lon = dLng + bounds.southwest.longitude;
while (lon < bounds.northeast.longitude) {
LatLng iconPos = new LatLng(lat, lon);
if (PolyUtil.containsLocation(iconPos, polygonPoints, true)) {
MarkerOptions markerOptions = new MarkerOptions().position(iconPos)
.draggable(false)
.flat(true)
.icon(icon);
mGoogleMap.addMarker(markerOptions);
}
lon += dLng;
}
lat += dLat;
}
其中getPolygonBounds()
是:
private LatLngBounds getPolygonBounds(List<LatLng> polygon) {
LatLngBounds.Builder builder = new LatLngBounds.Builder();
for(int i = 0 ; i < polygon.size() ; i++) {
builder.include(polygon.get(i));
}
LatLngBounds bounds = builder.build();
return bounds;
}
和 ... 私有GoogleMap mGoogleMap; ...
@Override
public void onMapReady(GoogleMap googleMap) {
mGoogleMap = googleMap;
...
}
结果就是这样:
这是最简单的解决方案,但是某些图标可能会部分位于多边形边界之外(您可以通过MarkerOptions.anchor()
方法进行略微调整,如果希望标记图标随多边形一起旋转,也可以设置MarkerOptions.flat(true)
),并且大量标记会开始滞后。
2)使用“动态”(通过编程为每个多边形创建)GroundOverlay
。为此,还需要确定多边形矩形边界,然后创建具有相同比例的像素覆盖图(也需要将其限制在较大的一侧,以避免内存不足(OOM)错误)。注意,您需要使用Projection
来计算矩形范围,因为不同纬度和经度的LatLng度以米和像素为单位具有不同的大小。
然后,您需要在创建的位图画布上绘制多边形。为此,您可以将Path
对象与重新计算的LatLng多边形点一起使用,以像素为单位的本地叠加位图坐标。为此,您应该使用简单的比例,而不是Projection.toScreenLocation()
:
screenPoint.x = (int) (width * (latLng.longitude - bounds.southwest.longitude) / boundWidth);
screenPoint.y = height - (int) (height * (latLng.latitude - bounds.southwest.latitude) / boundHeight); // y-axis of screen is inverted to Lat
要用图标的位图填充覆盖位图上的多边形,可以使用Shader
并将其设置为Paint.setShader()
的Paint
对象。最后,您可以在GoogleMap对象上将创建的位图添加为Ground Overlay。 fillPoly()
方法的完整源代码,用polygon
填充tileBitmap
:
private GroundOverlay fillPoly(Projection projection, Polygon polygon, Bitmap tileBitmap, float transparency) {
LatLngBounds bounds = getPolygonBounds(polygon.getPoints());
double boundHeight = bounds.northeast.latitude - bounds.southwest.latitude;
double boundWidth = bounds.northeast.longitude - bounds.southwest.longitude;
Point northEast = projection.toScreenLocation(bounds.northeast);
Point southWest = projection.toScreenLocation(bounds.southwest);
int screenBoundHeight = northEast.y - southWest.y;
int screenBoundWidth = northEast.x - southWest.x;
// determine overlay bitmap size
double k = Math.abs((double) screenBoundHeight / (double) screenBoundWidth);
int overlayWidth;
int overlayHeight;
if (Math.abs(boundWidth) > Math.abs(boundHeight)) {
overlayWidth = OVERLAY_MAX_SIZE;
overlayHeight = (int) (overlayWidth * k);
} else {
overlayHeight = OVERLAY_MAX_SIZE;
overlayWidth = (int) (overlayHeight * k);
}
// create overlay bitmap
Bitmap overlayBitmap = createOverlayBitmap(overlayWidth, overlayHeight, bounds, polygon.getPoints(), tileBitmap);
// create ground overlay
GroundOverlayOptions overlayOptions = new GroundOverlayOptions()
.image(BitmapDescriptorFactory.fromBitmap(overlayBitmap))
.transparency(transparency)
.positionFromBounds(bounds);
return mGoogleMap.addGroundOverlay(overlayOptions);
}
其中createOverlayBitmap()
是:
private Bitmap createOverlayBitmap(int width, int height, LatLngBounds bounds, List<LatLng> polygon, Bitmap tileBitmap) {
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
Path path = new Path();
BitmapShader shader = new BitmapShader(tileBitmap,
Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
Paint paint = new Paint();
paint.setStyle(Paint.Style.FILL);
paint.setStrokeWidth(10);
paint.setAntiAlias(true);
paint.setShader(shader);
List<Point> screenPoints = new ArrayList<>(polygon.size());
double boundHeight = bounds.northeast.latitude - bounds.southwest.latitude;
double boundWidth = bounds.northeast.longitude - bounds.southwest.longitude;
for (int i = 0; i < polygon.size(); i++) {
LatLng latLng = polygon.get(i);
Point screenPoint = new Point();
screenPoint.x = (int) (width * (latLng.longitude - bounds.southwest.longitude) / boundWidth);
screenPoint.y = height - (int) (height * (latLng.latitude - bounds.southwest.latitude) / boundHeight);
screenPoints.add(screenPoint);
}
path.moveTo(screenPoints.get(0).x, screenPoints.get(0).y);
for (int i = 1; i < polygon.size(); i++) {
path.lineTo(screenPoints.get(i).x, screenPoints.get(i).y);
}
path.close();
canvas.drawPath(path, paint);
return bitmap;
}
您可以这样使用它:
Bitmap tileBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_forest); // create bitmap of fill icon
fillPoly(mGoogleMap.getProjection(), polygon, tileBitmap, 0.5f);
mGoogleMap.getProjection()
需要正确计算位图比例。如果需要在渲染地图之前使用mGoogleMap.getProjection()
,则应使用this的andr解决方案。
结果就是这样:
如果您需要“图标之间的某些指定空间”,则这些空间应在图标本身内部:
(h-水平空间,v-垂直空间)。
地面叠加层始终随地图缩放和旋转,如果需要独立于缩放的图标,则应使用其他方法。另外,对于较大的重叠位图,设备可能没有足够的内存。
3)使用Tile Overlays
。实际上,就像第2页一样,但是叠加位图是针对每个缩放级别生成的,并分成小块(例如256x256像素),以避免高内存消耗。您可以将生成的切片存储在设备文件系统中,并像在TileProvider
的'this'答案中那样实现Alex Vasilkov
;
4)使用基于custom
MapView
的视图和覆盖的dispatchDraw()
,您可以在其中控制视图画布上的所有绘图。这是最好的,但很复杂的解决方案。
注意!这只是演示-并不是完整的解决方案(例如,它没有针对负LatLng等进行测试。)