iOS开发-iOS8地理位置定位
现在的App基本上都有定位功能,旅游网站根据定位推荐旅游景点,新闻App通过地理位置推荐当地新闻,社交类的App通过位置交友,iOS中实现以上功能需要一个核心的框架CoreLocation,框架提供了一些服务可以获取和定位用户当前的位置。服务会通过一种低功耗的方式通知用户地理位置的变化,iOS中三种地位方式, Wifi定位(通过查询一个Wifi路由器的地理位置的信息),蜂窝基站定位(通过移动运用商基站定位) 和GPS卫星定位(准确度最高,耗电量最大)。
1.新建一个iOS项目,在ViewController中导入核心框架(#import <CoreLocation/CoreLocation.h>);
2.定义一个CLLocationManager变量,实现CLLocationManagerDelegate协议,CLLocationManager负责具体的实现;
ViewController.h中代码:
- #import <UIKit/UIKit.h>
- #import <CoreLocation/CoreLocation.h>
- @interface ViewController : UIViewController<CLLocationManagerDelegate>
- {
- CLLocationManager *mylocationManager;
- }
- @end
ViewDidLoad方法中代码:
- self.view.backgroundColor=[UIColor greenColor];
- if (nil == mylocationManager)
- mylocationManager = [[CLLocationManager alloc] init];
- mylocationManager.delegate = self;
- //设置定位的精度
- mylocationManager.desiredAccuracy = kCLLocationAccuracyKilometer;
- //设置定位服务更新频率
- mylocationManager.distanceFilter = 500;
- if ([[[UIDevice currentDevice] systemVersion] doubleValue]>=8.0)
- {
- [mylocationManager requestWhenInUseAuthorization];// 前台定位
- NSLog(@"当前的版本是%f",[[[UIDevice currentDevice] systemVersion] doubleValue]);
- //[mylocationManager requestAlwaysAuthorization];// 前后台同时定位
- }
- [mylocationManager startUpdatingLocation];
效果图如下:
3.如果不能弹出以上信息,你需要在Info.plist文件中设置一下,加入一个NSLocationWhenInUseUsageDescription(前台获取GPS定位),NSLocationAlwaysUsageDescription(前后台获取GPS定位),Value可以为空;
4.常用方法调用:
大多数协议中都会包含一个处理失败的方法,CoreLocationDelegate中的didFailWithError:
- -(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
- NSLog(@"FlyElephant-http://www.cnblogs.com/xiaofeixiang");
- }
获取变化的之后地理位置didUpdateLocations,locations是按时间先后顺序的集合:
- //地理定位完成之后的一个数组
- -(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
- //获取最新的位置
- CLLocation * currentLocation = [locations lastObject];
- CLLocationDegrees latitude=currentLocation.coordinate.latitude;
- CLLocationDegrees longitude=currentLocation.coordinate.longitude;
- NSLog(@"didUpdateLocations当前位置的纬度:%.2f--经度%.2f",latitude,longitude);
- }
获取地理位置变化的起始点和终点,didUpdateToLocation:
- - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
- CLLocationDegrees latitude=newLocation.coordinate.latitude;
- CLLocationDegrees longitude=oldLocation.coordinate.longitude;
- NSLog(@"didUpdateToLocation当前位置的纬度:%.2f--经度%.2f",latitude,longitude);
- }
比较简单,关于具体的使用可以参考苹果官方文档https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/LocationAwarenessPG/CoreLocation/CoreLocation.html#//apple_ref/doc/uid/TP40009497-CH2-SW1
iOS开发-iOS8地理位置定位的更多相关文章
- iOS开发--地图与定位
概览 现在很多社交.电商.团购应用都引入了地图和定位功能,似乎地图功能不再是地图应用 和导航应用所特有的.的确,有了地图和定位功能确实让我们的生活更加丰富多彩,极大的改变了我们的生活方式.例如你到了一 ...
- iOS开发系列--地图与定位
概览 现在很多社交.电商.团购应用都引入了地图和定位功能,似乎地图功能不再是地图应用和导航应用所特有的.的确,有了地图和定位功能确实让我们的生活更加丰富多彩,极大的改变了我们的生活方式.例如你到了一个 ...
- 转-iOS开发系列--地图与定位
来自: http://www.cnblogs.com/kenshincui/p/4125570.html#autoid-3-4-0 概览 现在很多社交.电商.团购应用都引入了地图和定位功能,似乎地图功 ...
- iOS开发——高级篇——地理定位 CoreLocation
一.CoreLocation 在移动互联网时代,移动app能解决用户的很多生活琐事,比如周边:找餐馆.找KTV.找电影院等等导航:根据用户设定的起点和终点,进行路线规划,并指引用户如何到达 在上述应用 ...
- iOS开发----地图与导航--定位和位置信息获取
要实现地图.导航功能,往往需要先熟悉定位功能,在iOS中通过Core Location框架进行定位操作.Core Location自身可以单独使用,和地图开发框架MapKit完全是独立的,但是往往地图 ...
- ios开发——实用技术OC篇&地图与定位
地图与定位 11.1 iOS定位服务 11.2 iOS地图 11.3 Web地图 1 iOS定位服务 iOS中有三个定位服务组件: Wifi定位,通过查询一个Wifi路由器的地理位置的信息.比较省电, ...
- iOS开发拓展篇—CoreLocation定位服务
iOS开发拓展篇—CoreLocation定位服务 一.简单说明 1.CLLocationManager CLLocationManager的常用操作和属性 开始用户定位- (void)startUp ...
- 《iOS开发指南》要改iOS8版本了,听听您的意见?
<iOS开发指南>要改iOS8版本了,听听您的意见?参加问卷同学均可获得智捷课堂50元代金卡一张,同时抽取一名同学赠送即将出版的基于iOS8的<iOS开发指南>一本,欢迎大家填 ...
- 转:【iOS开发每日小笔记(十一)】iOS8更新留下的“坑” NSAttributedString设置下划线 NSUnderlineStyleAttributeName 属性必须为NSNumber
http://www.bubuko.com/infodetail-382485.html 标签:des class style 代码 html 使用 问题 文件 数据 ...
随机推荐
- BZOJ4556 HEOI2016字符串
没错,又是这题,使用后缀自动机,反向建树,主席树维护right集合. By:大奕哥 #include<bits/stdc++.h> using namespace std; ; ]; ch ...
- 【LCA+MST】BZOJ3732-Network
[题目大意] 给你N个点的无向图 (1 <= N <= 15,000),记为:1…N.图中有M条边 (1<=M<=30,000) ,第j条边的长度:d_j (1<=d_j ...
- Codeforces Round #374 (Div. 2) C. Journey DP
C. Journey 题目连接: http://codeforces.com/contest/721/problem/C Description Recently Irina arrived to o ...
- LPC43xx SGPIO Configuration -- Why not use GPDMA ?
LPC43xx SGPIO Configuration The LPC43xx SGPIO peripheral is used to move samples between USB and the ...
- IAR EWARM 关闭纯汇编函数的警告的方法
/关闭警告 #pragma diag_suppress=Pe940 uint8_t GetMyData(void) { asm(“MOV R0, #0x550F”); } //恢复警告 #pragma ...
- Linux Shell脚本入门--wc命令
wc 统计文件里面有多少单词,多少行,多少字符. wc语法 [root@www ~]# wc [-lwm] 选项与参数: -l :仅列出行: -w :仅列出多少字(英文单字): -m :多少字符: 默 ...
- python dtrace 安装与应用
https://ipfans.github.io/2016/09/tracing-python-program-with-dtrace/?utm_source=tuicool&utm_medi ...
- C程序设计的抽象思维-递归过程-砝码称重
[问题] 在狄更斯时代,商人们用砝码和天平来称量商品的重量,假设你仅仅有几个砝码,就仅仅能精确地称出一定的重量.比如,假定仅仅有两个砝码:各自是1kg和3kg. 仅仅用1kg的砝码能够称出1kg重量的 ...
- Revit API创建墙的保温层修改墙厚度
start [Transaction(TransactionMode.Manual)] [Regeneration(RegenerationOption.Manual)] / ; ; ...
- 没有可用的复制构造函数或复制构造函数声明为“explicit”
c:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\vector(810) : error C2558: str ...