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 使用 问题 文件 数据 ...
随机推荐
- GPU安装
安装驱动 https://developer.nvidia.com/cuda-downloads?target_os=Linux&target_arch=x86_64&target_d ...
- BZOJ.4514.[SDOI2016]数字配对(费用流SPFA 二分图)
BZOJ 洛谷 \(Solution\) 很显然的建二分图后跑最大费用流,但有个问题是一个数是只能用一次的,这样二分图两部分都有这个数. 那么就用两倍的.如果\(i\)可以向\(j'\)连边,\(j\ ...
- Linux 系统及编程相关知识总汇
Linux C function() 参考手册 STL 学习文档 Linux内核
- UVALive 6908 Electric Bike dp
Electric Bike 题目连接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8 ...
- A CANBus Tiny Network without Transceiver ICs : STM32F4 Discovery
Sometimes you have a CAN equipped processor on a low cost board but it has no CAN transceiver chips. ...
- Answer Sheet - Both, Either & Neither
http://www.usingenglish.com/quizzes/answers.php?quiz_id=44 This page displays the answers to the 'Bo ...
- IAR EWARM Checksum Technical Note
IELFTOOL Checksum - Basic actions EW targets: ARM, RH850, RX, SH, STM8 EW component: General issues ...
- Xamarin.Android,Xamarin.iOS, Linking
Xamarin.Android applications use a linker in order to reduce the size of the application. The linker ...
- 布局控件Grid
XAML概述 Silverlight的控件绘制是由XAML语言进行支持的.什么是XAML语言? 简单的说,XAML(Extensible Application Markup Language )是一 ...
- CentOS安装sctp协议
转自:http://blog.csdn.net/fly_yr/article/details/48375247 序 最近学习Unix网络编程,在第10章节,SCTP客户/服务器 程序实现时,发现很多由 ...