MKPolygon的渲染标题

3

我正在尝试使用以下代码渲染MKPolygon

NSMutableArray *overlays = [NSMutableArray array];

        for (NSDictionary *state in states) {
            NSArray *points = [state valueForKeyPath:@"point"];

            NSInteger numberOfCoordinates = [points count];
            CLLocationCoordinate2D *polygonPoints = malloc(numberOfCoordinates * sizeof(CLLocationCoordinate2D));

            NSInteger index = 0;
            for (NSDictionary *pointDict in points) {
                polygonPoints[index] = CLLocationCoordinate2DMake([[pointDict valueForKeyPath:@"latitude"] floatValue], [[pointDict valueForKeyPath:@"longitude"] floatValue]);
                index++;
            }

            MKPolygon *overlayPolygon = [MKPolygon polygonWithCoordinates:polygonPoints count:numberOfCoordinates];
            overlayPolygon.title = [state valueForKey:@"name"];

            [overlays addObject:overlayPolygon];

            free(polygonPoints);
        }
[self.stateMapView addOverlays:overlays];

我使用以下代码提供笔画和填充颜色:
- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id <MKOverlay>)overlay NS_AVAILABLE(10_9, 7_0);
{
    if ([overlay isKindOfClass:[MKPolygon class]])
    {
        MKPolygonRenderer *pv = [[MKPolygonRenderer alloc] initWithPolygon:overlay];

        pv.fillColor = [UIColor redColor];
        pv.strokeColor = [UIColor blackColor];

        return pv;
    }

    return nil;
}

我需要做些什么才能呈现标题?我认为我应该启用一个配置或者类似的东西,但是我对MapView不熟悉。或者我需要创建一个UILabel吗?

1个回答

4
覆盖层不像注释那样自动显示它们的标题(实际上是在它们的callout中),因此您没有任何“需要做的事情”或任何可以启用的配置。想要在覆盖层上显示标题的简单解决方法就是,如您所建议的,创建一个UILabel。但是,这个UILabel应该添加到每个覆盖层的中心位置上的一个注释视图中。
这种方法的一个小缺点(或可能不是)是,标题不会随着地图缩放而缩放 -- 它们的大小将保持不变,并可能与其他标题发生碰撞和重叠(但您可能对此无所谓)。
要实现此方法:
1. 对于每个覆盖层,添加一个注释(使用addAnnotation:或addAnnotations:),并将coordinate设置为覆盖层的近似中心,title设置为覆盖层的标题。请注意,由于MKPolygon同时实现了MKOverlay和MKAnnotation协议,因此您不一定需要为每个覆盖层创建一个单独的注释类或单独的对象。MKPolygon自动将其coordinate属性设置为多边形的近似中心,因此您不需要计算任何内容。您只需将覆盖层对象本身添加为注释即可。以下示例就是这样做的。
2. 实现mapView:viewForAnnotation:委托方法,并创建一个MKAnnotationView,其中包含一个UILabel,用于显示标题。
示例:
[self.stateMapView addOverlays:overlays];

//After adding the overlays as "overlays",
//also add them as "annotations"...
[self.stateMapView addAnnotations:overlays];


//Implement the viewForAnnotation delegate method...
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
    if ([annotation isKindOfClass:[MKUserLocation class]])
    {
        //show default blue dot for user location...
        return nil;
    }

    static NSString *reuseId = @"ann";
    MKAnnotationView *av = [mapView dequeueReusableAnnotationViewWithIdentifier:reuseId];
    if (av == nil)
    {
        av = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:reuseId];
        av.canShowCallout = NO;

        //add a UILabel in the view itself to show the title...
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 30)];
        titleLabel.tag = 42;
        titleLabel.backgroundColor = [UIColor clearColor];
        titleLabel.textColor = [UIColor blackColor];
        titleLabel.font = [UIFont boldSystemFontOfSize:16];
        titleLabel.textAlignment = NSTextAlignmentCenter;
        titleLabel.minimumScaleFactor = 0.5;
        [av addSubview:titleLabel];
        av.frame = titleLabel.frame;
    }
    else
    {
        av.annotation = annotation;
    }

    //find the UILabel and set the title HERE
    //so that it gets set whether we're re-using a view or not...
    UILabel *titleLabel = (UILabel *)[av viewWithTag:42];
    titleLabel.text = annotation.title;

    return av;
}

另一种方法是创建自定义覆盖层渲染器并自行完成所有绘制(多边形线条、描边颜色、填充颜色和文本)。请参见在圆形覆盖层中绘制文本是否有一种使用路径绘图添加文本的方法获取一些实现这种方法的想法。


1
创建标签时,还需添加以下内容,否则 minimumScaleFactor 将不起作用:titleLabel.adjustsFontSizeToFitWidth = YES; - user467105

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接