IOS Augmented Reality增强现实学习笔记 -基于位置

2024-04-23 05:38

本文主要是介绍IOS Augmented Reality增强现实学习笔记 -基于位置,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

地图编程和定位是增强现实技术的基础,因此首先学习Location Services
1标准定位(standard location service)
 有两种方式可以监控我们的位置,首先是标准定位。这是一种很普遍的方式,因为所有的ios设备都支持标准定位。首先我们要导入Core Location Framework到我们的工程中,然后把Core Location Framework加入到头文件中,如下所示:


#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
@interface FirstViewController : UIViewController <CLLocationManagerDelegate> {UITextView *locationTextView;
}
@property (nonatomic, retain) IBOutlet UITextView *locationTextView;
@end 

CLLocationManagerDelegate 会将定位信息传给locationManager:didUpdateToLocation:fromLocation: 方法,如果发生错误则会传给
locationManager:didFailWithError:  方法,将这些方法的实现加入到.m文件中


- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation*)newLocation
fromLocation:(CLLocation *)oldLocation{


       //Apple建议检查新位置的时间戳,因为有时调用代理方法会有延迟
NSDate* eventDate = newLocation.timestamp;
NSTimeInterval howRecent = [eventDate timeIntervalSinceNow];if (abs(howRecent) < 15.0)



locationTextView.text = [NSString stringWithFormat:@"latitude %+.6f, longitude%+.6f\n", 
newLocation.coordinate.latitude,newLocation.coordinate.longitude]; 
       }else{


locationTextView.text = @"Update was old";


      }
}



ok,现在让我们实现启动服务的方法


- (void)startStandardUpdates{
CLLocationManager *locationManager = [[CLLocationManager alloc] init];locationManager.delegate = self; 
locationManager.desiredAccuracy = kCLLocationAccuracyKilometer;
locationManager.distanceFilter = 500;
[locationManager startUpdatingLocation];  

最后我们在viewDidLoad方法中调用 startStandUpdates方法


- (void)viewDidLoad{
[self startStandardUpdates];
[super viewDidLoad];

这样运行就可以获取到latitude和longitude信息了
1显著位置变化定位(significant-change location service)
 从iOS 4.0之后新加入了significant-change location service服务,它更省电,但是精确度会差一些。
它使用cellular radio代替GPS radio来定位.
使用的方式和标准定位很相似,它也会调用delegate的locationManager:didUpdateToLocation:fromLocation: 方法来传递位置信息。下面是启动significant-change location service的方法:
- (void)startSignificantChangeUpdates {
CLLocationManager *locationManager = [[CLLocationManager alloc] init]; 
locationManager.delegate = self;
[locationManager startMonitoringSignificantLocationChanges];
}

然后在viewDidLoad方法中加入调用:
- (void)viewDidLoad{
//[self startStandardUpdates];
[self startSignificantChangeUpdates];
[super viewDidLoad];

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
两种位置定位总结完后,下面学习地理位置监控服务(Geographic Region Monitoring Service),即当我们进入某一个区域后,系统会识别到,并调用didEnterRegion和didExitRegion方法来处理事件。直接上代码:
- (void)startRegionMonitoring {
NSLog(@"Starting region monitoring");
CLLocationManager *locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
CLLocationCoordinate2D coord = CLLocationCoordinate2DMake(37.787359, -122.408227);
CLRegion *region = [[CLRegion alloc] initCircularRegionWithCenter:coord radius:1000.0 identifier:@"San Francisco"];
[locationManager startMonitoringForRegion:region desiredAccuracy:kCLLocationAccuracyKilometer];


}
此方法设置(37.787359, -122.408227)为中心,半径1000米的范围为检测区域.之后我们就可以实现didEnterRegion和dieExitRegion方法完成对于进入区域和离开区域的事件的处理了:
- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region { 
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"区域检测"
message:@"你进入此区域" 
delegate:self
cancelButtonTitle:@"OK" 
otherButtonTitles:nil, nil];
[alert show];
[alert release];
 }

- (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region { 
      UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Region Alert"
message:@"你离开此区域" 
delegate:self
cancelButtonTitle:@"OK" 
otherButtonTitles:nil, nil];
[alert show];
[alert release];
 }

最后一步,即在viewDidLoad中调用startRegionMonitoring方法
- (void)viewDidLoad {
[self startStandardUpdates];
//[self startSignificantChangeUpdates];
if ([CLLocationManager regionMonitoringAvailable]) { // 检查服务是否可用
[self startRegionMonitoring];
} // else do something
[super viewDidLoad]; 
}

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
本帖最后讲述如何计算改变的距离。此方法只需要将前面standard location service中的方法做稍许改变即可
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
 NSDate* eventDate = newLocation.timestamp;
NSTimeInterval howRecent = [eventDate timeIntervalSinceNow]; 
if (abs(howRecent) < 15.0){
      //locationTextView.text = [NSString stringWithFormat:@"latitude %+.6f, longitude %+.6f\n",newLocation.coordinate.latitude,  newLocation.coordinate.longitude]; 
     CLLocationDistance dist=[newLocation distanceFromLocation:oldLocation]/1000;
     locationTextView.text=[NSString stringWithFormat:@"distance %5.1f traveled"];
         }else{
    locationTextView.text = @"Update was old";
        }
}

这样即可获取到位置的改变值了。










这篇关于IOS Augmented Reality增强现实学习笔记 -基于位置的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

Go学习记录之runtime包深入解析

《Go学习记录之runtime包深入解析》Go语言runtime包管理运行时环境,涵盖goroutine调度、内存分配、垃圾回收、类型信息等核心功能,:本文主要介绍Go学习记录之runtime包的... 目录前言:一、runtime包内容学习1、作用:① Goroutine和并发控制:② 垃圾回收:③ 栈和

Android学习总结之Java和kotlin区别超详细分析

《Android学习总结之Java和kotlin区别超详细分析》Java和Kotlin都是用于Android开发的编程语言,它们各自具有独特的特点和优势,:本文主要介绍Android学习总结之Ja... 目录一、空安全机制真题 1:Kotlin 如何解决 Java 的 NullPointerExceptio

如何更改pycharm缓存路径和虚拟内存分页文件位置(c盘爆红)

《如何更改pycharm缓存路径和虚拟内存分页文件位置(c盘爆红)》:本文主要介绍如何更改pycharm缓存路径和虚拟内存分页文件位置(c盘爆红)问题,具有很好的参考价值,希望对大家有所帮助,如有... 目录先在你打算存放的地方建四个文件夹更改这四个路径就可以修改默认虚拟内存分页js文件的位置接下来从高级-

PyCharm如何更改缓存位置

《PyCharm如何更改缓存位置》:本文主要介绍PyCharm如何更改缓存位置的实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录PyCharm更改缓存位置1.打开PyCharm的安装编程目录2.将config、sjsystem、plugins和log的路径

重新对Java的类加载器的学习方式

《重新对Java的类加载器的学习方式》:本文主要介绍重新对Java的类加载器的学习方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、介绍1.1、简介1.2、符号引用和直接引用1、符号引用2、直接引用3、符号转直接的过程2、加载流程3、类加载的分类3.1、显示

Android与iOS设备MAC地址生成原理及Java实现详解

《Android与iOS设备MAC地址生成原理及Java实现详解》在无线网络通信中,MAC(MediaAccessControl)地址是设备的唯一网络标识符,本文主要介绍了Android与iOS设备M... 目录引言1. MAC地址基础1.1 MAC地址的组成1.2 MAC地址的分类2. android与I

Java学习手册之Filter和Listener使用方法

《Java学习手册之Filter和Listener使用方法》:本文主要介绍Java学习手册之Filter和Listener使用方法的相关资料,Filter是一种拦截器,可以在请求到达Servl... 目录一、Filter(过滤器)1. Filter 的工作原理2. Filter 的配置与使用二、Listen

利用Python快速搭建Markdown笔记发布系统

《利用Python快速搭建Markdown笔记发布系统》这篇文章主要为大家详细介绍了使用Python生态的成熟工具,在30分钟内搭建一个支持Markdown渲染、分类标签、全文搜索的私有化知识发布系统... 目录引言:为什么要自建知识博客一、技术选型:极简主义开发栈二、系统架构设计三、核心代码实现(分步解析

Java进阶学习之如何开启远程调式

《Java进阶学习之如何开启远程调式》Java开发中的远程调试是一项至关重要的技能,特别是在处理生产环境的问题或者协作开发时,:本文主要介绍Java进阶学习之如何开启远程调式的相关资料,需要的朋友... 目录概述Java远程调试的开启与底层原理开启Java远程调试底层原理JVM参数总结&nbsMbKKXJx

Java深度学习库DJL实现Python的NumPy方式

《Java深度学习库DJL实现Python的NumPy方式》本文介绍了DJL库的背景和基本功能,包括NDArray的创建、数学运算、数据获取和设置等,同时,还展示了如何使用NDArray进行数据预处理... 目录1 NDArray 的背景介绍1.1 架构2 JavaDJL使用2.1 安装DJL2.2 基本操