首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MKPinAnnotationView不工作

MKPinAnnotationView不工作
EN

Stack Overflow用户
提问于 2013-01-31 08:44:11
回答 1查看 2.9K关注 0票数 2

我想有一个显示注释的MKMapView -按钮,这导致了一个像the Golden Gate Bridge annotation in this Apple sample app的视图控制器。

我从plist加载坐标,注释正确显示为title/subtitle,但方法

代码语言:javascript
复制
- (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>)annotation

没有任何效果。

我想我必须以某种方式将注解与link注解联系起来?

MapViewController.h:

代码语言:javascript
复制
#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:

代码语言:javascript
复制
#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;
}

Annotation.h:

代码语言:javascript
复制
#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:

代码语言:javascript
复制
#import "Annotation.h"

@implementation Annotation

@synthesize coordinate, title, subtitle;

@end
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-01-31 09:44:18

很可能没有设置映射视图的delegate,这意味着不会调用viewForAnnotation委托方法。

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

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

代码语言:javascript
复制
mapView.delegate = self;
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14616622

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档