#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. RabbitMQ(一)

    官网:http://www.rabbitmq.com/ 一.什么是RabbitMQ(官方话)? 1.稳健的应用消息系统 2.容易使用 3.可以运行在主流操作系统上 4.支持大部分的开发平台(Java. ...

  2. ORACLE-SELECT学习

    (一)select格式:SELECT [ ALL | DISTINCT ] <字段表达式1[,<字段表达式2[,…] FROM <表名1>,<表名2>[,…] [W ...

  3. hive查询

    查询语句可以直接使用非分析函数的变量的别名 select t1.* from ( select account_id, sum(recharge_money) recharge_money_total ...

  4. JS,Jquery获取各种屏幕的宽度和高度

    Javascript: 网页可见区域宽: document.body.clientWidth网页可见区域高: document.body.clientHeight网页可见区域宽: document.b ...

  5. Android IOS WebRTC 音视频开发总结(七二)-- 看到Google Duo,你想到了什么?

    本文主要介绍在线教育这个行业,文章最早发表在我们的微信公众号上,支持原创,详见这里, 欢迎关注微信公众号blackerteam,更多详见www.rtc.help 在昨天的Google I/O大会上Go ...

  6. jQuery ajax()使用serialize()提交form数据

    jQuery的serialize()方法通过序列化表单值,创建URL编码文本字符串,我们就可以选择一个或多个表单元素,也可以直接选择form将其序列化 <form action="&q ...

  7. Maven实战(五)坐标详解

    1.为什么要定义Maven坐标      在我们开发Maven项目的时候,需要为其定义适当的坐标,这是Maven强制要求的.在这个基础上,其他Maven项目才能应用该项目生成的构件. 2.Maven坐 ...

  8. 解决Cannot modify header information - headers already sent by

    output_buffering = On ,在php.ini中设置.

  9. Spring —— 三种配置数据源的方式:spring内置、c3p0、dbcp

    01.Spring内置数据源配置Class:DriverManagerDataSource全限定名:org.springframework.jdbc.datasource.DriverManagerD ...

  10. php 万能加密

    function fue($hash,$times) { // Execute the encryption(s) as many times as the user wants for($i=$ti ...