gpt4 book ai didi

ios - 更改注释图钉的图像

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:20:13 24 4
gpt4 key购买 nike

这就是我在 MapKit 上添加注释的方式:

 MKPointAnnotation *point = [[MKPointAnnotation alloc] init];

point.coordinate = zoomLocation;
point.title = @"Where am I?";
point.subtitle = @"I'm here!!!";


[self.mapView addAnnotation:point ];

如何更改注释的图像?

最佳答案

创建自定义注释类。

CustomAnnotation.h

#import <MapKit/MapKit.h>

@interface CustomAnnotation : MKAnnotationView

@end

CustomAnnotation.m

#import "CustomAnnotation.h"

@implementation CustomAnnotation

-(id)initWithAnnotation:(id<MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier {

self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
if (self != nil) {
CGRect frame = self.frame;
frame.size = CGSizeMake(46.0, 49.0);
self.frame = frame;
self.backgroundColor = [UIColor clearColor];
self.centerOffset = CGPointMake(-5, -5);
}
return self;
}


// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
[[UIImage imageNamed:@"IMAGE_NAME"] drawInRect:rect];

}

在我们的 mapview 委托(delegate)方法 viewForAnnotation

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation  {


static NSString *defaultPinId = @"Pin";
CustomAnnotation *pinView = (CustomAnnotation *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinId];

if (pinView == nil) {

pinView = [[CustomAnnotation alloc]initWithAnnotation:annotation reuseIdentifier:defaultPinId];

}
else {
pinView.annotation = annotation;
}

return pinView;

}

关于ios - 更改注释图钉的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19560610/

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