iphone - 用注释中的文本标签替换图标图钉?

标签 iphone ios mapkit

是否可以用动态文本标签替换注释的图钉图标?

也许使用 css,或者动态创建图像?

例如,标签是使用 JavaScript 在 Google Maps API 上使用 CSS 完成的。

最佳答案

是的,这是可能的。

在 iOS MapKit 中,您需要实现 viewForAnnotation 委托(delegate)方法并返回一个添加了 UILabelMKAnnotationView

例如:

-(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/

相关文章:

ios - 检测应用程序何时在后台状态 iOS 中被用户杀死

swift - 创建指向指定位置方向的自定义罗盘箭头

iOS MapView 位于导航栏、状态栏和标签栏 Controller 下方

iphone - 如何在 iphone 中将 unix 时间戳转换为 nsdate

ios - IOS 中的 XMPP 一对一聊天

iphone - 涉及 NSDocumentDirectory 和其他 iOS 应用程序特定调用的静态库的单元测试

ios - iCloud 公共(public)数据可能性

ios - 控制嵌套的 for 循环 Swift 3

ios - 从 MKPlacemark 创建 MKMapItem 时,MKMapitem 的 Placemark.name 为 nil

ios - 在 iOS 上获取电池健康信息