MKMapView中Annotation的使用

#import "ViewController.h"
#import 
#import "TRAnnotation.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet MKMapView *mapView;
@property (nonatomic, strong) CLLocationManager *mgr;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    //1.初始化manager对象
    self.mgr = [CLLocationManager new];
    //2.征求用户的同意/授权(>=iOS8.0+假设用户同意)+Info.plist添加key
    [self.mgr requestWhenInUseAuthorization];
    //3.设置mapView属性(和定位相关属性)
    self.mapView.userTrackingMode = MKUserTrackingModeFollow;
    //4.设置mapView的代理
    self.mapView.delegate = self;
    //设置旋转;地图类型
    self.mapView.rotateEnabled = NO;
    //默认是标准类型(standard)
    self.mapView.mapType = MKMapTypeStandard;
}

//监听用户的位置
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation {
    //修改弹出框的两个文本(上下)
    userLocation.title = @"用户的位置";
    userLocation.subtitle = @"位置的详细描述...";
    
    //打印用户的位置
    NSLog(@"纬度%f; 经度%f", userLocation.location.coordinate.latitude, userLocation.location.coordinate.longitude);
}
//监听用户挪动地图停止的时机
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated {
    //region:设备上显示的那部分称为地图视图的区域
    NSLog(@"用户挪动地图视图停止...");
}
//添加标注对象(大头针对象)
static int count = 0;
- (IBAction)addAnnotations:(id)sender {
    //V2: 1.创建标注对象
    TRAnnotation *annotation = [TRAnnotation new];
    //2.设置属性(经纬度;两个标题)
    CLLocationDegrees latitude = 39.001 + arc4random_uniform(10);//0~9
    CLLocationDegrees longitude = 116.001 + arc4random_uniform(10); //0~9
    annotation.coordinate = CLLocationCoordinate2DMake(latitude, longitude);
    annotation.title = @"1602...";
    //V2: 人为地给定条件:偶数给定红色图片;奇数给定蓝色图片
    if (count %2 == 0) {
        annotation.subtitle = @"我是偶数偶数...";
        annotation.image = [UIImage imageNamed:@"icon_paopao_waterdrop_streetscape"];
    } else {
        //奇数
        annotation.subtitle = @"我是奇数奇数...";
        annotation.image = [UIImage imageNamed:@"icon_pin_floating"];
    }
    count++;
    //3.添加到地图视图mapView上
    [self.mapView addAnnotation:annotation];
    
    //把每次新添加的大头针对象作为区域region的中心(代码挪动地图)
    //center:中心;span:跨度
    MKCoordinateSpan span = MKCoordinateSpanMake(0.5, 0.2);
    MKCoordinateRegion region = MKCoordinateRegionMake(annotation.coordinate, span);
    [self.mapView setRegion:region animated:YES];
}

//需求:设定标注对象的自定义图片(看懂)
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id)annotation {
    if ([annotation isKindOfClass:[MKUserLocation class]]) {
        return nil;
    }
    static NSString *identifier = @"annotation";
    MKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
    if (!annotationView) {
        annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
        annotationView.canShowCallout = YES;
        //设置自定义图片40x60
//        annotationView.image = [UIImage imageNamed:@"icon_pin_floating"];
        
        //V2:把annotation的类型强转成自定义的TRAnnotation类型
        TRAnnotation *anno = (TRAnnotation *)annotation;
        //V2:把标注对象的image值赋值给标注视图的image属性
        annotationView.image = anno.image;
        annotationView.leftCalloutAccessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon_classify_cafe"]];
    } else {
        annotationView.annotation = annotation;
    }

    return annotationView;
}

@end

你可能感兴趣的:(MKMapView中Annotation的使用)