简单理解MapView 以及 设置 MKAnnotationView

MKMapView  相当于一个容器 。可以展示  MKAnnotationView..

要使用它需要设置 数据源代理  

    _mapView.delegate = self;

 它的数据源对象就是 

符合   MKAnnotation   协议的对象 

包含

@protocol MKAnnotation <NSObject>

// Center latitude and longitude of the annotion view.
// The implementation of this property must be KVO compliant.
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;

@optional

// Title and subtitle for use by selection UI.
@property (nonatomic, readonly, copy) NSString *title;
@property (nonatomic, readonly, copy) NSString *subtitle;

// Called as a result of dragging an annotation view.
- (void)setCoordinate:(CLLocationCoordinate2D)newCoordinate NS_AVAILABLE(10_9, 4_0);

@end

这里不得不关联下 最常用的tableView 控件 

它也需要数据源   和 代理 

需要把数据源  加载到 tableViewCell 上 显示

对比mapView ,它的数据源是  (id<MKAnnotation>)annotation

- (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id<MKAnnotation>)annotation {

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath...

对比下  是不是很相识 。。

同样的还有 uipickerView

下面实现自定义  MKAnnotationView 跟自定义UITabelViewcell  差不多

http://www.codigator.com/tutorials/advanced-mapkit-tutorial-for-ios-custom-callout/

实现  不点击annotation 也能显示calloutview 

添加 代理方法

- (void)mapView:(MKMapView *)mv didAddAnnotationViews:(NSArray *)views {
        
    for (MKAnnotationView *annoView in views) {
                
        MKAnnotation *anno = annoView.annotation;
                [mv selectAnnotation:anno animated:YES]; 
    }
}
原文地址:https://www.cnblogs.com/DamonTang/p/3684707.html