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

ios - 如何创建符合 <MKAnnotation> 协议(protocol)的自定义类

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

我有一张显示所有餐厅对象的 map 。我正在尝试将餐厅对象传递给 map 注释,以便我可以显示包含所有餐厅信息的详细 View 。经过研究,我正在尝试创建一个符合协议(protocol)的类,但是我无法在 map 中放置注释。这是我的代码:

RestaurantAnnotationClass.h(自定义类):

#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
#import "Restaurant.h"

static NSString *restaurantAnnotationIdentifier = @"restaurantAnnotationIdentifier";

@interface RestaurantAnnotation : NSObject <MKAnnotation>

@property (copy, nonatomic) NSString *title;
@property (copy, nonatomic) NSString *subtitle;
@property (nonatomic) CLLocationCoordinate2D coordinate;
@property (nonatomic, strong) Restaurant *restaurant;


- (id)initWithTitleNSString *)newTitle subtitleNSString *)newSubtitle restaurantRestaurant *)newRestaurant locationCLLocationCoordinate2D)location;

- (MKAnnotationView *)annotationView;

@end

RestaurantAnnotationClass.m

    #import "RestaurantAnnotation.h"

@implementation RestaurantAnnotation



- (id)initWithTitleNSString *)newTitle subtitleNSString *)newSubtitle restaurantRestaurant *)newRestaurant locationCLLocationCoordinate2D)location {

    self = [super init];
    if (self) {
        self.title = newTitle;
        self.coordinate = location;
        self.subtitle = newSubtitle;
        self.restaurant = newRestaurant; 
    }

    return self;
}

- (MKAnnotationView *)annotationView {

    MKAnnotationView *annotationView = [[MKAnnotationView alloc] initWithAnnotation:self reuseIdentifier:restaurantAnnotationIdentifier];
    annotationView.enabled = YES;
    annotationView.canShowCallout = YES;
    annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];

    return annotationView;

}

@end

我的 View Controller 与 map

- (NSArray *)convertRestaurantToMKAnnotationsFromArrayNSArray *)restaurantsArray {
    NSMutableArray *mkPointAnnotations = [NSMutableArray new];

    double mileToMeters = 1609.344;
    CLLocation *userLocation = [[CLLocation alloc] initWithLatitude:self.userCurrentLocation.coordinate.latitude longitude:self.userCurrentLocation.coordinate.longitude];
    for (Restaurant *restaurant in restaurantsArray) {
        CLLocation *restaurantLocation = [[CLLocation alloc] initWithLatitude:restaurant.geoLocation.latitude longitude:restaurant.geoLocation.longitude];

       RestaurantAnnotation *newRestaurantAnnotation = [[RestaurantAnnotation alloc] initWithTitle:restaurant.name subtitle:[NSString stringWithFormat"%.02f",[userLocation distanceFromLocation:restaurantLocation] / mileToMeters] restaurant:restaurant location:CLLocationCoordinate2DMake(restaurant.geoLocation.latitude, restaurant.geoLocation.longitude)];

        [mkPointAnnotations addObject: newRestaurantAnnotation];

    }
    return mkPointAnnotations;
}

这里我实现了 vuewforannotation 方法

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


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

    if ([annotation isKindOfClass:[RestaurantAnnotation class]]) {
        RestaurantAnnotation *restaurant = (RestaurantAnnotation *)annotation;
        MKAnnotationView *restaurantAnnotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:restaurantAnnotationIdentifier];

        if (restaurantAnnotationView == nil)
            restaurantAnnotationView = restaurant.annotationView;
        else
            restaurantAnnotationView.annotation = annotation;

        return restaurantAnnotationView;
    } else
        return nil;

}

提前谢谢你



Best Answer-推荐答案


我在应用 viewForAnnotation 方法时出错了,这是我现在的代码

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

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

if ([annotation isKindOfClass:[RestaurantAnnotation class]]) {
    RestaurantAnnotation *restauntAnnotation = (RestaurantAnnotation *)annotation;
    MKAnnotationView *restaurantAnnotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:restaurantAnnotationIdentifier];
    if (restaurantAnnotationView == nil) {
        restaurantAnnotationView = restauntAnnotation.annotationView;
    } else {
        restaurantAnnotationView.annotation = annotation;
        return restaurantAnnotationView;
    }
}
return nil;

关于ios - 如何创建符合 <MKAnnotation> 协议(protocol)的自定义类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32323080/

回复

使用道具 举报

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

本版积分规则

关注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