是否可以用动态文本标签替换注释的图钉图标?
也许使用 css,或者动态创建图像?
例如,标签是使用 JavaScript 在 Google Maps API 上使用 CSS 完成的。
最佳答案
是的,这是可能的。
在 iOS MapKit 中,您需要实现 viewForAnnotation
委托(delegate)方法并返回一个添加了 UILabel
的 MKAnnotationView
。
例如:
-(MKAnnotationView *)mapView:(MKMapView *)mapView
viewForAnnotation:(id<MKAnnotation>)annotation
{
if ([annotation isKindOfClass:[MKUserLocation class]])
return nil;
static NSString *reuseId = @"reuseid";
MKAnnotationView *av = [mapView dequeueReusableAnnotationViewWithIdentifier:reuseId];
if (av == nil)
{
av = [[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:reuseId] autorelease];
UILabel *lbl = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 30)] autorelease];
lbl.backgroundColor = [UIColor blackColor];
lbl.textColor = [UIColor whiteColor];
lbl.alpha = 0.5;
lbl.tag = 42;
[av addSubview:lbl];
//Following lets the callout still work if you tap on the label...
av.canShowCallout = YES;
av.frame = lbl.frame;
}
else
{
av.annotation = annotation;
}
UILabel *lbl = (UILabel *)[av viewWithTag:42];
lbl.text = annotation.title;
return av;
}
确保设置了 map View 的 delegate
属性,否则将不会调用此委托(delegate)方法,您将获得默认的红色图钉。
关于iphone - 用注释中的文本标签替换图标图钉?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9822756/