gpt4 book ai didi

ios - "CustomAnnotation is tapped"在MKMapView中的使用

转载 作者:行者123 更新时间:2023-11-28 22:26:54 25 4
gpt4 key购买 nike

我需要将自定义注释 View 放入 mapview,并且它们需要可点击。我需要的不是经典标注,而是点击注释,它会执行一些功能。

我尝试了很多事情,但仍然没有实现。

最佳答案

我为您找到了一个非常好的实现:

http://blog.asolutions.com/2010/09/building-custom-map-annotation-callouts-part-1/

在那里你可以添加按钮到带有 Action 的calloutView

或者..

在 mapView 委托(delegate)中,您可以自定义注释和标注 View :

查看注释:返回与指定注释对象关联的注释 View (如果有)。

  • (MKAnnotationView *)viewForAnnotation:(id < MKAnnotation >)注释

有一个例子:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
MKAnnotationView *annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"loc"];

// Button
UIButton *button = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
button.frame = CGRectMake(0, 0, 23, 23);
annotationView.rightCalloutAccessoryView = button;

// Image and two labels
UIView *leftCAV = [[UIView alloc] initWithFrame:CGRectMake(0,0,23,23)];
[leftCAV addSubview : yourImageView];
[leftCAV addSubview : yourFirstLabel];
[leftCAV addSubview : yourSecondLabel];
annotationView.leftCalloutAccessoryView = leftCAV;

annotationView.canShowCallout = YES;

return annotationView;
}

自定义 PIN 的编辑答案:

创建一个 MKAnnotationView 子类

.h

#import <MapKit/MapKit.h>

@interface AnnotationView : MKAnnotationView

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

@end

.m

#import "AnnotationView.h"

@implementation AnnotationView

{
NSString *identifier;
}

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

{

self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];

if (self != nil)

{

CGRect frame = self.frame;

frame.size = CGSizeMake(78.0, 35.0);

self.frame = frame;

self.backgroundColor = [UIColor clearColor];

self.centerOffset = CGPointMake(0, 0);


}

return self;

}

-(void) drawRect:(CGRect)rect

{


[[UIImage imageNamed:@"yourImage"]drawInRect:CGRectMake(0, 0, 78, 35)];


}


@end

然后在 mapview 委托(delegate)中:

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

if ([annotation isKindOfClass:[MKUserLocation class]]) {
return nil;
}

if ([annotation isKindOfClass:[Annotation class]]) {

NSString* annotationIdentifier = @"yourAnnotation";
AnnotationView* customAnnotationView = (AnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:annotationIdentifier];

if (!customAnnotationView) {
customAnnotationView = [[AnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:annotationIdentifier];

customAnnotationView.canShowCallout = YES;
}

else{
customAnnotationView.annotation= annotation;
}


return customAnnotationView;
}
return nil;
}

注解类是我自定义的注解类:

@interface Annotation : NSObject <MKAnnotation>

希望对你有帮助

关于ios - "CustomAnnotation is tapped"在MKMapView中的使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18692948/

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