如何创建符合<mkannotation>协议的自定义类

时间:2015-09-01 03:29:04

标签: ios mkmapview mapkit mkannotation mkannotationview

我有一张显示所有餐馆对象的地图。我试图将餐馆对象传递给地图注释,以便我可以显示包含所有餐馆信息的详细视图。在研究之后,我试图创建一个符合协议的类,但是我还没有能够将注释放在地图中。这是我的代码:

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)initWithTitle:(NSString *)newTitle subtitle:(NSString *)newSubtitle restaurant:(Restaurant *)newRestaurant location:(CLLocationCoordinate2D)location;

- (MKAnnotationView *)annotationView;

@end

RestaurantAnnotationClass.m

    #import "RestaurantAnnotation.h"

@implementation RestaurantAnnotation



- (id)initWithTitle:(NSString *)newTitle subtitle:(NSString *)newSubtitle restaurant:(Restaurant *)newRestaurant location:(CLLocationCoordinate2D)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

我的视图带有地图的控制器

- (NSArray *)convertRestaurantToMKAnnotationsFromArray:(NSArray *)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 *)mapView:(MKMapView *)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;

}

提前谢谢

1 个答案:

答案 0 :(得分:1)

I was making a mistake when applying the method viewForAnnotation, this is my code now

- (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;