#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>
@interface ViewController () <MKMapViewDelegate>
{
CLLocationManager *_manager;
MKMapView *_mapView;
}
@property (nonatomic,strong) CLLocationManager *manager;
@property (nonatomic,strong) MKMapView *mapView;;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self creatMapView];
[self creatAnnoations];
[self createLongPress];
}
- (void)creatMapView {
//定位管理
self.manager = [[CLLocationManager alloc] init];
CGFloat v = [[[UIDevice currentDevice] systemVersion] doubleValue];
if (v >= 8.0) {
//申请验证
[self.manager requestAlwaysAuthorization];
}
//实例化地图
self.mapView = [[MKMapView alloc] initWithFrame:self.view.bounds];
//设置地图的类型
/*
MKMapTypeStandard = 0,//标准的
MKMapTypeSatellite,卫星地图
*/
self.mapView.mapType = MKMapTypeStandard;
//设置 地图显示的区域范围(地图在视图上的中心点)
/*
typedef struct {
CLLocationCoordinate2D center;//经纬度
MKCoordinateSpan span;//缩放比例(0.01--0.05)
} MKCoordinateRegion;
*/
self.mapView.region = MKCoordinateRegionMake(CLLocationCoordinate2DMake(34.77274892, 113.67591140), MKCoordinateSpanMake(0.01, 0.01));
//是否显示用户位置
self.mapView.showsUserLocation = YES;
//设置代理
self.mapView.delegate = self;
//粘贴到视图上
[self.view addSubview:self.mapView];
NSArray *titles = @[@"move",@"回到用户位置"];
for (NSInteger i = 0; i < titles.count; i++) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(200*i, 100, 100, 30);
[button setTitle:titles[i] forState:UIControlStateNormal];
button.tag = 101+i;
[button addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
}
- (void)btnClick:(UIButton *)button {
if (button.tag == 101) {
//移动-->更改 地图的中心点位置
//获取地理位置
CLLocationCoordinate2D coordinate = self.mapView.centerCoordinate;
coordinate.latitude += 0.01;
coordinate.longitude += 0.01;
//更改中心点区域
[self.mapView setCenterCoordinate:coordinate animated:YES];
}else{
//回到用户原来当前位置
//获取用户位置
CLLocation * userLocation = self.mapView.userLocation.location;
self.mapView.region = MKCoordinateRegionMake(userLocation.coordinate, MKCoordinateSpanMake(0.01, 0.01));
}
}
#pragma mark - 大头针/点标注
- (void)creatAnnoations {
//先创建地图再创建大头针
/*
点标注:1.点标注视图(视图) 2.点标注数据(数据模型)
*/
//创建点标注数据(大头针数据)
MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
//设置大头针的经纬度
annotation.coordinate = CLLocationCoordinate2DMake(34.77274892, 113.67591140);
annotation.title = @"农贸市场";
annotation.subtitle = @"纬五路21号";
//把大头针数据添加到地图上 增加 一个遵守 MKAnnotation的对象
[self.mapView addAnnotation:annotation];
}
#pragma mark - MKMapViewDelegate
/**
* 当大头针视图显示的时候调用
*创建的都是大头针视图
* @param mapView 地图
* @param annotation 传入的大头针数据模型
*
* @return 大头针视图
*/
/*
所有的点标注视图的父类 MKAnnotationView
MKPinAnnotationView 是 MKAnnotationView 的子类
*/
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
//采用 复用机制
/**
* annotation 和 数据模型类似
* MKAnnotationView UITableViewCell类似
* mapView 和UITableView类似
我们在地图上加了多少 annotation 数据 当前方法就会调用多少,就会传入多少个annotation
*/
//判断是哪一类大头针数据
if ([annotation isKindOfClass:[MKPointAnnotation class]]) {
//从复用队列获取 大头针视图MKPinAnnotationView
MKPinAnnotationView *pinView = (MKPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:@"MKPinAnnotationView"];
if (!pinView) {
//没有那么创建
//根据 点标注模型 annotation 显示 大头针的内容
pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"MKPinAnnotationView"];
}
//设置大头针视图的属性
pinView.animatesDrop = YES;//是否有掉落的效果
/*
MKPinAnnotationColorRed = 0,
MKPinAnnotationColorGreen,
MKPinAnnotationColorPurple
*/
//设置颜色
pinView.pinColor = MKPinAnnotationColorRed;
//显示气泡
pinView.canShowCallout = YES;
//设置 气泡左侧附件
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 30)] ;
view.backgroundColor = [UIColor redColor];
pinView.leftCalloutAccessoryView = view;
//设置右侧附件
UIButton *button = [UIButton buttonWithType:UIButtonTypeInfoLight];
button.frame = CGRectMake(0, 0, 30, 30);
//气泡的左右侧附件 如果是UIControl 子类那么不需要增加事件大头针视图会自动添加事件
//
//不用添加事件
pinView.rightCalloutAccessoryView = button;
return pinView;
}
return nil;
}
#pragma mark - 当气泡附件是一个UIControl 子类的时候 点击会调用
//点击气泡右侧附件(必须是UIControl的子类)
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control {
NSLog(@"气泡附件被点击");
}
//大头针视图被选中
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view {
NSLog(@"大头针被选中");
}
#pragma mark - 长按
- (void)createLongPress {
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
//加到地图上
[self.mapView addGestureRecognizer:longPress];
}
- (void)longPress:(UILongPressGestureRecognizer *)longPress {
//长按的时候会触发两次(长按开始 和长按结束)
if (longPress.state == UIGestureRecognizerStateBegan) {
//手势开始的时候
//获取长按 地图视图 点得位置
CGPoint point = [longPress locationInView:self.mapView];
//把 一个视图上的 坐标位置转化为 一个地图上的地理经纬度
//相对于 mapView
CLLocationCoordinate2D coordinate = [self.mapView convertPoint:point toCoordinateFromView:self.mapView];
//创建 大头针模型
MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
annotation.coordinate = coordinate;
annotation.title = @"Wyon";
annotation.subtitle = @"my home";
//增加到地图
[self.mapView addAnnotation:annotation];
//地图视图会 刷新视图
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>
@interface ViewController () <MKMapViewDelegate>
{
CLLocationManager *_manager;
MKMapView *_mapView;
}
@property (nonatomic,strong) CLLocationManager *manager;
@property (nonatomic,strong) MKMapView *mapView;;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self creatMapView];
[self creatAnnoations];
[self createLongPress];
}
- (void)creatMapView {
//定位管理
self.manager = [[CLLocationManager alloc] init];
CGFloat v = [[[UIDevice currentDevice] systemVersion] doubleValue];
if (v >= 8.0) {
//申请验证
[self.manager requestAlwaysAuthorization];
}
//实例化地图
self.mapView = [[MKMapView alloc] initWithFrame:self.view.bounds];
//设置地图的类型
/*
MKMapTypeStandard = 0,//标准的
MKMapTypeSatellite,卫星地图
*/
self.mapView.mapType = MKMapTypeStandard;
//设置 地图显示的区域范围(地图在视图上的中心点)
/*
typedef struct {
CLLocationCoordinate2D center;//经纬度
MKCoordinateSpan span;//缩放比例(0.01--0.05)
} MKCoordinateRegion;
*/
self.mapView.region = MKCoordinateRegionMake(CLLocationCoordinate2DMake(34.77274892, 113.67591140), MKCoordinateSpanMake(0.01, 0.01));
//是否显示用户位置
self.mapView.showsUserLocation = YES;
//设置代理
self.mapView.delegate = self;
//粘贴到视图上
[self.view addSubview:self.mapView];
NSArray *titles = @[@"move",@"回到用户位置"];
for (NSInteger i = 0; i < titles.count; i++) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(200*i, 100, 100, 30);
[button setTitle:titles[i] forState:UIControlStateNormal];
button.tag = 101+i;
[button addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
}
- (void)btnClick:(UIButton *)button {
if (button.tag == 101) {
//移动-->更改 地图的中心点位置
//获取地理位置
CLLocationCoordinate2D coordinate = self.mapView.centerCoordinate;
coordinate.latitude += 0.01;
coordinate.longitude += 0.01;
//更改中心点区域
[self.mapView setCenterCoordinate:coordinate animated:YES];
}else{
//回到用户原来当前位置
//获取用户位置
CLLocation * userLocation = self.mapView.userLocation.location;
self.mapView.region = MKCoordinateRegionMake(userLocation.coordinate, MKCoordinateSpanMake(0.01, 0.01));
}
}
#pragma mark - 大头针/点标注
- (void)creatAnnoations {
//先创建地图再创建大头针
/*
点标注:1.点标注视图(视图) 2.点标注数据(数据模型)
*/
//创建点标注数据(大头针数据)
MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
//设置大头针的经纬度
annotation.coordinate = CLLocationCoordinate2DMake(34.77274892, 113.67591140);
annotation.title = @"农贸市场";
annotation.subtitle = @"纬五路21号";
//把大头针数据添加到地图上 增加 一个遵守 MKAnnotation的对象
[self.mapView addAnnotation:annotation];
}
#pragma mark - MKMapViewDelegate
/**
* 当大头针视图显示的时候调用
*创建的都是大头针视图
* @param mapView 地图
* @param annotation 传入的大头针数据模型
*
* @return 大头针视图
*/
/*
所有的点标注视图的父类 MKAnnotationView
MKPinAnnotationView 是 MKAnnotationView 的子类
*/
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
//采用 复用机制
/**
* annotation 和 数据模型类似
* MKAnnotationView UITableViewCell类似
* mapView 和UITableView类似
我们在地图上加了多少 annotation 数据 当前方法就会调用多少,就会传入多少个annotation
*/
//判断是哪一类大头针数据
if ([annotation isKindOfClass:[MKPointAnnotation class]]) {
//从复用队列获取 大头针视图MKPinAnnotationView
MKPinAnnotationView *pinView = (MKPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:@"MKPinAnnotationView"];
if (!pinView) {
//没有那么创建
//根据 点标注模型 annotation 显示 大头针的内容
pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"MKPinAnnotationView"];
}
//设置大头针视图的属性
pinView.animatesDrop = YES;//是否有掉落的效果
/*
MKPinAnnotationColorRed = 0,
MKPinAnnotationColorGreen,
MKPinAnnotationColorPurple
*/
//设置颜色
pinView.pinColor = MKPinAnnotationColorRed;
//显示气泡
pinView.canShowCallout = YES;
//设置 气泡左侧附件
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 30)] ;
view.backgroundColor = [UIColor redColor];
pinView.leftCalloutAccessoryView = view;
//设置右侧附件
UIButton *button = [UIButton buttonWithType:UIButtonTypeInfoLight];
button.frame = CGRectMake(0, 0, 30, 30);
//气泡的左右侧附件 如果是UIControl 子类那么不需要增加事件大头针视图会自动添加事件
//
//不用添加事件
pinView.rightCalloutAccessoryView = button;
return pinView;
}
return nil;
}
#pragma mark - 当气泡附件是一个UIControl 子类的时候 点击会调用
//点击气泡右侧附件(必须是UIControl的子类)
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control {
NSLog(@"气泡附件被点击");
}
//大头针视图被选中
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view {
NSLog(@"大头针被选中");
}
#pragma mark - 长按
- (void)createLongPress {
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
//加到地图上
[self.mapView addGestureRecognizer:longPress];
}
- (void)longPress:(UILongPressGestureRecognizer *)longPress {
//长按的时候会触发两次(长按开始 和长按结束)
if (longPress.state == UIGestureRecognizerStateBegan) {
//手势开始的时候
//获取长按 地图视图 点得位置
CGPoint point = [longPress locationInView:self.mapView];
//把 一个视图上的 坐标位置转化为 一个地图上的地理经纬度
//相对于 mapView
CLLocationCoordinate2D coordinate = [self.mapView convertPoint:point toCoordinateFromView:self.mapView];
//创建 大头针模型
MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
annotation.coordinate = coordinate;
annotation.title = @"Wyon";
annotation.subtitle = @"my home";
//增加到地图
[self.mapView addAnnotation:annotation];
//地图视图会 刷新视图
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end