(一)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. Spring boot 官网学习笔记 - 开发第一个Spring boot web应用程序(使用mvn执行、使用jar执行)

    Creating the POM <?xml version="1.0" encoding="UTF-8"?> <project xmlns= ...

  2. javascript:history.go(-1)的使用

    1.问题描述 在微信项目开发中,比如常用联系人的增删改查操作中,比如跳入常用联系人管理页面,选中一个联系人修改它,就会跳入修改页面,修改完成后跳转到常用联系人管理页面,此时如果修改成功跳转采用的是页面 ...

  3. css3练习

    读条的实现1 .div{position: relative;border: 1px solid #111;width: 80px;height: 60px} .div div{width: 20px ...

  4. 使用 .gitignore 忽略 Git 仓库中的文件

    .gitignore 在Git中,很多时候你只想将代码提交到仓库,而不是将当前文件目录下的文件全部提交到Git仓库中,例如在MacOS系统下面的.DS_Store文件,或者是Xocde的操作记录,又或 ...

  5. Angular 路由守卫

    1. 路由 Angular路由: 可以控制页面跳转:可以在多视图间切换: 2. 路由守卫 Angular路由守卫: 在进入或离开某路由时,用于判断是否可以离开.进入某路由::: return true ...

  6. Spring5源码解析3-refresh方法初探

    接上回分析完register(annotatedClasses);后,现在来看一下refresh();方法. // new AnnotationConfigApplicationContext(App ...

  7. Redis info 说明

    背景 前面几篇文章介绍完了Redis相关的一些说明,现在看看如何查看Redis的一些性能指标和统计信息,也可以看官网说明. INFO [section] INFO命令返回有关服务器的信息和统计信息,带 ...

  8. Vue核心知识——computed和watch的细节全面分析

    computed和watch的区别 computed特性 1.是计算值,2.应用:就是简化tempalte里面{{}}计算和处理props或$emit的传值,computed(数据联动).3.具有缓存 ...

  9. Ned 的难题

    题目描述 Ned 再也看不下去 Robert 的种种恶习, 于是他决定出一道题来让他醒悟. Ned 的题目是这样: 给出一个有 n 个数的序列, 求其中所有连续子序列的数的最大公因数的乘积模 1000 ...

  10. 车载导航应用中基于Sketch UI主题定制方案的实现

    1.导读 关于应用的主题定制,相信大家或多或少都有接触,基本上,实现思路可以分为两类: 内置主题(应用内自定义style) 外部加载方式(资源apk形式.压缩资源.插件等) 其实,针对不同的主题定制实 ...