MKAnnotation 的自定义图像
我创建了一个注释,并将其添加到 MKMapView 中。我该如何使用自定义图像而不是标准的红色图钉?
@interface AddressAnnotation : NSObject<MKAnnotation> {
CLLocationCoordinate2D coordinate;
NSString *title;
NSString *subtitle;
MKPinAnnotationColor pinColor;
}
@property (nonatomic,retain) NSString *title;
@property (nonatomic,retain) NSString *subtitle;
@property (nonatomic, assign) MKPinAnnotationColor pinColor;
@end
I have created an annotation which I'm adding to an MKMapView. How would I go about having a custom image instead of the standard red pin?
@interface AddressAnnotation : NSObject<MKAnnotation> {
CLLocationCoordinate2D coordinate;
NSString *title;
NSString *subtitle;
MKPinAnnotationColor pinColor;
}
@property (nonatomic,retain) NSString *title;
@property (nonatomic,retain) NSString *subtitle;
@property (nonatomic, assign) MKPinAnnotationColor pinColor;
@end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
MKMapView 从其委托方法获取其 pin 视图 mapView:viewForAnnotation: 所以你必须:
将您的控制器设置为委托
使用委托协议标记接口。这让您可以在 Interface Builder (IB) 中将控制器设置为 MKMapView 的委托。打开包含地图的 .xib 文件,右键单击 MKMapView,然后将
delegate
出口拖动到控制器。如果您更喜欢使用代码而不是 IB,请在控制器的 viewDidLoad 方法中添加
self.yourMapView.delegate=self;
。结果是一样的。实现mapView:viewForAnnotation:
MKMapView gets its pin views from its delegate method mapView:viewForAnnotation: So you have to:
Set your controller as delegate
Mark the interface with the delegate protocol. This let's you set the controller as MKMapView's delegate in Interface Builder (IB). Open the .xib file containing your map, right click the MKMapView, and drag the
delegate
outlet to your controller.If you prefer to use code instead IB, add
self.yourMapView.delegate=self;
in the controller's viewDidLoad method. The result will be the same.Implement mapView:viewForAnnotation:
重写
mapView:viewForAnnotation:
委托方法。如果annotation
参数指向您的自定义注释之一,则返回一个符合您喜好的自定义视图。Override the
mapView:viewForAnnotation:
delegate method. If theannotation
param points to one of your custom annotations, return a custom view that looks to your liking.要设置自定义图像而不是标准
MKPinAnnotationView
,唯一的方法是将MKAnnotationView
与函数- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation 结合使用:(id)注释
。示例如下:对于
aView.image
值,您可以设置自己的图像。还要查看MKAnnotationView
类引用可以更好地处理它。To set custom image instead of standart
MKPinAnnotationView
the only way to do that is to useMKAnnotationView
with function- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
. Here's the example:For the
aView.image
value You can set Your own image. And also look intoMKAnnotationView
class reference to handle better with it.