gpt4 book ai didi

ios - MKMapKit UILabel 作为注解

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:57:11 28 4
gpt4 key购买 nike

我认为这将是相当普遍的事情 - 但无法在任何地方找到它。我知道如何将图像放在 map 上。我使用在这里找到的 CSMapAnnotation http://spitzkoff.com/craig/?p=81 .

我几乎以 CSMapAnnotationTypeImage 为例并制作了 CSMapAnnotationTypeLabel,但它一直只在 map 上显示图钉,而不是像我预期的那样显示 UILabel。

头文件

#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>


@interface CSLabelAnnotationView : MKAnnotationView {
UILabel* _label;
}
@property(retain, nonatomic) UILabel* label;

@end

和源文件

#import "CSLabelAnnotationView.h"
#import "CSMapAnnotation.h"
#import "util.h"

@implementation CSLabelAnnotationView
@synthesize label = _label;

#define kWidth 120
#define kHeight 20

- (id)initWithAnnotation:(id <MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
self.frame = CGRectMake(0, 0, kWidth, kHeight);
self.backgroundColor = [UIColor clearColor];

CSMapAnnotation* csAnnotation = (CSMapAnnotation*)annotation;
_label = [[UILabel alloc] initWithFrame:self.frame];
_label.text=csAnnotation.title;
[self addSubview:_label];
return self;

}
#if 0
- (void)prepareForReuse
{
TGLog(@"");
[_imageView removeFromSuperview];
[_imageView release];
}
#endif

-(void) dealloc
{
[_label release];
[super dealloc];
}
@end

将其添加到 map

annotation = [[[CSMapAnnotation alloc] initWithCoordinate:coordinate
annotationType:CSMapAnnotationTypeLabel
title:i.name
reuseIdentifier:@"ocualabel"] autorelease];
[_mapView addAnnotation:annotation];

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
MKAnnotationView* annotationView = nil;

if (annotation == mapView.userLocation){
return nil; //default to blue dot
}

// determine the type of annotation, and produce the correct type of annotation view for it.
CSMapAnnotation* csAnnotation = (CSMapAnnotation*)annotation;
TGLog(@"csAnnotation.annotationType %d", csAnnotation.annotationType);
if(csAnnotation.annotationType == CSMapAnnotationTypeStart ||
csAnnotation.annotationType == CSMapAnnotationTypeEnd)
{
NSString* identifier = @"Pin";
MKPinAnnotationView* pin = (MKPinAnnotationView*)[self.mapView dequeueReusableAnnotationViewWithIdentifier:identifier];

if(nil == pin)
{
pin = [[[MKPinAnnotationView alloc] initWithAnnotation:csAnnotation reuseIdentifier:identifier] autorelease];
}

[pin setPinColor:(csAnnotation.annotationType == CSMapAnnotationTypeEnd) ? MKPinAnnotationColorRed : MKPinAnnotationColorGreen];

annotationView = pin;
TGLog(@"csPin anno");
}
else if(csAnnotation.annotationType == CSMapAnnotationTypeImage)
{
NSString* identifier = csAnnotation.reuseIdentifier;

CSImageAnnotationView* imageAnnotationView = (CSImageAnnotationView*)[self.mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
if(nil == imageAnnotationView)
{
imageAnnotationView = [[[CSImageAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier] autorelease];
imageAnnotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
}
TGLog(@"CSImage anno");
annotationView = imageAnnotationView;
} else if(csAnnotation.annotationType == CSMapAnnotationTypeLabel)
{
NSString* identifier = csAnnotation.reuseIdentifier;
CSLabelAnnotationView* labelAnnotationView = (CSLabelAnnotationView*)[self.mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
if(nil == labelAnnotationView)
{
labelAnnotationView = [[[CSLabelAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier] autorelease];
}
TGLog(@"CSLabel anno");

} else {
TGLog(@"%d", csAnnotation.annotationType);
}
[annotationView setEnabled:YES];
[annotationView setCanShowCallout:YES];
return annotationView;
}

谁能帮我解决这个问题?或者是否有其他一些标准方法可以在 map 上获取 UILabel?

最佳答案

找到了。
需要在 viewForAnnotation() 中添加:

annotationView = labelAnnotationView;

关于ios - MKMapKit UILabel 作为注解,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5480838/

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