首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MKPolygon渲染标题

MKPolygon渲染标题
EN

Stack Overflow用户
提问于 2014-03-28 21:23:42
回答 1查看 2.3K关注 0票数 3

我试图使用以下代码呈现MKPolygon

代码语言:javascript
复制
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];

我使用以下代码提供笔画和填充颜色:

代码语言:javascript
复制
- (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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-03-30 13:10:29

覆盖不会自动显示它们的标题,就像注释一样(实际上是在标注中),所以没有什么需要“做”的,也没有任何您可以启用的配置。

正如您建议的那样,在覆盖上显示标题的一个简单解决方法是创建一个UILabel

但是,应该将此UILabel添加到位于每个覆盖的中心的注释视图中。

这种方法的一个小缺点(或者不是)是,标题不会随着地图的缩放而缩放--它们将保持相同的大小,并最终会与其他标题发生碰撞和重叠(但您可能对此没有意见)。

为实施这一办法:

  1. 对于每个覆盖,添加一个注释(使用addAnnotation:addAnnotations:),并将coordinate设置为覆盖的近似中心,将title设置为覆盖的title.Note,因为MKPolygon实现了MKOverlayMKAnnotation协议,因此不一定需要为每个覆盖创建单独的注释类或单独的对象。MKPolygon自动将其coordinate属性设置为多边形的近似中心,因此不需要计算任何内容。您只需将覆盖对象本身添加为注释即可。下面的例子就是这样做的。
  2. 实现mapView:viewForAnnotation:委托方法,并创建一个MKAnnotationView,其中包含一个显示titleUILabel

示例:

代码语言:javascript
复制
[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;
}

另一种方法是创建一个自定义的覆盖渲染器,并自己完成所有的绘图(多边形线、笔画颜色、填充颜色和文本)。有关如何实现该功能的一些想法,请参见Draw text in circle overlayIs there a way to add text using Paths Drawing

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22723449

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档