gpt4 book ai didi

ios - 在 MapKit 中使用字母数字表示的自定义图钉

转载 作者:行者123 更新时间:2023-11-29 10:50:05 24 4
gpt4 key购买 nike

我现在正自学在 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

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

不过,我希望能够选择该自定义 pin 并继续或显示更多详细信息。

感谢您的宝贵时间。

最佳答案

Anna,谢谢你提供的链接,我试过了并且成功了。我做了一些更改以提高标签的美感,我想我会在此处发布我的发现以供引用:

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

关于ios - 在 MapKit 中使用字母数字表示的自定义图钉,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20816977/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com