• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - MKPinAnnotationView 不起作用

[复制链接]
菜鸟教程小白 发表于 2022-12-13 00:42:19 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

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

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

- (MKAnnotationView *)mapViewMKMapView *)theMapView viewForAnnotationid <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 *)mapViewMKMapView *)theMapView viewForAnnotationid <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
                            actionselector(showDetails
                  forControlEvents:UIControlEventTouchUpInside];
            customPinView.rightCalloutAccessoryView = rightButton;

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

Annotation.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

Annotation.m:

#import "Annotation.h"

@implementation Annotation

@synthesize coordinate, title, subtitle;

@end



Best Answer-推荐答案


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

由于您已将 mapView 声明为 IBOutlet,因此在 xib 中,请确保 map View 的 delegate 已连接到 File's Owner。

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

mapView.delegate = self;

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

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap