0

因此,我编写了一个自定义叠加项,用于填充基于地理点数组的透明蓝色叠加层

@Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
    Projection projection = mapView.getProjection();

    Paint fill = new Paint();
    fill.setColor(Color.BLUE);
    fill.setAlpha(50);
    fill.setStyle(Paint.Style.FILL_AND_STROKE);

    Path path = new Path();
    Point firstPoint = new Point();
    projection.toPixels(geoPoints.get(0), firstPoint);
    path.moveTo(firstPoint.x, firstPoint.y);

    for (int i = 1; i < geoPoints.size(); ++i) {
        Point nextPoint = new Point();
        projection.toPixels(geoPoints.get(i), nextPoint);
        path.lineTo(nextPoint.x, nextPoint.y);
    }

    path.lineTo(firstPoint.x, firstPoint.y);
    path.setLastPoint(firstPoint.x, firstPoint.y);

    canvas.drawPath(path, fill);

    super.draw(canvas, mapView, shadow);
}

我需要的是一种方法来获得这个叠加层的中心点,这样我就可以在上面放置一个标记,有人有什么想法吗?

4

1 回答 1

1

虽然我不熟悉 android 框架,但我假设你用 java 编写并使用某种 google maps api。但我确实熟悉图形和地理开发。我对您的建议首先是检查标准 api 是否有某种 getBounds(path) 返回给您的 RectangularBounds 对象或类似对象。然后,您可以从矩形边界请求 bounds.getCenter() ,它将边界中心作为地理点或其他度量返回。如果您使用像素,只需像您一样转换地理点...

如果 api 中不存在 getBounds(难以置信),只需实现一个简单的接口,您可以在网上找到很多示例。

用于查找地理点的地理形状边界的简单伪代码,如果您需要像素分别使用 x,y:

bounds = { topLeft: new GeoPoint(path[0]), bottomRight: new GeoPoint(path[0])};
for( point in path ){
    bounds.topLeft.lat = max( bounds.topLeft.lat,point.lat );
    bounds.topLeft.lng = min( bounds.topLeft.lng,point.lng );
    bounds.bottomRight.lat = min( bounds.bottomRight.lat,point.lat );
    bounds.bottomRight.lng = max( bounds.bottomRight.lng,point.lng );
}

bounds.getCenter(){
    return new GeoPoint(rectangle center point); 
    // i am sure you will able to manage the code here )))
}

希望这会有所帮助

于 2012-07-25T15:09:51.567 回答