#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
@interface PPAnnotation : NSObject<MKAnnotation>
/**
* 大头针的位置
*/
@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
/**
* 大头针标题
*/
@property (nonatomic, copy) NSString *title;
/**
* 大头针的子标题
*/
@property (nonatomic, copy) NSString *subtitle;
/**
* 图标
*/
@property (nonatomic, copy) NSString *icon;
@end
------------------------------------------------------------------------------------------------------------------
#import <MapKit/MapKit.h>
@interface PPAnnotationView : MKAnnotationView
/**
* 快速创建 大头针
*
* @param mapView mapView
*/
+ (instancetype)annotationViewWithMap:(MKMapView *)mapView;
@end
#import "PPAnnotationView.h"
#import "PPAnnotation.h"
@implementation PPAnnotationView
- (instancetype)initWithAnnotation:(id<MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier
{
if (self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier]) {
// 初始化
// 设置显示标题
self.canShowCallout = YES;
// 设置辅助视图
self.leftCalloutAccessoryView = [[UISwitch alloc] init];
self.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeContactAdd];
}
return self;
}
+ (instancetype)annotationViewWithMap:(MKMapView *)mapView
{
static NSString *ID = @"anno";
// 1. 从缓存中取
// [注意] 默认情况下, MKAnnotationView 是无法显示的, 如果想自定义大头针, 需要使用MKAnnotationView的子类 MKPinAnnotationView
// [注意] 如果是自定义的大头针, 默认情况点击大头针不会显示标题和副标题, 需要手动设置显示
// MKPinAnnotationView *annoView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:ID];
PPAnnotationView *annoView = (PPAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:ID];
// 2. 如果缓存中没有, 创建一个新的大头针
if (annoView == nil) {
annoView = [[PPAnnotationView alloc] initWithAnnotation:nil reuseIdentifier:ID];
}
return annoView;
}
//- (void)setAnnotation:(id<MKAnnotation>)annotation
- (void)setAnnotation:(PPAnnotation *)annotation
{
[super setAnnotation:annotation];
// 处理自己特有的操作
self.image = [UIImage imageNamed:annotation.icon];
}
@end
---------------------------------------------------------------------------------------------
#import "ViewController.h"
//#import <MapKit/MapKit.h>
#import "PPAnnotation.h"
#import "PPAnnotationView.h"
@interface ViewController ()<MKMapViewDelegate>
/**
* 地图
*/
@property (weak, nonatomic) IBOutlet MKMapView *mapView;
@property (nonatomic, strong) CLLocationManager *mgr;
@property (nonatomic, strong) CLGeocoder *geocoder;
@end
@implementation ViewController
/**
* 添加大头针
*/
- (IBAction)addAnnotation:(id)sender {
PPAnnotation * annotation = [[PPAnnotation alloc] init];
annotation.title = @"北京智德创辉网络科技有限公司";
annotation.subtitle = @"RO";
annotation.icon = @"category_4";
CGFloat latitude = 39.915094 + arc4random_uniform(20);
CGFloat longitude = 116.487775 + arc4random_uniform(20);
annotation.coordinate = CLLocationCoordinate2DMake(latitude, longitude);
[self.mapView addAnnotation:annotation];
}
- (CLLocationManager *)mgr{
if (!_mgr) {
_mgr = [[CLLocationManager alloc] init];
}
return _mgr;
}
- (CLGeocoder *)geocoder{
if (!_geocoder) {
_geocoder = [[CLGeocoder alloc] init];
}
return _geocoder;
}
- (void)viewDidLoad {
[super viewDidLoad];
// ios 8
if ([[UIDevice currentDevice].systemVersion doubleValue] >= 8.0) {
[self.mgr requestAlwaysAuthorization];
}
// 设置代理
self.mapView.delegate = self;
// 设置模式
self.mapView.mapType = MKMapTypeStandard;
// 设置跟踪
self.mapView.userTrackingMode = MKUserTrackingModeFollow;
// 设置xuanzhuan
self.mapView.rotateEnabled = NO;
}
#pragma mark -MKMapViewDelegate
/**
* 每次添加大头针 都会调用此方法
*
* @param mapView 地图
* @param annotation 大头针模型
*/
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
LogYellow(@"%@",annotation);
// 对用户当前位置的大头针, 特殊处理 - MKUserLocation
if(![annotation isKindOfClass:[PPAnnotation class]]){
return nil;
}
// 1. 创建大头针
PPAnnotationView *annoView = [PPAnnotationView annotationViewWithMap:mapView];
// 2. 设置模型
annoView.annotation = annotation;
// 3. 返回大头针
return annoView;
}
/**
* 地图区域改变完成 会调用此方法
*/
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated
{
NSLog(@"地图区域改变完成");
/**
*
CLLocationCoordinate2D center;
MKCoordinateSpan span;
*/
LogRed(@"%f --- %f",self.mapView.region.span.latitudeDelta, self.mapView.region.span.longitudeDelta);
}
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
[self.geocoder reverseGeocodeLocation:userLocation.location completionHandler:^(NSArray *placemarks, NSError *error) {
CLPlacemark *placemark = [placemarks firstObject];
userLocation.title = placemark.name;
userLocation.subtitle = placemark.locality;
}];
// 移动地图到当前用户所在位置
[self.mapView setCenterCoordinate:userLocation.location.coordinate animated:YES];
/*
// 设置地图显示的区域
CLLocationCoordinate2D center = userLocation.location.coordinate;
// 指定经纬度的跨度
MKCoordinateSpan span = MKCoordinateSpanMake(0.001, 0.0001);
// 将用户的当前位置 设置为中心点, 并且制定显示的跨度
MKCoordinateRegion region = MKCoordinateRegionMake(center, span);
[self.mapView setRegion:region animated:YES];
*/
}