#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. Python基础篇【第2篇】: Python自定义函数

    Python函数 1. 函数定义 函数是组织好的,可重复使用的,用来实现单一,或相关联功能的代码段.函数能提高应用的模块性,和代码的重复利用率.你已经知道Python提供了许多内建函数,比如print ...

  2. 自动improt的xcode插件 Auto-Importer

    https://github.com/lucholaf/Auto-Importer-for-Xcode

  3. python os.system()返回值判断

    最近遇到os.system()执行系统命令的情况,上网搜集了一下资料,整理如下,以备不时之需,同时也希望能帮到某些人. 一.python中的 os.system(cmd)的返回值与linux命令返回值 ...

  4. Flex数据交互之WebService

    In this article you will learn how to call webservices hosted on asp.net applications from flex. Fir ...

  5. POS管理系统之新增设备入库

    <%@ page language="java" import="java.util.*" pageEncoding="utf-8"% ...

  6. IOS畅销榜

    20W 在80-100  30W 在50左右  50W 在30左右  100W在20左右 N-1日的 收入  对N日还有权重的影响

  7. JS转义 escape()、encodeURI()、encodeURIComponent()区别详解

    JavaScript中有三个可以对字符串编码的函数,分别是: escape,encodeURI,encodeURIComponent,相应3个解码函数:unescape,decodeURI,decod ...

  8. 使用Synplify综合时保留logic

    在使用Synplify综合时,此工具会自动优化我的设计. 当然此功能有好有坏,最近有个项目需要使用Chipscope观察内部信号,打开inserter就懵了,信号列表中我的设计有的是名字被改了,有的是 ...

  9. objective c, protocol

    OC中协议类似于java中的接口,在多个类具有类似的方法时可以将这些方法定义到protocol中,然后各个类分别实现protocol中的各个方法. 例:有两个类Square和Circle, 定义一个p ...

  10. IE 下JS和CSS 阻塞后面内容总结

    总结: 1.  CSS 都是可以并行下载的. 2.  IE6 和 IE7   JS 不能并行下载,CSS 和 JS 阻塞后面内容下载. 3.  IE8   JS 还是会阻塞图片下载 开始改变加载模式, ...