(一)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. 使用Java元注解和反射实现简单MVC框架

    Springmvc的核心是DispatcherServlet来进行各种请求的拦截,进而进行后续的各种转发处理.流程图如下: 说明:客户端发出一个http请求给web服务器,web服务器对http请求进 ...

  2. 玩转 SpringBoot 2 之整合 JWT 上篇

    前言 该文主要带你了解什么是 JWT,以及JWT 定义和先关概念的介绍,并通过简单Demo 带你了解如何使用 SpringBoot 2 整合 JWT. 介绍前在这里我们来探讨一下如何学习一门新的技术, ...

  3. linux脚本入门之终端显示输出

    主要基本命令为 echo 与 printf. 关于echo: 其语法结构为:echo -选项参数 字符串: 例如:echo hello,world   echo 'hello,world'  echo ...

  4. win7远程连接全屏和窗口模式切换

    最近开发需要win7远程连接,我知道在连接的时候可以设置全屏模式 但是进去之后想要切换就只能通过快捷键了上网查了一下是ctrl+alt+break.网上说的没有错.我查官方文档也是这样.但是我按的时候 ...

  5. C++——指针

    目录 一.地址和指针 1.1内存 1.2针和指针变量 二.指针变量 2.1始化 2.2赋值 2.3指针类型算术运算 2.4指针类型关系运算 2.5指向指针的指针 三.指针与数组 3.1指针运算 3.2 ...

  6. Nullable Reference Types 可空引用类型

    在写C#代码的时候,你可能经常会遇到这个错误: 但如果想避免NullReferenceException的发生,确实需要做很多麻烦的工作. 可空引用类型 Null Reference Type 所以, ...

  7. Rust入坑指南:常规套路

    搭建好了开发环境之后,就算是正式跳进Rust的坑了,今天我就要开始继续向下挖了. 由于我们初来乍到 ,对Rust还不熟悉,所以我决定先走一遍常规套路. 变不变的变量 学习一门语言第一个要了解的当然就是 ...

  8. Python 爬虫从入门到进阶之路(十)

    之前的文章我们介绍了一下 Python 中正则表达式和 re 模块来做一个案例,爬取<糗事百科>的糗事并存储到本地.本章我们来看一下另一种爬取数据的方式 XPath. 我们在前面爬取< ...

  9. redis之spring应用

    前言 读本文之前,需要大家对redis有所了解,大家可以去看一下我之前分享的redis安装及简单使用这一随笔,而本文我将跟大家分享学习一下redis与spring的集成.当然,首先需要打开我们的red ...

  10. 【JZOJ5329】-时间机器

    [JZOJ5264]化学 Description Input Output Sample Input 3 10 1 2 10 Sample Output 5 Hint 题解: 这个题目又是一道贪心题, ...