获取gps信息
(一)plist修改
添加如下变量
(二)新建视图用来启动Gps
此视图控制器继承CLLocationManagerDelegate
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h> @interface GpsViewController : UIViewController <CLLocationManagerDelegate> @end
(三)定义标签用来显示位置,并开启定位
UILabel *latitudeValue;
UILabel *longitudeValue;
CLLocationManager *locationManager; - (void)viewDidLoad {
[super viewDidLoad]; self.view.backgroundColor=[UIColor redColor];
self.view.alpha=0.5; UILabel *latitude=[[UILabel alloc] init];
latitude.text=@"Latitude:";
latitude.frame=CGRectMake(, , , );
[self.view addSubview:latitude]; latitudeValue=[[UILabel alloc] init];
latitudeValue.text=@"";
latitudeValue.frame=CGRectMake(, , , );
[self.view addSubview:latitudeValue]; UILabel *longitude=[[UILabel alloc] init];
longitude.text=@"Longitude:";
longitude.frame=CGRectMake(, , , );
[self.view addSubview:longitude]; longitudeValue=[[UILabel alloc] init];
longitudeValue.text=@"";
longitudeValue.frame=CGRectMake(, , , );
[self.view addSubview:longitudeValue]; //开启定位
locationManager=[[CLLocationManager alloc] init];
locationManager.delegate=self;
locationManager.desiredAccuracy=kCLLocationAccuracyBest;
locationManager.distanceFilter=kCLDistanceFilterNone;
locationManager.allowsBackgroundLocationUpdates=YES;
//使用期间定位
[locationManager requestWhenInUseAuthorization];
[locationManager requestAlwaysAuthorization];
[locationManager startUpdatingLocation];
// Do any additional setup after loading the view.
}
(四)通过委托说去实时位置
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations
{
NSLog(@"Update location");
CLLocation *newLoaction=locations[];
latitudeValue.text=[[NSString alloc] initWithFormat:@"%f",newLoaction.coordinate.latitude];
longitudeValue.text=[[NSString alloc] initWithFormat:@"%f",newLoaction.coordinate.longitude];
}
(五)用户权限检测
if ([CLLocationManager locationServicesEnabled]) {
switch ([CLLocationManager authorizationStatus]) {
case kCLAuthorizationStatusNotDetermined:
NSLog(@"用户尚未进行选择");
break;
case kCLAuthorizationStatusRestricted:
NSLog(@"定位权限被限制");
break;
case kCLAuthorizationStatusAuthorizedAlways:
case kCLAuthorizationStatusAuthorizedWhenInUse:
NSLog(@"用户允许定位");
break;
case kCLAuthorizationStatusDenied:
NSLog(@"用户不允许定位");
break; default:
break;
}
}
(六)定位失败委托
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(@"Update error"); if(error.code == kCLErrorLocationUnknown) {
NSLog(@"无法检索位置");
}
else if(error.code == kCLErrorNetwork) {
NSLog(@"网络问题");
}
else if(error.code == kCLErrorDenied) {
NSLog(@"定位权限的问题");
[locationManager stopUpdatingLocation];
}
}
//--------------------------------方法封装-----------------------------------//
通过自定义委托来实现获取地址后的数据传递
LLLocation.h
#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h> @protocol LocationHandlerDelegate <NSObject> @required
-(void) didUpdateToLocation:(CLLocation*)newLocation; @end @interface LLLocation : NSObject<CLLocationManagerDelegate>
{
CLLocationManager *locationManager;
} @property(nonatomic,strong) id<LocationHandlerDelegate> delegate; +(id)getSharedInstance;
-(void)startUpdatingLocation;
-(void) stopUpdatingLocation; @end
LLLocation.m
#import "LLLocation.h" @interface LLLocation() -(void)initLocationManager; @end @implementation LLLocation + (LLLocation *)getSharedInstance
{
static LLLocation *location = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
location = [[LLLocation alloc] init];
[location initLocationManager];
});
return location;
} -(void)initLocationManager
{
NSLog(@"init");
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
} -(void)startUpdatingLocation
{
[locationManager startUpdatingLocation];
} -(void) stopUpdatingLocation
{
[locationManager stopUpdatingLocation];
} - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations
{
[locationManager stopUpdatingLocation];
NSLog(@"Update gps");
if ([self.delegate respondsToSelector:@selector
(didUpdateToLocation:)])
{
[self.delegate didUpdateToLocation:locations[]]; }
} @end
调用
LLLocation *location=[LLLocation getSharedInstance];
[location startUpdatingLocation];
[location setDelegate:self];
上述调用文件中要实现自定义的委托,如下
@interface GpsViewController ()<LocationHandlerDelegate> @property(nonatomic,strong) CLLocationManager *locationManager; @end
-(void) didUpdateToLocation:(CLLocation*)newLocation
{
latitudeValue.text=[[NSString alloc] initWithFormat:@"%f",newLocation.coordinate.latitude];
longitudeValue.text=[[NSString alloc] initWithFormat:@"%f",newLocation.coordinate.longitude]; }
PS:在应用过程中会出现委托并未调用情况,如果按上述步骤进行,委托还未调用,则建议查看CLLocationManagerDelegate的定义,确定委托方法是否在此ios版本中适用;或者把CLLocationManager *locationManager定义为强引用的属性试试
获取gps信息的更多相关文章
- Android开发程序获取GPS信息步骤
1.获取LOCATION_SERVICE系统服务.2.创建Criteria对象,调用该对象的set方法设置查询条件.3.调用LocationManager.getBestProvider(Criter ...
- 文件读写以及NMEA码中GPS信息的提取
首先先了解下什么是NMEA码,这里有很好的解释,就不直接搬运了 http://www.gpsbaby.com/wz/nmea.html 首先要找到包含GPS信息的文本行,即字符串GPGGA所在行 $G ...
- 使用SerialPort 读取外置GPS信息和使用GeoCoordinateWatcher获取内置gps的信息
简介最近工作中需要读取gps设备的信息,平板本身有内置的gps设备,但是精度不够,就又添加了一个外置的gps.对于外置的gps,我们主要通过SerialPort类来获得串口的信息,然后对接收到的内容进 ...
- Android GPS应用:动态获取位置信息
在上文中,介绍了GPS概念及Android开发GPS应用涉及到的常用类和方法.在本文中,开发一个小应用,实时获取定位信息,包括用户所在的纬度.经度.高度.方向.移动速度等.代码如下: Activity ...
- Android 使用GPS获取到经纬度后 无法在Android8.0上使用Geocoder类获取位置信息
由于我的应用在获取到经纬度后在Android8.0不能使用如下代码获取位置信息.只好使用百度地图 WEB服务API 通过调接口的方式获取位置信息. Geocoder geocoder = new Ge ...
- Android获取位置信息的方法总结
1.位置服务的简介:位置服务,英文翻译为Location-Based Services,缩写为LBS,又称为定位服务或基于位置的服务,融合了GPS定位.移动通信.导航等多种技术,提供与空间位置相关的综 ...
- Html5 Geolocation获取地理位置信息
Html5中提供了地理位置信息的API,通过浏览器来获取用户当前位置.基于此特性可以开发基于位置的服务应用.在获取地理位置信息前,首先浏览器都会向用户询问是否愿意共享其位置信息,待用户同意后才能使用. ...
- 使用高德地图SDK获取定位信息
使用高德地图SDK获取定位信息 第一步: 在高德官网下载SDK,如我这里需要获取定位信息,故仅下载"定位功能" 第二步: 注册成为开发者,并创建应用获取相应的key.其中,在使用A ...
- GPS(2)关于位置的3个示例,实时获取GPS定位数据,求两个经纬点距离,邻近某个区域圆时警告
实时获取GPS定位数据 import android.app.Activity; import android.content.Context; import android.location.Loc ...
随机推荐
- mybatis-generator生成数据对象
mybatis-generator生成数据对象 步骤一:在pom文件中添加build的插件 <build> <finalName>doudou</finalName> ...
- 9 个基于JavaScript 和 CSS 的 Web 图表框架
COMSHARP CMS 写道:jQuery, MooTools, Prototype 等优秀的 JavaScript 框架拥有各种强大的功能,包括绘制 Web图表,使用这些框架以及相应插件,我们可以 ...
- Linux 安装CentOS7操作系统
作者:小啊博 QQ:762641008 转载请声明:https://www.cnblogs.com/-bobo 1.安装操作系统 开启虚拟机后会出现以下界面 Install CentOS 7 安装Ce ...
- 性能测试的基础知识--QPS和TPS
基本概念: QPS:Queries Per Second意思是“每秒查询率” ,是一台服务器每秒能够相应的查询次数,是对一个特定的查询服务器在规定时间内所处理流量多少的衡量标准. TPS:Transa ...
- Django之使用haystack+whoosh实现搜索功能
为了实现项目中的搜索功能,我们使用的是全文检索框架haystack+搜索引擎whoosh+中文分词包jieba 安装和配置 安装所需包 pip install django-haystack pip ...
- Java的一些基础知识深入
1.浅析Java中的final关键字: 2.Java中的static关键字解析: 3.探秘Java中String.StringBuilder以及StringBuffer: 4.Java内部类详解: 5 ...
- 记一次客户DB CPU短时间内冲高至99%处理
问题背景: 客户反映DB CPU短时间内冲高,查询变得缓慢,记录此背景下的处理方式 1> 查看系统负载及相关sql 2> 查看相关sql的执行计划 3> 想看相关sql的执行计划是否 ...
- ORM查询简化
文章出处 https://www.cnblogs.com/wupeiqi/articles/6216618.html 字段 常用字段 AutoField(Field) - int自增列,必须填入参数 ...
- Faker——生成测试数据的PHP类库
工作上用的是TP框架,每次测试功能的时候都要手动添加测试数据,词穷起名总是起一些test1.test2这种low到爆炸的用户名,这让我很难受.稍微翻阅了一些资料,发现laravel有一个生成测试数据的 ...
- GLSL 参考GIMP源码实现色彩平衡调节
色彩平衡 修图工具中的色彩平衡一般用来根据亮度等级调整图片中颜色的偏色,调整偏色涉及到加色原理和减色原理 其实我们通过三原色加色原理的图片就可以知道,红色的对比色是青色,蓝色的对比色是黄色,绿色的对比 ...