将自定义注释类添加到 map View 时,我无法访问它。我的自定义类工作正常,但当我将其添加到 map 时,我不确定如何通过此委托(delegate)访问自定义注释:
- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
我尝试在网上查找,但没有找到任何东西。任何帮助都会很棒。
它的名字是这样的:
CLLocationCoordinate2D coords = CLLocationCoordinate2DMake(shops.latitude, shops.longitude);
AnnotationForId *shop = [[AnnotationForId alloc] initWithCoordinate:coords];
//[[CLLocationCoordinate2DMake(shops.latitude, shops.longtitude)]];
shop.title = shops.name;
shop.subtitle = @"Coffee Shop";
shop.shopId = shops.id;
[map addAnnotation:shop];
最佳答案
这是如何创建自定义 AnnotationView 的简单示例。
创建自定义AnnotationView
:
#import <MapKit/MapKit.h>
@interface AnnotationView : MKPlacemark
@property (nonatomic, readwrite, assign) CLLocationCoordinate2D coordinate;
@property (nonatomic, strong) NSString *title;
@property (nonatomic, strong) NSString *subtitle;
// you can put here any controllers that you want. (such like UIImage, UIView,...etc)
@end
并在.m文件
#import "AnnotationView.h"
@implementation AnnotationView
- (id)initWithCoordinate:(CLLocationCoordinate2D)coordinate addressDictionary:(NSDictionary *)addressDictionary
{
if ((self = [super initWithCoordinate:coordinate addressDictionary:addressDictionary]))
{
self.coordinate = coordinate;
}
return self;
}
@end
//使用注释 在相关.m 文件
中添加#import "AnnotationView.h"
:
CLLocationCoordinate2D pCoordinate ;
pCoordinate.latitude = LatValue;
pCoordinate.longitude = LanValue;
// Create Obj Of AnnotationView class
AnnotationView *annotation = [[AnnotationView alloc] initWithCoordinate:pCoordinate addressDictionary:nil] ;
annotation.title = @"I m Here";
annotation.subtitle = @"This is Sub Tiitle";
[self.mapView addAnnotation:annotation];
关于iphone - 将自定义注释添加到 map 时使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15684080/