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

4

1 回答 1

4

覆盖不会像注释那样自动显示它们的标题(实际上是在它们的标注中),因此您“不需要做”任何事情或您可以启用任何配置。

正如您所建议的,在叠加层上显示标题的一个简单解决方法是创建一个UILabel.
但是,这UILabel应该添加到位于每个叠加层中心 的注释视图中。

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

要实施这种方法:

  1. 对于每个叠加层,添加一个注释(使用addAnnotation:addAnnotations:)并将 设置为coordinate叠加层的大致中心,并将 设置title为叠加层的标题。

    请注意,由于同时MKPolygon实现theMKOverlay 协议,因此MKAnnotation您不一定需要为每个叠加层创建单独的注释类或单独的对象。 MKPolygon自动将其coordinate属性设置为多边形的近似中心,因此您无需计算任何内容。 您可以将叠加对象本身添加为注释。 下面的示例就是这样做的。

  2. 实现mapView:viewForAnnotation:委托方法并在其中创建MKAnnotationView一个UILabel显示title.

例子:

[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;
}

另一种方法是创建一个自定义叠加渲染器并自己完成所有绘图(多边形线、描边颜色、填充颜色和文本)。有关如何实现它的一些想法,请参阅在圆形叠加中绘制文本是否有一种方法可以使用路径绘图添加文本。

于 2014-03-30T13:10:29.397 回答