我已经使用几个纬度、经度点在谷歌地图上绘制了一个多边形。但是现在我需要在需要中心坐标的多边形中心放置一个标记。如何计算中心点。
下面是在 map 上添加多边形的代码:
for (Warning w : warningsList) {
// Instantiates a new Polygon object and adds points
PolygonOptions rectOptions = new PolygonOptions();
List<PolyPoints> pp = w.getPolyPoints();
for (PolyPoints p : pp) {
rectOptions.add(new LatLng(Double.valueOf(p.getLatitude()),
Double.valueOf(p.getLongitude())));
}
mMap.addPolygon(rectOptions.strokeColor(Color.GREEN)
.fillColor(Color.RED).strokeWidth(STROKE_WIDTH));
}
我找到了类似的 question已经回答了,但那是针对 JavaScript Api 的。在我的案例中,他们有没有使用相同解决方案的方法?
最佳答案
下面的代码用于查找多边形的中心点。它也对我有用
private LatLng getPolygonCenterPoint(ArrayList<LatLng> polygonPointsList){
LatLng centerLatLng = null;
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;
}
关于android - 如何在 Google Maps Android API v2 中计算多边形的中心?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18440823/