//
// Annotation.h
// 自定义大头针模型
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
@interface Annotation : NSObject<MKAnnotation>
@property(nonatomic,assign)CLLocationCoordinate2D coordinate;
@property(nonatomic,copy)NSString *title;
@property(nonatomic,copy)NSString *subtitle;
/**
* 图标
*/
@property(nonatomic,copy)NSString *icon;
//
// ViewController.m
//
#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>
#import "Annotation.h"
@interface ViewController ()<CLLocationManagerDelegate,MKMapViewDelegate>
/**
* 位置管理者对象
*/
@property(nonatomic,strong) CLLocationManager *locMgr;
- (IBAction)addAnnotations:(UIButton *)sender;
@property (weak, nonatomic) IBOutlet MKMapView *mapView;
@end
@implementation ViewController
/**
* 延迟创建定位管理者对象
*/
- (CLLocationManager *)locMgr
{
//如果定位服务不可用,加一个判断
if(![CLLocationManager locationServicesEnabled]) return nil;
if (_locMgr == nil) {
//创建定位管理者对象
self.locMgr =[[CLLocationManager alloc]init];
//设置代理
self.locMgr.delegate = self;
}
return _locMgr;
}
- (IBAction)addAnnotations:(UIButton *)sender {
//添加大头针
Annotation *anno1 = [[Annotation alloc] init];
anno1.title = @"大饭店";
anno1.subtitle = @"全场9折,会员8折";
anno1.icon = @"category_1";
anno1.coordinate = CLLocationCoordinate2DMake(39.9, 115);
[self.mapView addAnnotation:anno1];
Annotation *anno2 = [[Annotation alloc] init];
anno2.title = @"xxx影院";
anno2.subtitle = @"最新大片:xxxxxxx";
anno2.icon = @"category_5";
anno2.coordinate = CLLocationCoordinate2DMake(39, 115);
[self.mapView addAnnotation:anno2];
}
- (void)viewDidLoad {
[super viewDidLoad];
//用户授权
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0){
[self.locMgr requestWhenInUseAuthorization]; //调用了这句,就会弹出允许框了.
}
//1.设置追踪用户的位置(这个设置了就回显示蓝色发光的圆点)
// self.mapView.userTrackingMode = MKUserTrackingModeFollow;
//2.设置代理
self.mapView.delegate = self;
}
#pragma mark - MKMapViewDelegate
/**
* 返回大头针的View
*/
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
//0.如果不是自定义的大头针模型就排除(系统默认的大头针还是使用系统的view样式)
if (![annotation isKindOfClass:[Annotation class]]) {
return nil;
}
//1.创建MKAnnotationView(从缓存池取)
static NSString *ID = @"tg";
MKAnnotationView *annoView = [mapView dequeueReusableAnnotationViewWithIdentifier:ID];
if (annotation == nil) {
annoView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:ID];
//大头针上显示描述(标题和子标题)
annoView.canShowCallout = YES;
annoView.opaque = NO;
//设置大头针描述的偏移量
annoView.calloutOffset = CGPointMake(0, -10);
//设置大头针描述右边控件
annoView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeContactAdd];
//设置大头针描述左边控件
annoView.leftCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
// UIImageView *imageView = [[UIImageView alloc] init];
// imageView.image = [UIImage imageNamed:@"category_1"];
// annoView.detailCalloutAccessoryView = imageView;
}
//2.传递模型
annoView.annotation = annotation;
//设置图片
Annotation *tgAnnotation = annotation;
annoView.image = [UIImage imageNamed:tgAnnotation.icon];
//3.返回
return annoView;
}
/**
* 当用户位置更新就会调用
*/
//- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
//{
// //设置地图的显示范围
// MKCoordinateSpan span = MKCoordinateSpanMake(0.5, 0.5);
// MKCoordinateRegion region = MKCoordinateRegionMake(userLocation.location.coordinate, span);
// [mapView setRegion:region];
//}
@end