我试图使用以下代码呈现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
发布于 2014-03-30 13:10:29
覆盖不会自动显示它们的标题,就像注释一样(实际上是在标注中),所以没有什么需要“做”的,也没有任何您可以启用的配置。
正如您建议的那样,在覆盖上显示标题的一个简单解决方法是创建一个UILabel。
但是,应该将此UILabel添加到位于每个覆盖的中心的注释视图中。
这种方法的一个小缺点(或者不是)是,标题不会随着地图的缩放而缩放--它们将保持相同的大小,并最终会与其他标题发生碰撞和重叠(但您可能对此没有意见)。
为实施这一办法:
addAnnotation:或addAnnotations:),并将coordinate设置为覆盖的近似中心,将title设置为覆盖的title.Note,因为MKPolygon实现了MKOverlay和MKAnnotation协议,因此不一定需要为每个覆盖创建单独的注释类或单独的对象。MKPolygon自动将其coordinate属性设置为多边形的近似中心,因此不需要计算任何内容。您只需将覆盖对象本身添加为注释即可。下面的例子就是这样做的。mapView:viewForAnnotation:委托方法,并创建一个MKAnnotationView,其中包含一个显示title的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;
}另一种方法是创建一个自定义的覆盖渲染器,并自己完成所有的绘图(多边形线、笔画颜色、填充颜色和文本)。有关如何实现该功能的一些想法,请参见Draw text in circle overlay和Is there a way to add text using Paths Drawing。
https://stackoverflow.com/questions/22723449
复制相似问题