嗨,目前正在开发谷歌地图应用程序。
我想要以下效果:
要做到这一点,我首先要在全国范围内创建多边形覆盖,然后为具有特定KM半径的突出显示区域的此多边形添加一个孔,以便在缩放时缩小和扩展。
现在我知道如何创建一个多边形;
mMap.addPolygon(new PolygonOptions().addAll(sCountryBorder).fillColor(0xcc000000));
现在我想为这个多边形添加一个洞,但我不知道如何生成一个具有正确半径的圆孔。
mMap.addPolygon(new PolygonOptions().addAll(sCountryBorder).fillColor(0xcc000000).addHole({CIRCULAR_HOLE}));
我知道可以在Google地图中创建一个具有一定半径的圆圈是否也可以某种方式将其转换为LatLng对象数组?
mMap.addCircle(new CircleOptions()
.center(newLocation)
.radius(mRadius.size)
.strokeWidth(0)
.fillColor(getResources().getColor(R.color.transparant)));
答案 0 :(得分:9)
不幸的是,Google地图库不允许获取圆圈的LatLng数组。所以你需要自己画一个圆圈。
基本上,您需要提供一种方法,为填充地图的多边形创建一个孔(圆)。
有3个步骤。
1步是构建一个方法,该方法将创建一个覆盖整个地图的多边形。
private static List<LatLng> createOuterBounds() {
float delta = 0.01f;
return new ArrayList<LatLng>() {{
add(new LatLng(90 - delta, -180 + delta));
add(new LatLng(0, -180 + delta));
add(new LatLng(-90 + delta, -180 + delta));
add(new LatLng(-90 + delta, 0));
add(new LatLng(-90 + delta, 180 - delta));
add(new LatLng(0, 180 - delta));
add(new LatLng(90 - delta, 180 - delta));
add(new LatLng(90 - delta, 0));
add(new LatLng(90 - delta, -180 + delta));
}};
}
2步是创建一个方法,该方法将返回带有Iterable
个圆圈的LatLng
。
private static Iterable<LatLng> createHole(LatLng center, int radius) {
int points = 50; // number of corners of inscribed polygon
double radiusLatitude = Math.toDegrees(radius / (float) EARTH_RADIUS);
double radiusLongitude = radiusLatitude / Math.cos(Math.toRadians(center.latitude));
List<LatLng> result = new ArrayList<>(points);
double anglePerCircleRegion = 2 * Math.PI / points;
for (int i = 0; i < points; i++) {
double theta = i * anglePerCircleRegion;
double latitude = center.latitude + (radiusLatitude * Math.sin(theta));
double longitude = center.longitude + (radiusLongitude * Math.cos(theta));
result.add(new LatLng(latitude, longitude));
}
return result;
}
3,最后一步是使用这些方法创建PolygonOptions
。
static PolygonOptions createPolygonWithCircle(Context context, LatLng center, int radius) {
return new PolygonOptions()
.fillColor(ContextCompat.getColor(context, R.color.grey_500_transparent))
.addAll(createOuterBounds())
.addHole(createHole(center, radius))
.strokeWidth(0);
}
我还创建了一个演示存储库,其中包含一个绘制所需圆圈的应用程序。 https://github.com/AntonyGolovin/Google-Map-mask。所有需要的逻辑都包含在MapHelper.java类中。
结果:
答案 1 :(得分:-1)
我使用这种方法从用户的位置绘制一个半径为5000米的圆圈,但我不知道如何在你的演示图片中获得漂亮的高亮效果,这种方法为你提供了一个浅蓝色背景的圆圈和深蓝色的笔触。希望能帮助到你!
private void drawCircle(LatLng point){
// Instantiating CircleOptions to draw a circle around the marker
CircleOptions circleOptions = new CircleOptions();
// Specifying the center of the circle
circleOptions.center(point);
// Radius of the circle
circleOptions.radius(5000);
// Border color of the circle
circleOptions.strokeColor(0xFF0000FF);
// Fill color of the circle
circleOptions.fillColor(0x110000FF);
// Border width of the circle
circleOptions.strokeWidth(2);
// Adding the circle to the GoogleMap
mMap.addCircle(circleOptions);
}