#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. Android UI 绘制过程浅析(一)LayoutInflater简介

    前言 这篇blog是我在阅读过csdn大牛郭霖的<带你一步步深入了解View>一系列文章后,亲身实践并做出的小结.作为有志向的前端开发工程师,怎么可以不搞懂View绘制的基本原理——简直就 ...

  2. 【转】web测试总结

    1.字符型输入框: (1)字符型输入框:英文全角.英文半角.数字.空或者空格.特殊字符“~!@#¥%……&*?[]{}”特别要注意单引号和&符号.禁止直接输入特殊字符时,使用“粘贴.拷 ...

  3. 纸上谈兵:AVL树

    作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 二叉搜索树的深度与搜索效率 我们在树, 二叉树, 二叉搜索树中提到,一个有n个节点 ...

  4. C++学习基础三——迭代器基础

    迭代器分为两种:一种是iterator,另一种是const_iterator.两者都可进行访问容器中的元素,不同之处是:(1)const_iterator类型只能用于读取容器内的元素,不能更改其值:而 ...

  5. PIC32MZ tutorial -- Input Capture

    Today I accomplish a simple application for PIC32MZ EC Starter Kit. This application uses Input Capt ...

  6. PIC32MZ tutorial -- Change Notification

    In my last post I implement "Key Debounce" with port polling, port polling is not very eff ...

  7. TabCtrl的基本用法

    MFC TabCtrl控件的使用方法及思路 1.建立基于Dialog的应用程序 2.添加TabCtrl控件,并关联变量 3.建立TabCtrl中存放的Dialog子对话框若干,并关联类 4.在Tabc ...

  8. Centos7 禁止firewalld并使用iptables 作默认防火墙以及忘记root密码的处理方法

    一.停止并禁用firewalld [root@test ~]# systemctl stop firewalld [root@test ~]# systemctl disable firewalld ...

  9. JQuery 简单的文字超出部分隐藏下拉显示

    HTML代码: <body> <div class="txt_bos"><!--在每一个放置文字的class外面包一个div,以便设置动画样式用,同样 ...

  10. C++ MFC获取软件运行目录 (包含软件名)

    TCHAR *path = new TCHAR[MAX_PATH];   GetModuleFileName(NULL,path,MAX_PATH); AfxMessageBox(path);