(一)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信息的更多相关文章

  1. Android开发程序获取GPS信息步骤

    1.获取LOCATION_SERVICE系统服务.2.创建Criteria对象,调用该对象的set方法设置查询条件.3.调用LocationManager.getBestProvider(Criter ...

  2. 文件读写以及NMEA码中GPS信息的提取

    首先先了解下什么是NMEA码,这里有很好的解释,就不直接搬运了 http://www.gpsbaby.com/wz/nmea.html 首先要找到包含GPS信息的文本行,即字符串GPGGA所在行 $G ...

  3. 使用SerialPort 读取外置GPS信息和使用GeoCoordinateWatcher获取内置gps的信息

    简介最近工作中需要读取gps设备的信息,平板本身有内置的gps设备,但是精度不够,就又添加了一个外置的gps.对于外置的gps,我们主要通过SerialPort类来获得串口的信息,然后对接收到的内容进 ...

  4. Android GPS应用:动态获取位置信息

    在上文中,介绍了GPS概念及Android开发GPS应用涉及到的常用类和方法.在本文中,开发一个小应用,实时获取定位信息,包括用户所在的纬度.经度.高度.方向.移动速度等.代码如下: Activity ...

  5. Android 使用GPS获取到经纬度后 无法在Android8.0上使用Geocoder类获取位置信息

    由于我的应用在获取到经纬度后在Android8.0不能使用如下代码获取位置信息.只好使用百度地图 WEB服务API 通过调接口的方式获取位置信息. Geocoder geocoder = new Ge ...

  6. Android获取位置信息的方法总结

    1.位置服务的简介:位置服务,英文翻译为Location-Based Services,缩写为LBS,又称为定位服务或基于位置的服务,融合了GPS定位.移动通信.导航等多种技术,提供与空间位置相关的综 ...

  7. Html5 Geolocation获取地理位置信息

    Html5中提供了地理位置信息的API,通过浏览器来获取用户当前位置.基于此特性可以开发基于位置的服务应用.在获取地理位置信息前,首先浏览器都会向用户询问是否愿意共享其位置信息,待用户同意后才能使用. ...

  8. 使用高德地图SDK获取定位信息

    使用高德地图SDK获取定位信息 第一步: 在高德官网下载SDK,如我这里需要获取定位信息,故仅下载"定位功能" 第二步: 注册成为开发者,并创建应用获取相应的key.其中,在使用A ...

  9. GPS(2)关于位置的3个示例,实时获取GPS定位数据,求两个经纬点距离,邻近某个区域圆时警告

    实时获取GPS定位数据 import android.app.Activity; import android.content.Context; import android.location.Loc ...

随机推荐

  1. abp(net core)+easyui+efcore实现仓储管理系统——EasyUI之货物管理二 (二十)

    abp(net core)+easyui+efcore实现仓储管理系统目录 abp(net core)+easyui+efcore实现仓储管理系统——ABP总体介绍(一) abp(net core)+ ...

  2. Java8新特性——接口默认方法

    Java 8 新增了接口的默认方法. 简单说,默认方法就是接口可以有实现方法,而且不需要实现类去实现其方法. 我们只需在方法名前面加个default关键字即可实现默认方法. 为什么要有这个特性? 首先 ...

  3. python import cv2异常(dll load fail / windows server 2008)

    最近服务器迁移,从得win7系统迁移到云服务器器上的windows server2008系统,迁移过程中安装Python的opencv一直出错,不管是用whl安装或者是在线pip安装都报错,尝试打包成 ...

  4. docker的使用 一容器命令

    Docker容器命令 前提 执行容器的前提是有镜像 . #创建并启动容器 docker run [options] images [command][args] // option 的说明 --nam ...

  5. 【C语言笔记】#define与typedef的区别

    1.#define define是预处理指令,在编译时不进行任何检查,只进行简单的替换 宏定义的一般形式为: #define 宏名 字符串 这里所说的字符串是一般意义上的字符序列,不要和C语言中的字符 ...

  6. 白话系列之实现自己简单的mvc式webapi框架

    前言:此文为极简mvc式的api框架,只当做入门api的解析方式,并且这里也不算是mvc框架,因为没有view层,毕竟现在大部分都属于前后端分离,当然也可以提供view层,因为只是将view当做文本返 ...

  7. oracle 分区表(子分区)收缩笔记

    思路1.首先移动子分区到别的表空间.2.收缩数据文件.3.再把子分区移回原表空间. ---------------------------------------------生成发送报告移动子分区语句 ...

  8. SQLMAP SSI注入错误解决

    记一次SQL注入 目标地址:https://www.xxxx.com/ 之前补天提交过这个注入  后来貌似”修复了”(实际就是装了安全狗和过滤了一些关键字) 不过今天试了下 还是可以注入 可以看到已经 ...

  9. [LUOGU2964] [USACO09NOV]硬币的游戏A Coin Game

    题目描述 Farmer John's cows like to play coin games so FJ has invented with a new two-player coin game c ...

  10. Python开发【第八篇】元组

    元组 元组是不可改变的序列,元组是可以存储任意类型数据的容器 元组和字符串的共同点:它们都是容器,都是不可变的序列 元组和字符串的不同点:元组可以存储任意的数据类型的元素,字符串只能存储字符 元组和列 ...