地图功能的实现

因为有个项目要在地图中显示位置,所以用到了MapKit。

记录下来,以免以后忘记。

加入MapKit library

首先得在项目中加入MapKit,如图

MapView

先增加一个ViewController,我这里用的storyboard,这个玩意还是挺好用的,比以前用xib好多了。

然后拖一个mapview上去,如:

给新增加的ViewController绑定一个class。首先得增加一个class,从uiViewController继承下来。这个很简单,如图

把新增加的ViewController绑定到这个class,也很easy,发现Xcode还是挺牛的。就是在右边Identity inspector里面的custom class里面改成新增加的类,原来是UIViewController。

然后给map view控件绑定一个变量,类型是MKMapView

然后就初始化mapview,显示。代码如下:

 1 - (void)viewDidLoad
2 {
3 [super viewDidLoad];
4 // Do any additional setup after loading the view.
5
6 _mapView.mapType = MKMapTypeStandard;//标准模式
7 _mapView.showsUserLocation = YES;//显示自己
8
9 _mapView.zoomEnabled = YES;//支持缩放
10
11
12 CLLocationCoordinate2D pos = {39.931203, 116.395573};//找个坐标,我是用百度坐标抓取弄的。http://api.map.baidu.com/lbsapi/getpoint/
13
14 MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(pos,2000, 2000);//以pos为中心,显示2000米
15 MKCoordinateRegion adjustedRegion = [_mapView regionThatFits:viewRegion];//适配map view的尺寸
16 [_mapView setRegion:adjustedRegion animated:YES];
17
18
19 }

我这里使用百度坐标,找了个坐标(直接搜索“百度 坐标”),然后在我们自己的地图里显示。这样运行一下就可以看到:

Map view delegate 回调

可以实现协议MKMapViewDelegate, 这样就会有几个回调。

 1 - (void) mapViewWillStartLoadingMap:(MKMapView *)mapView//开始从服务器获取地图数据
2 {
3
4 }
5
6 -(void)mapViewDidFinishLoadingMap:(MKMapView *)mapView//获取数据结束
7 {
8
9 }
10
11 - (void) mapViewDidFailLoadingMap:(MKMapView *)mapView withError:(NSError *)error//获取数据失败了。
12 {
13
14 }

获取设备当前位置并且在地图中显示

增加一个按钮,点击这个按钮,将显示设备当前位置。点击上面的按钮将显示某个固定位置。

CLLocationManager,首先使用CLLocationManager来获取设备的当前位置。

代码也是很简单

 1 //获得自己的当前的位置信息
2 - (void) getCurPosition
3 {
4 //开始探测自己的位置
5 if (locationManager==nil)
6 {
7 locationManager =[[CLLocationManager alloc] init];
8 }
9
10
11 if ([CLLocationManager locationServicesEnabled])
12 {
13 locationManager.delegate=self;
14 locationManager.desiredAccuracy=kCLLocationAccuracyBest;
15 locationManager.distanceFilter=10.0f;
16 [locationManager startUpdatingLocation];
17 }
18 }

然后实现回调函数

 1 #pragma mark -- CLLocationManagerDelegate
2 - (void) locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
3 {
4 if ([locations count] > 0) {
5 CLLocation* loc = [locations objectAtIndex:0];
6 CLLocationCoordinate2D pos = [loc coordinate];
7
8 NSLog(@"locationManager, longitude: %f, latitude: %f", pos.longitude, pos.latitude);
9
10 if (show == NO) {
11 MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(pos,2000, 2000);//以pos为中心,显示2000米
12 MKCoordinateRegion adjustedRegion = [_mapView regionThatFits:viewRegion];//适配map view的尺寸
13 [_mapView setRegion:adjustedRegion animated:YES];
14
15 show = YES;
16 }
17 }
18 }

当设备位置变化时,这个函数会被调用。这样我们就可以根据位置来做一些事情了。这个例子里就在第一次获取位置的时候更新一下地图显示。以设备当前位置为中心,显示2000米。

完了。贴一下mapview所在的controller代码:

  1 //
2 // KMapViewController.m
3 // MapDemo
4 //
5 // Created by Kevin on 14-2-10.
6 // Copyright (c) 2014年 Kevin. All rights reserved.
7 //
8
9 #import "KMapViewController.h"
10
11 @interface KMapViewController ()
12
13 @end
14
15 @implementation KMapViewController
16
17 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
18 {
19 self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
20 if (self) {
21 // Custom initialization
22 }
23 return self;
24 }
25
26 - (void)viewDidLoad
27 {
28 [super viewDidLoad];
29 // Do any additional setup after loading the view.
30
31 show = NO;
32
33 _mapView.mapType = MKMapTypeStandard;//标准模式
34 _mapView.showsUserLocation = YES;//显示自己
35 _mapView.delegate = self;
36 _mapView.zoomEnabled = YES;//支持缩放
37
38
39 NSString* i = self.Index;
40
41 if([i isEqualToString:@"1"])
42 {
43 CLLocationCoordinate2D pos = {39.931203, 116.395573};//找个坐标,我是用百度坐标抓取弄的。http://api.map.baidu.com/lbsapi/getpoint/
44
45 MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(pos,2000, 2000);//以pos为中心,显示2000米
46 MKCoordinateRegion adjustedRegion = [_mapView regionThatFits:viewRegion];//适配map view的尺寸
47 [_mapView setRegion:adjustedRegion animated:YES];
48
49 }
50 else
51 {
52 [self getCurPosition];
53 }
54
55 }
56
57 - (void)didReceiveMemoryWarning
58 {
59 [super didReceiveMemoryWarning];
60 // Dispose of any resources that can be recreated.
61 }
62
63 - (void) dealloc
64 {
65
66 // [super dealloc];
67 }
68
69 //获得自己的当前的位置信息
70 - (void) getCurPosition
71 {
72 //开始探测自己的位置
73 if (locationManager==nil)
74 {
75 locationManager =[[CLLocationManager alloc] init];
76 }
77
78
79 if ([CLLocationManager locationServicesEnabled])
80 {
81 locationManager.delegate=self;
82 locationManager.desiredAccuracy=kCLLocationAccuracyBest;
83 locationManager.distanceFilter=10.0f;
84 [locationManager startUpdatingLocation];
85 }
86 }
87
88 #pragma mark -- MPMapViewDelegate
89
90 - (void) mapViewWillStartLoadingMap:(MKMapView *)mapView
91 {
92
93 }
94
95 -(void)mapViewDidFinishLoadingMap:(MKMapView *)mapView
96 {
97
98 }
99
100 - (void) mapViewDidFailLoadingMap:(MKMapView *)mapView withError:(NSError *)error
101 {
102
103 }
104
105 #pragma mark -- CLLocationManagerDelegate
106 - (void) locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
107 {
108 if ([locations count] > 0) {
109 CLLocation* loc = [locations objectAtIndex:0];
110 CLLocationCoordinate2D pos = [loc coordinate];
111
112 NSLog(@"locationManager, longitude: %f, latitude: %f", pos.longitude, pos.latitude);
113
114 if (show == NO) {
115 MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(pos,2000, 2000);//以pos为中心,显示2000米
116 MKCoordinateRegion adjustedRegion = [_mapView regionThatFits:viewRegion];//适配map view的尺寸
117 [_mapView setRegion:adjustedRegion animated:YES];
118
119 show = YES;
120 }
121 }
122 }
123
124 @end
 

iOS开发——高级技术&地图功能的实现的更多相关文章

  1. iOS开发——高级技术OC篇&运行时(Runtime)机制

    运行时(Runtime)机制 本文将会以笔者个人的小小研究为例总结一下关于iOS开发中运行时的使用和常用方法的介绍,关于跟多运行时相关技术请查看笔者之前写的运行时高级用法及相关语法或者查看响应官方文档 ...

  2. iOS开发——高级技术精选OC篇&Runtime之字典转模型实战

    Runtime之字典转模型实战 如果您还不知道什么是runtime,那么请先看看这几篇文章: http://www.cnblogs.com/iCocos/p/4734687.html http://w ...

  3. iOS开发——高级技术&广告服务

    广告服务 上 面也提到做iOS开发另一收益来源就是广告,在iOS上有很多广告服务可以集成,使用比较多的就是苹果的iAd.谷歌的Admob,下面简单演示一下如何 使用iAd来集成广告.使用iAd集成广告 ...

  4. iOS开发——高级技术&内购服务

    内购服务 大家都知道做iOS开发本身的收入有三种来源:出售应用.内购和广告.国内用户通常很少直接 购买应用,因此对于开发者而言(特别是个人开发者),内购和广告收入就成了主要的收入来源.内购营销模式,通 ...

  5. iOS开发——高级技术&签名机制

    签名机制 最近看了objc.io上第17期中的文章 <Inside Code Signing> 对应的中文翻译版 <代码签名探析> ,受益颇深,对iOS代码签名机制有了进一步的 ...

  6. iOS开发——高级技术&摇一摇功能的实现

    摇一摇功能的实现 在AppStore中多样化功能越来越多的被使用了,所以今天就开始介绍一些iOS开发的比较实用,但是我们接触的比较少的功能,我们先从摇一摇功能开始 在 UIResponder中存在这么 ...

  7. iOS开发——高级技术&调用地图功能的实现

    调用地图功能的实现 一:苹果自带地图 学习如逆水行舟,不进则退.古人告诉我们要不断的反思和总结,日思则日精,月思则月精,年思则年精.只有不断的尝试和总结,才能让我们的工作和生活更加 轻松愉快和美好.连 ...

  8. iOS开发——高级技术&蓝牙服务

    蓝牙服务 蓝牙 随着蓝牙低功耗技术BLE(Bluetooth Low Energy)的发展,蓝牙技术正在一步步成熟,如今的大部分移动设备都配备有蓝牙4.0,相比之前的蓝牙技术耗电量大大降低.从iOS的 ...

  9. iOS开发——高级技术&PassBook服务

    PassBook服务 Passbook是苹果推出的一个管理登机牌.会员卡.电影票.优惠券等信息的 工具.Passbook就像一个卡包,用于存放你的购物卡.积分卡.电影票.礼品卡等,而这些票据就是一个“ ...

随机推荐

  1. haploview出现“more than two alleles”的解决方法

    弹出“more than two alleles”的错误是因为ped文件中一个SNP位点上存在两个以上的等位基因,haploview连锁分析时默认为只有两个等位基因,因此我们要去掉超过两位等位基因的S ...

  2. 用nodej和glub-watcher写的监听go 项目自动编译,很鸡肋

    glub 一般都是很轻量的编译. go太重了,改一小个部分,就编译的话,多数是编译失败. 而且很消耗性能,还没想到完美的优化办法. 暂时用个定时器 监听2秒,停止1秒,如此循环,会减少些 “无效”的编 ...

  3. cinder ha

  4. PoEdu - C++阶段班【Po学校】- 第1课

    1 C++开讲 C ++  伟大的编程语言:能提高程序运行效率,节约更多的资源,"正确的使用C++,能够抑制全球变暖问题". 2 C++能力雷达图 通过 1效率 2灵活度 3 抽象 ...

  5. TestNG中用Parameters或DataProvider为测试方法传入参数

    转载于网络   一.设置参数 测试方法是可以带有参数的.每个测试方法都可以带有任意数量的参数,并且可以通过使用TestNG的@Parameters向方法传递正确的参数. 设置方式有两种方法:使用 te ...

  6. ab压力测试和CC预防

    这两天从服务器上拉数据时,发现取回的数据不正确,而客户端当初健壮性不强,导致解析的数据为空. 起初以为是服务器维护的问题,可今天服务器登陆上了,发现还是数据不正确,正好昨天数据部的哥们告诉了我一个比较 ...

  7. Java学习笔记 第一章 入门<转>

    第一章 JAVA入门 一.基础常识 1.软件开发 什么是软件? 软件:一系列按照特定顺序组织的计算机数据和指令的集合 系统软件:DOS,Windows,Linux 应用软件:扫雷.QQ.迅雷 什么是开 ...

  8. 树状DP (poj 2342)

    题目:Anniversary party 题意:给出N各节点的快乐指数,以及父子关系,求最大快乐指数和(没人职员愿意跟直接上司一起玩): 思路:从底向上的树状DP: 第一种情况:第i个员工不参与,F[ ...

  9. span width无效

    在默认情况下label.span 设置width 是无效的.一般要display属性 display:block; 但是他会自动加一个换行,如果不想换行的话,可以用 display:inline-bl ...

  10. 如何获取eID——公安部发行的网络实名认证方式

    var appInsights=window.appInsights||function(config){ function r(config){t[config]=function(){var i= ...