IOS Core Location 定位功能

2024-06-01 15:48
文章标签 功能 core 定位 ios location

本文主要是介绍IOS Core Location 定位功能,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Core Location可以利用三种技术来实现该功能:

  1. 蜂窝基站三角网定位根据手机所属范围内的手机基站的位置进行计算来确定当前位置。蜂窝基站三角网定位在城市和其他手机基站密度较高的区域非常准确,而在基站较为稀疏的区域则不太精确。
  2. GPS是三种技术中最为精确的,任何具有3G数据链接的设备还包含一个GPS单元,GPS读取来自多个卫星的微波信号来确定当前位置。
  3. Wi-Fi定位服务(WPS)使用Wi-Fi连接的MAC地址,通过参考已知服务提供商及其服务区域的大型数据库来猜测你的位置。WPS是不精确的,并且有时会有数英里的误差。
根据程序的需要,挑选合适的方法。

  • 位置管理器
Core Location API实际上非常易于使用。我们将使用的主类是CLLocationManager,通常称为位置管理器。
 创建一个位置管理器实例
CLLocationManager *locationManager = [[CLLocationManager alloc] init];

要求的Core Location的精度越高,消耗的电量就会越多。
  • 设置所需的精度
下面设置委托和请求指定精度级别的实例:
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.desiredAccuracy该值的单位为米,如果你指定它为10,
则希望尝试确定当前位置10米范围之内的区域。
  • 设置距离筛选器
默认情况下,位置管理器将通知委托任何检测到的在位置方面的更改。
距离筛选器也是以米为单位进行设置的。
locationManager.distanceFilter = 1000.0f;
  • 启动位置管理器
[locationManager startUpdatingLocation];
  • 更明智地使用位置管理器
如果只需要确定当前位置而不需要连续轮询位置,则当它获取应用程序所需的信息之后,
你应该让位置委托停止位置管理器。
只要你从位置管理器获得更新,就会消耗拥用户的电池。
位置管理器停止向其委托发送更新:
[locationManager stopUpdatingLocation];
  • 位置管理器委托
位置管理器委托必须符合CLLocationManagerDelegate协议,该协议定义了两种方法, 
当位置管理器已经确定位置或者 当它检测到位置的更改时将调用其中一个方法,
当位置管理器遇到错误时将调用另一个方法。
  • 获取位置更新
当位置管理器希望通知其委托当前位置时,它将调用locationManager:didUpdateToLocation:fromLocation:方法。
该方法接受3个参数。
第一个参数是调用该方法的位置管理器。
第二个参数是定义设备的当前位置的一个CLLocation对象。
第三个参数是上次更新定义之前的位置的一个CLLocation对象。第一次调用该方法时,以前的位置对象将为nil;
  • 使用CLLocation获取纬度和经度
纬度和经度存储在一个名为coordinate的属性中。
CLLocationDegrees latitude = theLocation.coordinate.latitude;
CLLocationDegrees longitude = theLocation.coordinate.longitude;

horizontalAccuracy属性描述以coordinate作为其圆的半径,值越大,Core Location所确定的位置就越不确定。

altitude属性是海平面以上或以下多少米:
CLLocationDegreesaltitude = theLocation.altitude;

除了这些属性之外,CLLocation还有一个非常有用的实例方法,该方法将允许你确定两个CLLocation对象之间的距离。
CLLocationDegrees distance = [fromLocation distanceFromLocation : toLocation];
返回的distance值将是大圆计算的结果,该计算忽略了海波属性,并且假设这两个点处于同一海平面来计算该距离。
  • 错误通知
如果Core Location无法确定你的当前位置,它会调用另一个名为locationManager:didFailwithError:的委托方法。
最有可能的错误原因是用户拒绝访问。 位置管理器的使用必须由用户进行授权。


在xcode工程Frameworks文件夹种添加CoreLocation.framework


上代码。。。

先在BIDViewController_iPhone.xib中创建12个label

BIDViewController.h

//
//  BIDViewController.h
//  CoreLocationTest
//
//  Created by ex next on 13-8-8.
//  Copyright (c) 2013年 ex next. All rights reserved.
//#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>@interface BIDViewController : UIViewController<CLLocationManagerDelegate>@property (strong, nonatomic) CLLocationManager *locationManager;
@property (strong, nonatomic) CLLocation *startingPoiont;
@property (strong, nonatomic) IBOutlet UILabel *latitudeLabel;
@property (strong, nonatomic) IBOutlet UILabel *longitudeLabel;
@property (strong, nonatomic) IBOutlet UILabel *horizontalAccuracyLabel;
@property (strong, nonatomic) IBOutlet UILabel *altitudeLabel;
@property (strong, nonatomic) IBOutlet UILabel *verticalAccuracyLabel;
@property (strong, nonatomic) IBOutlet UILabel *distanceTraveledLabel;@end

BIDViewController.m

//
//  BIDViewController.m
//  CoreLocationTest
//
//  Created by ex next on 13-8-8.
//  Copyright (c) 2013年 ex next. All rights reserved.
//#import "BIDViewController.h"@interface BIDViewController ()@end@implementation BIDViewController
@synthesize locationManager;
@synthesize startingPoiont;
@synthesize latitudeLabel;
@synthesize longitudeLabel;
@synthesize horizontalAccuracyLabel;
@synthesize altitudeLabel;
@synthesize verticalAccuracyLabel;
@synthesize distanceTraveledLabel;- (void)viewDidLoad
{[super viewDidLoad];// Do any additional setup after loading the view, typically from a nib.//配置位置管理器self.locationManager = [[CLLocationManager alloc] init];locationManager.delegate = self;locationManager.desiredAccuracy = kCLLocationAccuracyBest;[locationManager startUpdatingLocation];
}- (void)viewDidUnload
{//清除输出口[self viewDidUnload];self.locationManager = nil;self.latitudeLabel = nil;self.longitudeLabel = nil;self.horizontalAccuracyLabel = nil;self.altitudeLabel = nil;self.verticalAccuracyLabel = nil;self.distanceTraveledLabel = nil;
}- (void)didReceiveMemoryWarning
{[super didReceiveMemoryWarning];// Dispose of any resources that can be recreated.
}#pragma mark -
#pragma mark CLLocationManagerDelegate Methods
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{if (startingPoiont == nil) {self.startingPoiont = newLocation;}NSString *latitudeString = [NSString stringWithFormat:@"%g\u00B0",newLocation.coordinate.latitude];latitudeLabel.text = latitudeString;NSString *longitudeString = [NSString stringWithFormat:@"%g\u00B0",newLocation.coordinate.longitude];longitudeLabel.text = longitudeString;NSString *horizontalAccuracyString = [NSString stringWithFormat:@"%gm",newLocation.horizontalAccuracy];horizontalAccuracyLabel.text = horizontalAccuracyString;NSString *altitudeString = [NSString stringWithFormat:@"%gm",newLocation.altitude];altitudeLabel.text = altitudeString;NSString *verticalAccuracyString = [NSString stringWithFormat:@"%gm",newLocation.verticalAccuracy];verticalAccuracyLabel.text = verticalAccuracyString;CLLocationDistance distance = [newLocation distanceFromLocation:startingPoiont];NSString *distanceString = [NSString stringWithFormat:@"%gm",distance];distanceTraveledLabel.text = distanceString;
}- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{NSString *errorType = (error.code == kCLErrorDenied)?@"Access Denied" : @"Unknown Error";UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error getting Locatin"message:errorTypedelegate:nilcancelButtonTitle:@"Okay" otherButtonTitles: nil];[alert show];
}@end



这篇关于IOS Core Location 定位功能的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1021435

相关文章

Qt使用QSqlDatabase连接MySQL实现增删改查功能

《Qt使用QSqlDatabase连接MySQL实现增删改查功能》这篇文章主要为大家详细介绍了Qt如何使用QSqlDatabase连接MySQL实现增删改查功能,文中的示例代码讲解详细,感兴趣的小伙伴... 目录一、创建数据表二、连接mysql数据库三、封装成一个完整的轻量级 ORM 风格类3.1 表结构

怎样通过分析GC日志来定位Java进程的内存问题

《怎样通过分析GC日志来定位Java进程的内存问题》:本文主要介绍怎样通过分析GC日志来定位Java进程的内存问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、GC 日志基础配置1. 启用详细 GC 日志2. 不同收集器的日志格式二、关键指标与分析维度1.

Java进程异常故障定位及排查过程

《Java进程异常故障定位及排查过程》:本文主要介绍Java进程异常故障定位及排查过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、故障发现与初步判断1. 监控系统告警2. 日志初步分析二、核心排查工具与步骤1. 进程状态检查2. CPU 飙升问题3. 内存

mysql表操作与查询功能详解

《mysql表操作与查询功能详解》本文系统讲解MySQL表操作与查询,涵盖创建、修改、复制表语法,基本查询结构及WHERE、GROUPBY等子句,本文结合实例代码给大家介绍的非常详细,感兴趣的朋友跟随... 目录01.表的操作1.1表操作概览1.2创建表1.3修改表1.4复制表02.基本查询操作2.1 SE

Golang如何用gorm实现分页的功能

《Golang如何用gorm实现分页的功能》:本文主要介绍Golang如何用gorm实现分页的功能方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录背景go库下载初始化数据【1】建表【2】插入数据【3】查看数据4、代码示例【1】gorm结构体定义【2】分页结构体

Java Web实现类似Excel表格锁定功能实战教程

《JavaWeb实现类似Excel表格锁定功能实战教程》本文将详细介绍通过创建特定div元素并利用CSS布局和JavaScript事件监听来实现类似Excel的锁定行和列效果的方法,感兴趣的朋友跟随... 目录1. 模拟Excel表格锁定功能2. 创建3个div元素实现表格锁定2.1 div元素布局设计2.

HTML5实现的移动端购物车自动结算功能示例代码

《HTML5实现的移动端购物车自动结算功能示例代码》本文介绍HTML5实现移动端购物车自动结算,通过WebStorage、事件监听、DOM操作等技术,确保实时更新与数据同步,优化性能及无障碍性,提升用... 目录1. 移动端购物车自动结算概述2. 数据存储与状态保存机制2.1 浏览器端的数据存储方式2.1.

基于 HTML5 Canvas 实现图片旋转与下载功能(完整代码展示)

《基于HTML5Canvas实现图片旋转与下载功能(完整代码展示)》本文将深入剖析一段基于HTML5Canvas的代码,该代码实现了图片的旋转(90度和180度)以及旋转后图片的下载... 目录一、引言二、html 结构分析三、css 样式分析四、JavaScript 功能实现一、引言在 Web 开发中,

springboot下载接口限速功能实现

《springboot下载接口限速功能实现》通过Redis统计并发数动态调整每个用户带宽,核心逻辑为每秒读取并发送限定数据量,防止单用户占用过多资源,确保整体下载均衡且高效,本文给大家介绍spring... 目录 一、整体目标 二、涉及的主要类/方法✅ 三、核心流程图解(简化) 四、关键代码详解1️⃣ 设置

CSS Anchor Positioning重新定义锚点定位的时代来临(最新推荐)

《CSSAnchorPositioning重新定义锚点定位的时代来临(最新推荐)》CSSAnchorPositioning是一项仍在草案中的新特性,由Chrome125开始提供原生支持需... 目录 css Anchor Positioning:重新定义「锚定定位」的时代来了! 什么是 Anchor Pos