gpt4 book ai didi

ios - MKPinAnnotationView 不工作

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:47:38 26 4
gpt4 key购买 nike

我想要一个 MKMapView 显示带有公开按钮的注释,这会导致像 the Golden Gate Bridge annotation in this Apple sample app 这样的 View Controller .

我从 plist 加载坐标,注释正确显示标题/副标题,但方法

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

没有效果。

我想我必须以某种方式将注释与 pinannotations 链接起来?

MapViewController.h:

#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
#import "Annotation.h"

@interface MapViewController : UIViewController<CLLocationManagerDelegate, MKMapViewDelegate>

@property (strong, nonatomic) CLLocationManager *location;
@property (nonatomic, retain) NSArray *data;
@end

MapViewController.m:

#import "MapViewController.h"

@interface MapViewController ()
@property (nonatomic, weak) IBOutlet MKMapView *mapView;
@end

@implementation MapViewController
@synthesize data;
@synthesize location, minLatitude, maxLatitude, minLongitude, maxLongitude;

- (void)viewDidLoad
{
NSString *dataPath = [[NSBundle mainBundle] pathForResource:@"City" ofType:@"plist"];
self.data = [NSArray arrayWithContentsOfFile:dataPath];

for (int i = 0; i < data.count; i++) {

NSDictionary *dataItem = [data objectAtIndex:i];

//Create Annotation
Annotation *building = [[Annotation alloc] init];
building.title = [dataItem objectForKey:@"Title"];
building.subtitle = [dataItem objectForKey:@"Subtitle"];

MKCoordinateRegion buildingcoordinates = { {0.0, 0.0}, {0.0, 0.0} };
buildingcoordinates.center.latitude = [[dataItem objectForKey:@"Latitude"] floatValue];
buildingcoordinates.center.longitude = [[dataItem objectForKey:@"Longitude"] floatValue];

building.coordinate = buildingcoordinates.center;
[self.mapView addAnnotation:building];

}

[super viewDidLoad];

}

- (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>)annotation
{
if ([annotation isKindOfClass:[MKUserLocation class]])
return nil;

static NSString *pinIdentifier = @"pinIndentifier";

MKPinAnnotationView *pinView = (MKPinAnnotationView *)
[self.mapView dequeueReusableAnnotationViewWithIdentifier:pinIdentifier];
if (pinView == nil)
{
// if an existing pin view was not available, create one
MKPinAnnotationView *customPinView = [[MKPinAnnotationView alloc]
initWithAnnotation:annotation reuseIdentifier:pinIdentifier];
customPinView.pinColor = MKPinAnnotationColorPurple;
customPinView.animatesDrop = YES;
customPinView.canShowCallout = YES;

UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[rightButton addTarget:self
action:@selector(showDetails:)
forControlEvents:UIControlEventTouchUpInside];
customPinView.rightCalloutAccessoryView = rightButton;

return customPinView;
}
else
{
pinView.annotation = annotation;
}
return pinView;
}

注解.h:

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

@interface Annotation : NSObject <MKAnnotation> {
CLLocationCoordinate2D coordinate;
NSString *title;
NSString *subtitle;
}

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

@end

注释.m:

#import "Annotation.h"

@implementation Annotation

@synthesize coordinate, title, subtitle;

@end

最佳答案

很可能 map View 的 delegate 没有设置,这意味着 viewForAnnotation 委托(delegate)方法将不会被调用。

由于您已将 mapView 声明为 IBOutlet,因此在 xib 中,请确保 map View 的 delegate 已连接到文件所有者。

或者,在 MapViewController 中 viewDidLoad 方法的顶部,以编程方式设置它:

mapView.delegate = self;

关于ios - MKPinAnnotationView 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14616622/

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