[英]android google maps Polygon add circle hole
hi currently im working on a google maps app. 嗨,目前我在谷歌地图应用程序上工作。
and i want the following effect: 我想要以下效果:
To do this i was thing of first creating a polygon overlay over the country, following by adding a hole to this polygon for the highlighted area with a certain KM radius, so that it shrinks and expands when zooming. 要做到这一点,我首先在全国范围内创建一个多边形覆盖,然后为具有特定KM半径的突出显示区域的此多边形添加一个孔,以便在缩放时缩小和扩展。
Now i know how to create a polygon; 现在我知道如何创建一个多边形;
mMap.addPolygon(new PolygonOptions().addAll(sCountryBorder).fillColor(0xcc000000));
Now i want to add a hole to this polygon, but i dont know how to generate a circular hole with the correct radius. 现在我想为这个多边形添加一个洞,但我不知道如何生成一个具有正确半径的圆孔。
mMap.addPolygon(new PolygonOptions().addAll(sCountryBorder).fillColor(0xcc000000).addHole({CIRCULAR_HOLE}));
I know it is possible to create a circle with a certain radius in Google maps is it also possible to somehow convert this into an Array of LatLng objects? 我知道可以在谷歌地图中创建一个具有一定半径的圆圈是否也可以某种方式将其转换为LatLng对象数组?
mMap.addCircle(new CircleOptions()
.center(newLocation)
.radius(mRadius.size)
.strokeWidth(0)
.fillColor(getResources().getColor(R.color.transparant)));
Unfortunately, Google Maps library doesn't let to get an array of LatLng of the circle. 不幸的是,谷歌地图库不允许获得圆圈的LatLng数组。 So you need to draw a circle yourself. 所以你需要自己画一个圆圈。
Basically, you need to provide a method that will create a hole (circle) for a polygon which fills your map. 基本上,您需要提供一种方法,为填充地图的多边形创建一个孔(圆)。
There are 3 steps. 有3个步骤。
1 step is to build a method that will create a polygon that covers an entire map. 第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 step is to create a method that will return an Iterable
with LatLng
s of the circle. 第2步是创建一个方法,该方法将使用圆的LatLng
返回Iterable
。
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 and the last step is to use these methods for PolygonOptions
creation. 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);
}
I have also created a demo repository which contains an app which draws a needed circle. 我还创建了一个演示存储库,其中包含一个绘制所需圆圈的应用程序。 https://github.com/AntonyGolovin/Google-Map-mask . https://github.com/AntonyGolovin/Google-Map-mask 。 All needed logic is contained in MapHelper.java class. 所有需要的逻辑都包含在MapHelper.java类中。
Result: 结果:
I used this method to draw a circle with 5000m radius from user's location, but I do not know how to get that nice highlighted effect in your demo pic, this method gives you a circle with light blue background and a dark blue stroke. 我使用这种方法从用户的位置绘制一个半径为5000米的圆,但我不知道如何在你的演示图片中获得那个漂亮的高亮效果,这个方法为你提供了一个浅蓝色背景和深蓝色笔触的圆圈。 Hope it helps! 希望能帮助到你!
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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.