#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

MKMapView的使用的更多相关文章

  1. iOS - MKMapView 地图

    1.创建 MKMapView 地图 在 iOS6 或者 iOS7 中实现这个功能只需要添加地图控件.设置用户跟踪模式.在 mapView:didUpdateUserLocation: 代理方法中设置地 ...

  2. iOS 原生地图(MapKit、MKMapView)轨迹渐变

    WechatIMG2.png 项目已接入高德地图,并且大部分功能已经实现好,但BOSS觉得iOS自带的地图效果更好...本着面向老板编程的思想,换之.还好,高德地图是在MapKit上封装的,大部分ap ...

  3. CoreLocation MKMapView 地图

    系统自带地图  框架: CoreLocation MapKit CLLocationManager --> 定位管理者  CLGeocoder --> 地理编码器 MKMapView -- ...

  4. MKMapView的内存释放问题

    MKMapView的内存释放问题 by 伍雪颖 - (void)dealloc { self.mapView.showsUserLocation = NO; self.mapView.userTrac ...

  5. CoreLocation MKMapView

    高德开发者平台 有开发指南 iOS9配置网络: <key>NSAppTransportSecurity</key> <dict> <key>NSAllo ...

  6. MKMapView and Zoom Levels: A Visual Guide

    原帖:http://troybrant.net/blog/2010/01/mkmapview-and-zoom-levels-a-visual-guide/ So, how exactly does ...

  7. MKMapView移动事件地图

    MKMapView移动事件地图 by 吴雪莹 -(void)mapView:(MKMapView *)mapView regionWillChangeAnimated:(BOOL)animated { ...

  8. iOS 8中CLLocationManager及MKMapView showUserLocation失败的解决的方法

    用XCode 6编译的原来XCode 5.1.1写的程序时,发现原来写的CLLocationManager定位的代码以及MKmapView的showUserLocation失效.查了一下,XCode ...

  9. [ios]MKMapView中使用MKPolyline画线

    参考:http://blog.sina.com.cn/s/blog_9e8867eb0101dt76.html 首先在MapView.h中 #import <MapKit/MapKit.h> ...

随机推荐

  1. strong reference cycle in block

    However, because the reference is weak, the object that self points to could be deallocated while th ...

  2. .bashrc文件是干什么的(转)

    使用man bash命令查看到的联机帮助文件中的相关解释如下:.bashrc - The individual per-interactive-shell startup file. 这个文件主要保存 ...

  3. 常用的SQL分页

    有关分页 SQL 的资料很多,有的使用存储过程,有的使用游标.本人不喜欢使用游标,我觉得它耗资.效率低:使用存储过程是个不错的选择,因为存储过程是经过预编译的,执行效率高,也更灵活.先看看单条 SQL ...

  4. NUGET命令

    主题 about_NuGet 简短说明 提供有关 NuGet 程序包管理器命令的信息. 详细说明 本主题介绍 NuGet 程序包管理器命令.NuGet 是一种集成的程序包 管理工具,用于将库和工具添加 ...

  5. NHibernate系列文章十七:NHibernate Session管理(附程序下载)

    摘要 NHibernate的Session的管理涉及到NHibernate的两个最重要的对象ISessionFactory和ISession.ISessionFactory的生成非常消耗资源,通常都在 ...

  6. DataSanp的控制老大-DSServer

    DSServer作用:管理DataSnap服务器生命周期.(启动,停止) 一.方法: 1.BroadcastMessage 向所以客户端发送消息,客户端必须已注册通道. 2.BroadcastObje ...

  7. vi 使用

      1)命令 gf  ,可以从光标指定的文件位置打开对应文件 :bd回来 2)http://www.cnblogs.com/wangkangluo1/archive/2012/04/12/244495 ...

  8. OpenSSL命令系列

    1.1 ssl命令系列前言 openssl命令的格式是"openssl command command-options args",command部分有很多种命令,这些命令需要依赖 ...

  9. file access , argc, argv[ ]

    _____main函数含有 两个参数 ,argc ,argv[] 这两个参数用以指示命令行输入的参数信息. argc 的值是输入的参数的数量.argv是一个数组,每个数组元素指向一个string字符串 ...

  10. Vc++ 控件用法总结之List Control

    1.新建对话框MFC,在对话框上放一个ListCtrl ID:IDC_PATH View:Report 2.为ListCtrl添加变量 右击->添加变量m_wndPath 3.找到OnInitD ...