【问题标题】:Custom pin with alphanumeric representation in MapKitMapKit 中具有字母数字表示的自定义引脚
【发布时间】:2013-12-28 16:52:55
【问题描述】:

我现在正在自学如何在 Objective C 中使用 MapKit。我已经到了可以自动缩放到包含多个注释的位置的地步。我用内置的引脚表示注释。如果您单击图钉,我有一个字母数字 2 字符串来表示该位置。

我心想,为了更好的可用性,为什么不用实际数据替换引脚。有点像天气图,它们将温度显示为大头针。这可行吗?

我研究了这个,我能找到的只有这个:

- (MKAnnotationView *)mapView:(MKMapView *)map viewForAnnotation:(id <MKAnnotation>)annotation
{
    static NSString *AnnotationViewID = @"annotationViewID";

    MKAnnotationView *annotationView = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationViewID];

    if (annotationView == nil) {
        annotationView = [[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID] autorelease];
    }

    annotationView.image = [UIImage imageNamed:@"location.png"];
    annotationView.annotation = annotation;

    return annotationView;
}

问题是我不能也不应该为这两个字符的每个组合都有一个自定义图像。有没有办法让我在大头针的位置画出这些数字。

我找到了这个参考: https://developer.apple.com/library/ios/documentation/userexperience/conceptual/LocationAwarenessPG/AnnotatingMaps/AnnotatingMaps.html

我是否走在正确的轨道上。是否有一些完整的示例可以用来更好地理解流程。

但是,我希望能够选择该自定义图钉并继续或显示更多详细信息。

感谢您的宝贵时间。

【问题讨论】:

标签: ios objective-c mapkit


【解决方案1】:

安娜,谢谢你的链接,我试过了,它成功了。我做了一些改变以提高标签的美感,我想我会在这里发布我的发现以供参考:

-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id)annotation { if ([注解 isKindOfClass:[MKUserLocation 类]]) 返回零;

static NSString *reuseId = @"MapViewController";
MKAnnotationView *av = [mapView dequeueReusableAnnotationViewWithIdentifier:reuseId];
if (av == nil)
{
    av = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:reuseId];

    UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 20)];
    //UILabel *lbl = [[UILabel alloc] init];;
    lbl.backgroundColor = [UIColor clearColor];  // This makes the background clear and just shows the text
    lbl.textColor = [UIColor whiteColor];  // Use any colour you wish
    lbl.alpha = 1.0; //0.5;
    lbl.tag = 42;
    lbl.font = [UIFont fontWithName:@"Helvetica-Bold" size:16.0];   // This is optional, I found this fond and size most readable
    [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];
// I added this to the text to improve its visibility by essentially adding a stroke around the text.  Well its a poor man's stroke by adding a shadow
lbl.layer.shadowColor = [[UIColor blackColor] CGColor];
lbl.layer.shadowOffset = CGSizeMake(0.0f, 0.0f);
lbl.layer.shadowOpacity = 1.0f;
lbl.layer.shadowRadius = 1.0f;

lbl.text = annotation.title;

return av;

}

【讨论】:

    猜你喜欢
    • 2013-04-06
    • 2021-11-19
    • 2021-12-30
    • 1970-01-01
    • 1970-01-01
    • 2019-02-03
    • 2017-06-13
    • 2016-05-16
    • 2017-02-11
    相关资源
    最近更新 更多