绘制具有背景颜色的GMSPolyline

时间:2013-03-05 22:10:43

标签: ios google-maps-sdk-ios mkpolygon

我看到GMSPolyline协议已经为其笔触颜色定义了color属性,但有没有办法遮蔽其多边形的内部(理想情况下是透明度)?我正在寻找相当于MKPolygon和朋友的Google地图。

2 个答案:

答案 0 :(得分:2)

折线与Polygon不同。折线'没有填充颜色的概念。为要添加到SDK的多边形提交feature request

答案 1 :(得分:2)

有一种方法,你可以得到这样的东西:

Polygons example

方法很简单:

  • 使用覆盖绘图代码添加透明的非交互式UIView并将其传递给CGPoints以绘制多边形
  • 获取多边形的CLLocationCoordinate2D坐标并将其转换为CGPoints以进行绘制
  • 每次地图移动时都会更新这些CGPoints,以便您可以将它们重新绘制在正确的位置并使UIView重绘自己。

因此,您要做的是在地图视图的顶部添加UIView,这是透明且非用户交互的,已覆盖drawRect方法。它提供了一个CGPoints的双数组,如CGpoint **points,points[i][j]一样,其中 i 是每个闭合多边形, j 是个别点每个多边形。该类将是,我们称之为OverView:

#import "OverView.h"

@interface OverView ()
{
    CGPoint **points;
    int *pointsForPolygon;
    int count;
}

@end

@implementation OverView

- (id)initWithFrame:(CGRect)frame andNumberOfPoints:(int)numpoints andPoints:(CGPoint **)passedPoints andPointsForPolygon:(int *)passedPointsForPolygon;{
    self = [super initWithFrame:frame];
    if (self) {

        // You want this to be transparent and non-user-interactive

        self.userInteractionEnabled = NO;
        self.backgroundColor = [UIColor clearColor];

        // Passed data

        points = passedPoints; // all CGPoints
        pointsForPolygon = passedPointsForPolygon; // number of cgpoints for each polygon
        count = numpoints; // Number of polygons
    }
    return self;
}


// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.

- (void)drawRect:(CGRect)rect
{

    for(int i=0; i<count; i++) // For each of polygons, like blue ones in picture above
    {
        if (pointsForPolygon[i] < 2) // Require at least 3 points
            continue;

        CGContextRef context = UIGraphicsGetCurrentContext();

        CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
        CGContextSetRGBFillColor(context, 0.0, 0.0, 1.0, 1.0);

        CGContextSetLineWidth(context, 2.0);

        for(int j = 0; j < pointsForPolygon[i]; j++)
        {
            CGPoint point = points[i][j];

            if(j == 0)
            {
                // Move to the first point
                CGContextMoveToPoint(context, point.x, point.y);
            }
            else
            {
                // Line to others
                CGContextAddLineToPoint(context, point.x, point.y);
            }
        }

        CGContextClosePath(context); // And close the path
        CGContextFillPath(context);
        CGContextStrokePath(context);
    }
}


@end

现在,在带有mapview的原始UIViewController中,您需要访问制作所有多边形的所有坐标(与点相同的数组,但由CLLocationCoordinate2D组成,以及其他几个:

@interface ViewController () <GMSMapViewDelegate>
{
    CGPoint **points;
    int howmanypoints;
    int *pointsForPolygon;
    CLLocationCoordinate2D **acoordinates;
}
在获取多边形坐标的任何地方填充

acoordinates,我从Fusion Tables解析响应字符串,这是我的解析器方法的一部分

- (void)parseResponse2
{
    NSMutableArray *fullArray = [[self.fusionStringBeaches componentsSeparatedByString:@"\n"] mutableCopy];

    howmanypoints = fullArray.count; // This is number of polygons

    pointsForPolygon = (int *)calloc(howmanypoints, sizeof(int)); // Number of points for each of the polygons
    points = (CGPoint **)calloc(howmanypoints, sizeof(CGPoint *));
    acoordinates = (CLLocationCoordinate2D **)calloc(howmanypoints, sizeof(CLLocationCoordinate2D *));

    for(int i=0; i<fullArray.count; i++)
    {

        // Some parsing skipped here

        points[i] = (CGPoint *)calloc(koji, sizeof(CGPoint));
        acoordinates[i] = (CLLocationCoordinate2D *)calloc(koji, sizeof(CLLocationCoordinate2D));
        pointsForPolygon[i] = koji;

        if (koji > 2)
        {
            // Parsing skipped                
            for (int j=0; j<koji; j++)
            {
                CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(coordinates[j].latitude, coordinates[j].longitude);

                // Here, you convert coordinate and add it to points array to be passed to overview
                points[i][j] = [self.mapView.projection pointForCoordinate:coordinate];
                // and added that coordinate to array for future access
                acoordinates[i][j] = coordinate;
            }
        }
    }

    // Finally, allocate OverView passing points array and polygon and coordinate counts
    self.overView = [[OverView alloc] initWithFrame:self.view.bounds
                                  andNumberOfPoints:howmanypoints
                                          andPoints:points
                                andPointsForPolygon:pointsForPolygon];

     // And add it to view
    [self.view addSubview:self.overView];

}

现在,您可以在任何地方使用多边形,但必须遵守- (void)mapView:(GMSMapView *)mapView didChangeCameraPosition:(GMSCameraPosition *)position委托方法,因为绘制的多边形不会随地图移动。诀窍在于您拥有坐标为acoordinates的2D数组,并且您可以使用辅助函数(CGPoint *)[self.mapview.projection pointForCoordinate:(CLLocationCoordinate2D)coordinate]来重新计算位置,例如:

- (void)mapView:(GMSMapView *)mapView didChangeCameraPosition:(GMSCameraPosition *)position
{    
    if (points != nil)
    {
        // Determine new points to pass
        for (int i=0; i<howmanypoints; i++)
        {
            for(int j=0; j<pointsForPolygon[i]; j++)
            {
                // Call method to determine new CGPoint for each coordinate
                points[i][j] = [self.mapView.projection pointForCoordinate:acoordinates[i][j]];
            }
        }

        // No need to pass points again as they were passed as pointers, just refresh te view    
        [self.overView setNeedsDisplay];
    }

}

就是这样。希望你得到它的要点。请评论我是否需要澄清一些事情。我还可以创建一个小型的完整项目并将其上传到github,以便您可以更好地进行研究。

相关问题