#import "ViewController.h"

#import "BVAnnotation.h"

@interface ViewController ()<MKMapViewDelegate>

{

CLLocationManager *locationManager;//声明管理者对象

MKMapView *_mapView;

}

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

//    初始化管理者对象

locationManager = [[CLLocationManager alloc]init];

//    2.判断系统服务是否开启

if (![CLLocationManager locationServicesEnabled]) {

NSLog(@"系统服务未开启");

}

//    3.判断授权状态

if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined) {

//如果是未授权状态时,则进行授权

//        授权是要修改plist文件

if ([[[UIDevice currentDevice]systemVersion]floatValue]>= 8.0) {

[locationManager requestWhenInUseAuthorization];

}

}

//    创建MKMapView

_mapView = [[MKMapView alloc]initWithFrame:self.view.bounds];

//    设置mapview的属性

//    设置mapView的跟踪模式

_mapView.userTrackingMode = MKUserTrackingModeFollow;

//  设置mapView的类型

_mapView.mapType =   MKMapTypeStandard;

//    设置代理

_mapView.delegate = self;

[self.view addSubview:_mapView];

#pragma-mark 添加button 用来控制回到定位的位置中心点

UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(10, self.view.bounds.size.height-50-10, 80, 50)];

btn.backgroundColor = [UIColor cyanColor];

[btn addTarget:self action:@selector(btnAction:) forControlEvents:

UIControlEventTouchUpInside];

[self.view addSubview:btn];

#pragma -mark 通过点击手势添加大头针

UITapGestureRecognizer *tap =[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(follow:)];

[_mapView addGestureRecognizer:tap];

}

#pragma -mark 通过点击手势添加大头针的点击方法

-(void)follow:(UITapGestureRecognizer *)tap{

CGPoint point = [tap locationInView:_mapView];

//    把坐标点转换成地理坐标

CLLocationCoordinate2D coordinate = [_mapView convertPoint:point toCoordinateFromView:_mapView];

//   在该地理坐标位置创建大头针

BVAnnotation *annotation = [[BVAnnotation alloc]init];

annotation.coordinate = coordinate;

annotation.title = @"中华大地";

annotation.subtitle  = @"欢迎你";

annotation.image = [UIImage imageNamed:@"category_4"];

[_mapView addAnnotation:annotation];

}

#pragma -mark 自定义大头针的样式

-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{

//    判断这个大头针是不是系统定位的大头针 如果是,则不用重新定义

//    方法的使用类似于UiTableViewCell的创建和复用

if ([annotation isKindOfClass: [BVAnnotation class]])

{

MKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:@"annotationView"];

if (annotationView == nil) {

annotationView = [[MKAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@"annotationView"];

//            设置选中时显示详情框

annotationView.canShowCallout = YES;

//            添加辅助视图(左右两边)

annotationView.leftCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeContactAdd];

//            annotationView.image = [UIImage imageNamed:@""];

}

annotationView.annotation = annotation;

annotationView.image = ((BVAnnotation *)annotation).image;

return annotationView;

}

return nil;

}

#pragma -mark 回到定位的位置的but事件响应的方法

-(void)btnAction:(UIButton *)button{

[ _mapView setCenterCoordinate:_mapView.userLocation.location.coordinate animated:YES];

}

#pragma -mark 代理方法

-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{

//    完成位置更新的代理方法

NSLog(@"完成更新");

//    设置地图显示区域

MKCoordinateSpan span = MKCoordinateSpanMake(1, 1);

MKCoordinateRegion region = MKCoordinateRegionMake(userLocation.coordinate, span);

[mapView setRegion:region];

}

-(void)mapViewDidFinishLoadingMap:(MKMapView *)mapView{

//   地图完成加载

NSLog(@"完成加载");

}

========BVAnnotation==========

#import <Foundation/Foundation.h>

#import <MapKit/MapKit.h>

@interface BVAnnotation : NSObject<MKAnnotation>

//重新定义协议里的属性

@property (nonatomic, assign) CLLocationCoordinate2D coordinate;

@property (nonatomic, copy) NSString *title;

@property (nonatomic,copy) NSString *subtitle;

@property(nonatomic,retain)UIImage *image;

@end

最新文章

  1. 关于我-dinphy简介
  2. C++ vector容器find查询函数
  3. 模拟Jquery选择器
  4. VS2010 根据模型生成数据库 打开edmx.sql文件时 vs出现无响应的解决方案
  5. CUDA编程学习(二)
  6. MQ的通讯模式
  7. Android下拉刷新完全解析,教你如何一分钟实现下拉刷新功能 (转)
  8. 用CRT connect MongoDB 使用Backspace无效
  9. nyoj 84 阶乘的0
  10. 【进阶1-3期】JavaScript深入之内存空间详细图解(转)
  11. 安装 VMWare ESXi 6.7:VMB: 548: Unsupported CPU:6.7版本的ESXi 不支持 某些cpu了
  12. kafka 的quick start(windows平台)
  13. Tomcat必会的企业级配置调优
  14. Intellij idea上传项目到github
  15. scrapy抓取小说
  16. 搜索R包和查看包的技巧
  17. Linux 入门记录:四、Linux 系统常用命令
  18. 初学者配置第一个spring mvc Demo
  19. JNuit
  20. C#中 String和string的区别

热门文章

  1. iOS开发中的4种数据持久化方式【二、数据库 SQLite3、Core Data 的运用】
  2. 初始化成员列表 ——— 类的const成员和引用成员的初始化
  3. C#(Winform) Http 发送数据
  4. Linux命令总结:file
  5. NHibernate系列文章十一:NHibernate并发控制
  6. Failed to load the JNI shared library jvm.dll
  7. javaweb学习第一天 debug
  8. maven提示invalid LOC header (bad signature)的解决办法
  9. modelsim(2) - vcd (dump, 查看,格式理解)
  10. Python 之Django