gpt4 book ai didi

IOS 自定义 pin 未显示

转载 作者:行者123 更新时间:2023-11-29 01:52:05 25 4
gpt4 key购买 nike

我正在尝试显示图像,而不是源位置的默认红色图钉和目标位置的另一个图像。

- (void) sourceMarker:(MKPlacemark * ) homeLoc {
MKAnnotationView *point = [[MKAnnotationView alloc] init];

MKPointAnnotation *ml = point.annotation;

ml.coordinate = homeLoc.coordinate;

ml.title = @"Home";

point.image = [UIImage imageNamed:@"home.png"];
point.canShowCallout = YES;

[self.mapView addAnnotation:ml];
}

最佳答案

按照 Apple 文档 Annotating Maps 的建议。必须创建自定义注释类,请引用以下代码。

MyAnnotation.h

@interface MyAnnotation : NSObject<MKAnnotation>
{
CLLocationCoordinate2D coordinate;
NSString *title;
NSString *subtitle;
NSString *type;

}

@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *subtitle;
@property (nonatomic, copy) NSString *type;
@end

MyAnnotation.m

@implementation MyAnnotation
@synthesize coordinate;
@synthesize title;
@synthesize subtitle;
@synthesize type;

@end

yourViewControll.m - 您显示 map 的位置。

创建注释并设置类型属性

MyAnnotation *Ann = [[MyAnnotation alloc] init];
offAnn.coordinate = offLocation.coordinate;
offAnn.title = @"My Office";
offAnn.type = @"Office";
[_mapView removeAnnotation: offAnn];
[_mapView addAnnotation: offAnn];




- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
NSLog(@"viewForAnnotation Called");
if ([annotation isKindOfClass:[MyAnnotation class]])
{
MyAnnotation *myAnn=(MyAnnotation *)annotation;

MKAnnotationView *pinView = (MKAnnotationView*)[self.mapView dequeueReusableAnnotationViewWithIdentifier:@"MyAnnotation"];
if (!pinView)
{
pinView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"MyAnnotation"];
//pinView.animatesDrop = YES;
pinView.canShowCallout = YES;
pinView.calloutOffset = CGPointMake(0, 4);

} else {
pinView.annotation = annotation;
}
if([myAnn.type isEqual: @"Office"]) {
pinView.image = [UIImage imageNamed:@"office.png"];
}
return pinView;
}
return nil;
}

关于IOS 自定义 pin 未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31289660/

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