IOS开发笔记整理49之详解定位CLLocation

2016-02-19 10:39 9 1 收藏

下面这个IOS开发笔记整理49之详解定位CLLocation教程由图老师小编精心推荐选出,过程简单易学超容易上手,喜欢就要赶紧get起来哦!

【 tulaoshi.com - 编程语言 】

在项目功能中有一个定位CLLocation的需求,遇到了一些知识难点,经过各位大侠的帮助,问题解决,特此分享供大家学习,希望大家共同学习进步。

一、简单说明

1.CLLocationManager

CLLocationManager的常用操作和属性

开始用户定位- (void)startUpdatingLocation;

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

停止用户定位- (void) stopUpdatingLocation;

说明:当调用了startUpdatingLocation方法后,就开始不断地定位用户的位置,中途会频繁地调用代理的下面方法

  - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations;

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

每隔多少米定位一次

  @property(assign, nonatomic) CLLocationDistance distanceFilter;

定位精确度(越精确就越耗电)

  @property(assign, nonatomic) CLLocationAccuracy desiredAccuracy;

使用定位功能,首先要导入框架,遵守CLLocationManagerDelegate协议,再创建位置管理器CLLocationManager

在iOS8.0后,定位功能需要在info.plist中加入NSLocationWhenInUseUsageDescription和NSLocationAlwaysUsageDescription这两个NSString类型字段,才能够使用定位功能

代码贴出来与大家共勉,各位看官自行研究

{  self.locationManager = [[CLLocationManager alloc] init];  _locationManager.delegate = self;  if([CLLocationManager locationServicesEnabled] == NO) {   //  NSLog(@"没有GPS服务");  }  //地理位置精确度  _locationManager.desiredAccuracy=kCLLocationAccuracyNearestTenMeters;  //设置距离筛选器,double类型,只要距离变化多少,就调用委托代理  self.locationManager.distanceFilter = kCLDistanceFilterNone; // meters  [_locationManager requestWhenInUseAuthorization];// 前台定位  [_locationManager startUpdatingLocation];}- (void)locationManager:(CLLocationManager *)manager   didUpdateLocations:(NSArray *)locations{  NSLog(@"longitude = %f", ((CLLocation *)[locations lastObject]).coordinate.longitude);  NSLog(@"latitude = %f", ((CLLocation *)[locations lastObject]).coordinate.latitude);CGFloat longTI=((CLLocation *)[locations  lastObject]).coordinate.longitude;CGFloat latTI=((CLLocation *)[locations lastObject]).coordinate.latitude;//将经度显示到label上_longitudeLabel.text = [NSString stringWithFormat:@"%f",longTI];//将纬度现实到label上_latitudeLabel.text = [NSString stringWithFormat:@"%f",latTI];  // 获取当前所在的城市名  CLGeocoder *geocoder = [[CLGeocoder alloc] init];  //根据经纬度反向地理编译出地址信息  [geocoder reverseGeocodeLocation:locations.lastObject completionHandler:^(NSArray *array, NSError *error)   { if (array.count  0) {   CLPlacemark *placemark = [array objectAtIndex:0];//   //将获得的所有信息显示到label上//   self.location.text = placemark.name;   //获取城市   NSString *city = placemark.locality;   if (!city) { //四大直辖市的城市信息无法通过locality获得,只能通过获取省份的方法来获得(如果city为空,则可知为直辖市) city = placemark.administrativeArea;   }  // NSLog(@"city = %@", city);   _cityName=city; } else if (error == nil && [array count] == 0) {  // NSLog(@"No results were returned."); } else if (error != nil) {  // NSLog(@"An error occurred = %@", error); }   }];  //系统会一直更新数据,直到选择停止更新,因为我们只需要获得一次经纬度即可,所以获取之后就停止更新  [manager stopUpdatingLocation];}

以上是关于脚本之家图老师小编给大家整理的IOS开发之详解定位CLLocation,后续还会持续更新,希望大家能够喜欢。

来源:https://www.tulaoshi.com/n/20160219/1595387.html

延伸阅读
一、简单介绍 1.什么是UIPopoverController 是iPad开发中常见的一种控制器(在iPhone上不允许使用) 跟其他控制器不一样的是,它直接继承自NSObject,并非继承自UIViewController 它只占用部分屏幕空间来呈现信息,而且显示在屏幕的最前面 2.使用步骤 要想显示一个UIPopoverController,需要经过下列步骤 (1)设置内容控制器 ...
从事Android开发,免不了会在应用里嵌入一些广告SDK,在嵌入了众多SDK后,发现几乎每个要求在AndroidManifest.xml申明Activity的广告SDK都会要求加上注明这么一句属性: 代码如下: android:configChanges="orientation|keyboard|keyboardHidden" 通过查阅Android API可以得知android:onConfigurationChanged实际对应的是Activity里...
1.在layout文件下的配置xml文件中直接设置字体颜色,通过添加android:textcolor=“#FFFFFF”来变化颜色 但这样的效果只能让字体千篇一律的显示一种颜色 2.在activity中通过TextView tv=new TextView(this);实例化一个textview,通过setContentView(tv);将其加载到当前activity,设置要显示的内容String str=“想要显示的内容”;通过以下...
一、Keychain 基础 根据苹果的介绍,iOS设备中的Keychain是一个安全的存储容器,可以用来为不同应用保存敏感信息比如用户名,密码,网络密码,认证令牌。苹果自己用keychain来保存Wi-Fi网络密码,VPN凭证等等。它是一个sqlite数据库,位于/private/var/Keychains/keychain-2.db,其保存的所有数据都是加密过的。 开发者通常会希望能够利用...
这里只是简单写一下过程吧。 第一步:安装cocoapods sudo gem install cocoapods 要是不成功,执行以下操作 gem sources --remove https://rubygems.org/ gem sources -a http://ruby.taobao.org/ gem sources -l(用来查看是否成功) sudo gem install cocoapods 第二步:在项目中导入库 切到与工程目录相关的.xcodeproj同一目录下 v...

经验教程

813

收藏

32
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部