我想对两个标记周围的 GoogleMaps 应用色调。到目前为止,我得到的唯一可行的解决方案是在 map 上实际绘制多边形
map.addPolygon(new PolygonOptions()
.addAll(createRectangle(SphericalUtil.interpolate(markerData.getStartPoint(), markerData.getEndPoint(), CENTER_POINT_INTERPOLATE), 90, 45))
.strokeColor(Color.TRANSPARENT)
.fillColor(resources.getColor(R.color.tint_black_40)));
其中 createRectangle()
是来自 https://github.com/googlemaps/android-samples/blob/master/ApiDemos/app/src/main/java/com/example/mapdemo/PolygonDemoActivity.java 的方法
private List<LatLng> createRectangle(LatLng center, double halfWidth, double halfHeight) {
return Arrays.asList(new LatLng(center.latitude - halfHeight, center.longitude - halfWidth),
new LatLng(center.latitude - halfHeight, center.longitude + halfWidth),
new LatLng(center.latitude + halfHeight, center.longitude + halfWidth),
new LatLng(center.latitude + halfHeight, center.longitude - halfWidth),
new LatLng(center.latitude - halfHeight, center.longitude - halfWidth));
}
最佳答案
您可以创建一个 TileOverlay
并将其添加到 map (在您的标记上方或下方,具体取决于 zIndex)。
想法是创建一个 TileProvider
,它始终返回一个非常小的(在我的示例中为 2x2 像素)图像,该图像将绘制在 map 上。为确保性能,TileProvider
返回的Tile
将始终相同。
代码如下:
public class BackgroundTileProvider implements TileProvider {
private Tile tile;
@Override
public Tile getTile(int x, int y, int zoom) {
if (tile == null) {
// Create a very small (for performance) bitmap with alpha
Bitmap bmp = Bitmap.createBitmap(2, 2, Bitmap.Config.ARGB_8888);
// Draw the desired color to use as background
Canvas canvas = new Canvas(bmp);
canvas.drawColor(Color.argb(150, 0, 0, 0));
// Get the bytes and create the Tile
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
tile = new Tile(2, 2, stream.toByteArray());
}
return tile;
}
}
您需要将 TileOverlay
添加到您的 map 中,如下所示:
TileProvider tileProvider = new BackgroundTileProvider();
TileOverlay tileOverlay = map.addTileOverlay(
new TileOverlayOptions().tileProvider(tileProvider));
关于android - 谷歌地图添加色调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36264256/