您的位置:首页 > 移动开发 > IOS开发

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

2013-06-23 16:25 274 查看
地图编程和定位是增强现实技术的基础,因此首先学习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";

}

}

这样即可获取到位置的改变值了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息