@interface ViewController () <MKMapViewDelegate>
@property (nonatomic, weak) MKMapView *mapView;
@end
@implementation ViewController
#pragma mark - 懒加载
- (MKMapView *)mapView {
if (_mapView == nil) {
MKMapView *mapView = [[MKMapView alloc] initWithFrame:self.view.bounds];
CLLocationCoordinate2D coordinate = {39.9087607478,116.3975780499};
MKCoordinateSpan span = MKCoordinateSpanMake(0.01, 0.01);
MKCoordinateRegion region = MKCoordinateRegionMake(coordinate, span);
[mapView setRegion:region];
mapView.delegate = self;
[self.view addSubview:mapView];
_mapView = mapView;
}
return _mapView;
}
- (void)viewDidLoad {
[super viewDidLoad];
CLLocationCoordinate2D coordinate = {39.9087607478,116.3975780499};
CLLocationCoordinate2D coordinate2 = {39.8087607478,116.3975780499};
// 添加一个标注
MyAnnotation *annotation = [[MyAnnotation alloc] initWithCoordinate2D:coordinate title:@"北京市" subTitle:@"天安门"];
MyAnnotation *annotation2 = [[MyAnnotation alloc] initWithCoordinate2D:coordinate2 title:@"不知道" subTitle:@"不知道"];
[self.mapView addAnnotation:annotation];
[self.mapView addAnnotation:annotation2];
// 计算两个经纬度之间的距离
// 保存地理信息
CLLocation *l1 = [[CLLocation alloc] initWithLatitude:coordinate.latitude longitude:coordinate.longitude];
CLLocation *l2 = [[CLLocation alloc] initWithLatitude:coordinate2.latitude longitude:coordinate2.longitude];
// 单位是米
CLLocationDistance distance = [l1 distanceFromLocation:l2];
NSLog(@"---distance:%f", distance);
}
#pragma mark - MKMapView 代理方法
/**
* 添加标注的时候会触发
*/
- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views {
// MKPinAnnotationView -> MKAnnotationView -> UIView
NSLog(@"-----%@", views);
}
/**
* 返回自定义大头针视图 这里面的大头针视图会复用 类似于UITableViewCell的复用机制
*/
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
// 从复用队列里面通过标示符找可用的大头针对象 -> MKPinAnnotationView
MKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:@"identifier"];
if (annotationView == nil) {
annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"identifier"];
// 显示气泡 (三角视图)
annotationView.canShowCallout = YES;
// 添加大头针图片
annotationView.image = [UIImage imageNamed:@"map"];
// 左右视图
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"AppIcon57x57"]];
imageView.frame = CGRectMake(0, 0, 40, 40);
// 当cornerRadius设置为视图的一半 如果这个视图为正方形,则会变为圆形
imageView.layer.cornerRadius = imageView.frame.size.width * 0.5;
imageView.layer.masksToBounds = YES;
annotationView.leftCalloutAccessoryView = imageView;
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.backgroundColor = [UIColor lightGrayColor];
btn.frame = CGRectMake(0, 0, 40, 40);
[btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
annotationView.rightCalloutAccessoryView = btn;
}
return annotationView;
}
- (void)btnAction:(UIButton *)btn {
NSLog(@"%s", __FUNCTION__);
}
// 1. 自定义标注类 遵循MKAnnotation协议
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
@interface MyAnnotation : NSObject <MKAnnotation>
/**
* 构造方法
*/
- (instancetype)initWithCoordinate2D:(CLLocationCoordinate2D)coordinate2D
title:(NSString *)title
subTitle:(NSString *)subTitle;
@interface MyAnnotation () {
CLLocationCoordinate2D _coordinate2D;
NSString *_title;
NSString *_subTitle;
}
@end
@implementation MyAnnotation
#pragma mark - 初始化方法
- (instancetype)initWithCoordinate2D:(CLLocationCoordinate2D)coordinate2D
title:(NSString *)title
subTitle:(NSString *)subTitle {
self = [super init];
if (self) {
_coordinate2D = coordinate2D;
_title = title;
_subTitle = subTitle;
}
return self;
}
#pragma mark - MKAnnotation
- (CLLocationCoordinate2D)coordinate {
return _coordinate2D;
}
- (NSString *)title {
return _title;
}
- (NSString *)subtitle {
return _subTitle;
}