我想知道有没有MKAnnotationView类的其他子类,苹果文档上提到了MKPinAnnotationView这个例子,所以我想知道是否有其他预先创建的子类,就像用于跟踪设备当前位置的那个。如果有人有创建自己的MKAnnotationView子类的建议,那就太好了。
谢谢, bubster
我想知道有没有MKAnnotationView类的其他子类,苹果文档上提到了MKPinAnnotationView这个例子,所以我想知道是否有其他预先创建的子类,就像用于跟踪设备当前位置的那个。如果有人有创建自己的MKAnnotationView子类的建议,那就太好了。
谢谢, bubster
如果仍有人感兴趣:
您可以使用Objective-C运行时函数获取类的所有子类,具体方法在这里描述:http://cocoawithlove.com/2010/01/getting-subclasses-of-objective-c-class.html
其他继承自MKAnnotationView的类包括:
MKTransitCalloutView、MKAdAnnotationView、MKUserLocationView、MKUserLocationBreadCrumbView和MKPinAnnotationView
其中MKPinAnnotationView是唯一有文档支持的类,其他都是苹果内部使用的私有类。
MKAnnotation
协议的新类。您需要有两个类型为NSString*
的实例变量,分别命名为title
和subtitle
,一个类型为CLLocationCoordinate2D
的实例变量,命名为coordinate,并且一个适当的setter方法(例如属性)。这些字符串将显示在气泡中。在您的mapView的委托中,以类似于实现UITableView数据源的方式实现方法-mapView:viewForAnnotation:
。也就是说,通过标识符获取一个annotationView,设置新的属性(例如右附件视图的UIButtonTypeDetailDisclosure类型的按钮)。您还需要添加一个图像以显示在偏移下方。只需使用您的MKAnnotationView的image
属性即可。您自定义图像的中心将放置在指定的坐标处,因此您可能需要给出一个偏移量:aView.centerOffset = CGPointMake(0, -20)
以下是一些示例代码:- (MKAnnotationView *) mapView: (MKMapView *) mapView viewForAnnotation: (id<MKAnnotation>) annotation {
// reuse a view, if one exists
MKAnnotationView *aView = [mapView dequeueReusableAnnotationViewWithIdentifier:@"pinView"];
// create a new view else
if (!aView) {
aView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"pinView"];
}
// now configure the view
aView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[(UIButton*)aView.rightCalloutAccessoryView addTarget:self action:@selector(showDetails:) forControlEvents:UIControlEventTouchUpInside];
aView.canShowCallout = YES;
aView.enabled = YES;
aView.image = [UIImage imageNamed:@"green_pin.png"];
aView.centerOffset = CGPointMake(0, -20);
return aView;
}