ios - 如何创建符合 <MKAnnotation> 协议(protocol)的自定义类
<p><p>我有一张显示所有餐厅对象的 map 。我正在尝试将餐厅对象传递给 map 注释,以便我可以显示包含所有餐厅信息的详细 View 。经过研究,我正在尝试创建一个符合协议(protocol)的类,但是我无法在 map 中放置注释。这是我的代码:</p>
<h1>RestaurantAnnotationClass.h(自定义类):</h1>
<pre><code>#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
</code></pre>
<h1>RestaurantAnnotationClass.m</h1>
<pre><code> #import "RestaurantAnnotation.h"
@implementation RestaurantAnnotation
- (id)initWithTitle:(NSString *)newTitle subtitle:(NSString *)newSubtitle restaurant:(Restaurant *)newRestaurant location:(CLLocationCoordinate2D)location {
self = ;
if (self) {
self.title = newTitle;
self.coordinate = location;
self.subtitle = newSubtitle;
self.restaurant = newRestaurant;
}
return self;
}
- (MKAnnotationView *)annotationView {
MKAnnotationView *annotationView = [ initWithAnnotation:self reuseIdentifier:restaurantAnnotationIdentifier];
annotationView.enabled = YES;
annotationView.canShowCallout = YES;
annotationView.rightCalloutAccessoryView = ;
return annotationView;
}
@end
</code></pre>
<h1>我的 ViewController 与 map </h1>
<pre><code>- (NSArray *)convertRestaurantToMKAnnotationsFromArray:(NSArray *)restaurantsArray {
NSMutableArray *mkPointAnnotations = ;
double mileToMeters = 1609.344;
CLLocation *userLocation = [ initWithLatitude:self.userCurrentLocation.coordinate.latitude longitude:self.userCurrentLocation.coordinate.longitude];
for (Restaurant *restaurant in restaurantsArray) {
CLLocation *restaurantLocation = [ initWithLatitude:restaurant.geoLocation.latitude longitude:restaurant.geoLocation.longitude];
RestaurantAnnotation *newRestaurantAnnotation = [ initWithTitle:restaurant.name subtitle: / mileToMeters] restaurant:restaurant location:CLLocationCoordinate2DMake(restaurant.geoLocation.latitude, restaurant.geoLocation.longitude)];
;
}
return mkPointAnnotations;
}
</code></pre>
<p>这里我实现了 vuewforannotation 方法</p>
<pre><code>- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
if (])
return nil;
if (]) {
RestaurantAnnotation *restaurant = (RestaurantAnnotation *)annotation;
MKAnnotationView *restaurantAnnotationView = ;
if (restaurantAnnotationView == nil)
restaurantAnnotationView = restaurant.annotationView;
else
restaurantAnnotationView.annotation = annotation;
return restaurantAnnotationView;
} else
return nil;
}
</code></pre>
<p>提前谢谢你</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>我在应用 viewForAnnotation 方法时出错了,这是我现在的代码</p>
<pre><code>- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
if (]) {
return nil;
}
if (]) {
RestaurantAnnotation *restauntAnnotation = (RestaurantAnnotation *)annotation;
MKAnnotationView *restaurantAnnotationView = ;
if (restaurantAnnotationView == nil) {
restaurantAnnotationView = restauntAnnotation.annotationView;
} else {
restaurantAnnotationView.annotation = annotation;
return restaurantAnnotationView;
}
}
return nil;
</code></pre></p>
<p style="font-size: 20px;">关于ios - 如何创建符合 <MKAnnotation> 协议(protocol)的自定义类,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/32323080/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/32323080/
</a>
</p>
页:
[1]