在注释中用文本标签替换图标引脚? [英] Replace icon pin by text label in annotation?

查看:109
本文介绍了在注释中用文本标签替换图标引脚?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Is it possible to replace the pin icon of an annotation by a dynamic text label?

可能使用css,还是动态创建图像?

Maybe using css, or dynamically creating an image?

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

For example a label is done with CSS on Google Maps API with JavaScript.

推荐答案

是的,这是可能的。

在iOS MapKit中,您需要实现 viewForAnnotation 委托方法并返回 MKAnnotationView 添加了 UILabel

In iOS MapKit, you'll need to implement the viewForAnnotation delegate method and return an MKAnnotationView with a UILabel added to it.

For示例:

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

确保地图视图的委托属性已设置,否则此委托方法将不会被调用,您将获得默认的红色引脚。

Make sure the map view's delegate property is set otherwise this delegate method will not get called and you will get the default red pins instead.

这篇关于在注释中用文本标签替换图标引脚?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆