简体   繁体   English

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

[英]how to create a custom class that conforms to <MKAnnotation> protocol

I have a map that is displaying all restaurant objects.我有一张显示所有餐厅对象的地图。 I'm trying to pass the restaurant object to the map annotation so that i can display a detail view with all the restaurant info.我正在尝试将餐厅对象传递给地图注释,以便我可以显示包含所有餐厅信息的详细视图。 After researching, I'm trying to create a class that conforms to protocols, however I haven't been able to place the annotation in the map.经过研究,我正在尝试创建一个符合协议的类,但是我一直无法在地图中放置注释。 Here is my code:这是我的代码:

RestaurantAnnotationClass.h (custom class): 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 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

My view Controller with the map我的带有地图的视图控制器

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

here im implementing the vuewforannotation method我在这里实现了 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;

}

thank you in advance先感谢您

I was making a mistake when applying the method viewForAnnotation, this is my code now我在应用 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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM