您的位置:首页 > 其它

IPHONE实景导航开发总结

2013-05-13 12:08 302 查看
http://my.oschina.net/makeffort/blog/89016

1. 在摄像头捕获的画面上添加一个自定义的view。

使用iphoneSDK 3.1的新API:UIImagePickerController的新属性cameraOverView来添加一个自定义的view。

C代码

- (IBAction)getCameraPicture: (id)sender {
UIImagePickerController* picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
//picker.allowsEditing = YES;
picker.showsCameraControls = NO;//关闭默认的摄像设备
//picker.toolbarHidden = YES;

//设定图像缩放比例
picker.cameraViewTransform = CGAffineTransformScale(picker.cameraViewTransform, 1.0, 1.0);

//添加自定义信息层
self.overView = [[OverlayViewConroller alloc] initWithNibName:@"OverlayViewConroller" bundle:nil WithCameraPicker:picker];

overView.view.backgroundColor = [UIColor clearColor];//设定透明背景色
picker.cameraOverlayView = overView.view;

//打开摄像画面作为背景
[self presentModalViewController:picker animated:YES];

[picker release];
}

- (IBAction)getCameraPicture: (id)sender {
UIImagePickerController* picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
//picker.allowsEditing = YES;
picker.showsCameraControls = NO;//关闭默认的摄像设备
//picker.toolbarHidden = YES;

//设定图像缩放比例
picker.cameraViewTransform = CGAffineTransformScale(picker.cameraViewTransform, 1.0, 1.0);

//添加自定义信息层
self.overView = [[OverlayViewConroller alloc] initWithNibName:@"OverlayViewConroller" bundle:nil WithCameraPicker:picker];
overView.view.backgroundColor = [UIColor clearColor];//设定透明背景色
picker.cameraOverlayView = overView.view;

//打开摄像画面作为背景
[self presentModalViewController:picker animated:YES];

[picker release];
}


2. 在自定义的view上添加标志点图标。

一种方法是在view的- (void)drawRect:(CGRect)rect;方法里面添加图像的绘制。

另一种方法是新建一个按钮view,设定背景图片,利用addSubView的方法添加到当前view中,本应用中采用此方法。

3. 对动态添加的按钮绑定UIControlEventTouchUpInside事件关联

可以利用UIButton的方法 addTarget:self action:@selector(tagClick:) forControlEvents:UIControlEventTouchUpInside来绑定需要的事件。

C代码

- (UIButton*)createButton:(CGFloat) x withY:(CGFloat) y withTitle:(NSString*) title withPercent:(CGFloat)percent { UIButton* btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, ballon.size.width * percent, ballon.size.height * percent)]; [btn setCenter:CGPointMake(x, y)]; btn.autoresizingMask = UIViewAutoresizingNone; [btn setBackgroundImage:ballon forState:UIControlStateNormal]; [btn setTitle:title forState:UIControlStateNormal]; [btn addTarget:self action:@selector(tagClick:) forControlEvents:UIControlEventTouchUpInside]; return btn; }

- (UIButton*)createButton:(CGFloat) x withY:(CGFloat) y withTitle:(NSString*) title withPercent:(CGFloat)percent {
UIButton* btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, ballon.size.width * percent, ballon.size.height * percent)];
[btn setCenter:CGPointMake(x, y)];
btn.autoresizingMask = UIViewAutoresizingNone;
[btn setBackgroundImage:ballon forState:UIControlStateNormal];
[btn setTitle:title forState:UIControlStateNormal];
[btn addTarget:self action:@selector(tagClick:) forControlEvents:UIControlEventTouchUpInside];
return btn;

}


4. 点击view上的标记点,弹出描述详细情报的信息框,比如文字加上图片。

因为在iphone的应用中同时只能有一个窗体画面,所以只能采用弹出对话框来显示了,默认的对话框只能显示文字描述,要想显示图片,就需要改造对话框,方法是让类实现协议< UIAlertViewDelegate>,重写方法- (void) willPresentAlertView:(UIAlertView*) alertView ;在这个方法里添加UIImageView来显示图片,改变对话框的大小,以及按钮的位置。

C代码

- (void)tagClick:(id)sender { //[picker takePicture]; UIAlertView* infoView = [[UIAlertView alloc] initWithTitle:@"Info" message:@"some thing is done" delegate:self cancelButtonTitle:@"Close" otherButtonTitles:nil]; [infoView show]; [infoView release]; }

- (void)tagClick:(id)sender {
//[picker takePicture];
UIAlertView* infoView = [[UIAlertView alloc]
initWithTitle:@"Info"
message:@"some thing is done"
delegate:self
cancelButtonTitle:@"Close"
otherButtonTitles:nil];
[infoView show];
[infoView release];
}


5. 在详细信息中播放视频

由于iphone未提供在任意控件内播放视频的功能,所以只能在详细表示画面添加视频播放的按钮,调用MPMoviePlayerController的play方法来播放视屏,MPMoviePlayerController的初始化方法使用initWithContentURL方法加载视频播放的路径URL

C代码

- (void) playMovie { MPMoviePlayerController* mp = [[MPMoviePlayerController alloc] initWithContentURL:movieURL]; if (mp) { self.moviePlayer = mp; [mp release]; } [self.moviePlayer play]; }

- (void) playMovie {
MPMoviePlayerController* mp = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
if (mp) {
self.moviePlayer = mp;
[mp release];
}

[self.moviePlayer play];
}


6. 加载本地文件的路径URL

由于iphone在运行时有一个临时的运行环境,需要使用[NSBundle mainBundle]取得一个主Bundle,使用NSBundle的方法pathForResource:@"Movie" ofType:@"m4v"来生成一个本地运行时的文件路径。

C代码

- (NSURL*)localMovieURL {
if (self.movieURL == nil) {
NSBundle* bundle = [NSBundle mainBundle];
if (bundle) {
NSString* moviePath = [bundle pathForResource:@"Movie" ofType:@"m4v"];

if (moviePath) {
self.movieURL = [NSURL fileURLWithPath:moviePath];
}
}
}
return self.movieURL;
}

- (NSURL*)localMovieURL {
if (self.movieURL == nil) {
NSBundle* bundle = [NSBundle mainBundle];
if (bundle) {
NSString* moviePath = [bundle pathForResource:@"Movie" ofType:@"m4v"];
if (moviePath) {
self.movieURL = [NSURL fileURLWithPath:moviePath];
}
}
}
return self.movieURL;
}


7. 让画面中的按钮view随拍摄方位的变化而移动

让画面中的view的移动,是通过设定UIButton的属性transform来实现的,需要使用[UIView beginAnimations:nil context:nil];启动一个动画环境,设定动画的动作时间以及延迟,通过[UIView commitAnimations];提交动画,transform是通过CGAffineTransformMakeTranslation(x, y)的类来生成,其中x为x方向需要移动的相对距离,y为y方向上需要移动的相对距离。

C代码

- (void)moveButton:(UIButton*)button withOffsetX:(NSInteger)x andOffsetY:(NSInteger)y {

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.0];
[UIView setAnimationDelay:0.0];
CGAffineTransform transform = CGAffineTransformMakeTranslation(x, y);
button.transform = transform;
[UIView commitAnimations];
}

- (void)moveButton:(UIButton*)button withOffsetX:(NSInteger)x andOffsetY:(NSInteger)y {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.0];
[UIView setAnimationDelay:0.0];
CGAffineTransform transform = CGAffineTransformMakeTranslation(x, y);
button.transform = transform;
[UIView commitAnimations];
}


8. 让画面中的按钮view随距离远近而改变按钮view大小

取得当前按钮view 的frame.size,重新设定其width和height,把frame设定回按钮view就可以改变其大小了,可能有通过动画实现的方法,我暂时还未发现。

C代码

- (void)scaleButton:(UIButton*)button withOffsetX:(CGFloat)x andOffsetY:(CGFloat)y {

CGRect frame = button.frame;
frame.size.width = x;
frame.size.height = y;
button.frame = frame;
}

- (void)scaleButton:(UIButton*)button withOffsetX:(CGFloat)x andOffsetY:(CGFloat)y {
CGRect frame = button.frame;
frame.size.width = x;
frame.size.height = y;
button.frame = frame;
}


9. 使用GPS设定其精度并,取得方位(经度,纬度),方向的数据

使用CLLocationManager来取得当前的GPS经度,纬度,方位的数据,首先初始化CLLocationManager,设定其精度,更新回调,更新的距离筛选,通过方法startUpdatingHeading来开启方向的更新,通过方法startUpdatingLocation来开启方位的更新。

C代码

//初始化方位类 self.localManager = [[CLLocationManager alloc] init]; localManager.delegate = self; //开启电子罗盘 if (localManager.headingAvailable) [localManager startUpdatingHeading];//启动 //开启GPS if(localManager.locationServicesEnabled) { localManager.desiredAccuracy = kCLLocationAccuracyBest;//设定为最佳精度 localManager.distanceFilter = 5.0f;//响应位置变化的最小距离(m) [localManager startUpdatingLocation]; }

//初始化方位类
self.localManager = [[CLLocationManager alloc] init];
localManager.delegate = self;

//开启电子罗盘
if (localManager.headingAvailable)
[localManager startUpdatingHeading];//启动

//开启GPS
if(localManager.locationServicesEnabled) {
localManager.desiredAccuracy = kCLLocationAccuracyBest;//设定为最佳精度
localManager.distanceFilter = 5.0f;//响应位置变化的最小距离(m)
[localManager startUpdatingLocation];
}


10. 取得方位(经度,纬度),方向的数据更新回调值

通过实现协议CLLocationManagerDelegate来取得数据更新的回调,其中(BOOL)locationManagerShouldDisplayHeadingCalibration:(CLLocationManager *)manager是方向更新的过滤器,- (void)locationManager:(CLLocationManager
*)manager didUpdateHeading:(CLHeading *)newHeading 是方向更新的回调方法,可以通过newHeading.magneticHeading来取得当前的方向角度(正北方向为0度)。

- (void)locationManager:(CLLocationManager *)manager

didUpdateToLocation:(CLLocation *)newLocation

fromLocation:(CLLocation *)oldLocation为方位(经度,纬度)的更新回调,可以通过newLocation.horizontalAccuracy取得当前定位精度圆半径,newLocation.coordinate.latitude取得纬度数据,newLocation.coordinate.longitude取得精度数据,而-
(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error是GPS初始化失败的回调,应为开启GPS需要得到用户的许可,没有许可就无法正常开启。

C代码

//方位变化的回调函数
- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading {

//初始化方位
if ([fileLoader isKindOfClass:[TestLoader class]]) {

if (localDir == 0) {
baseDir = newHeading.magneticHeading;
localDir = newHeading.magneticHeading;
}
}
float newlocalDir = newHeading.magneticHeading;
//当变化超过一定范围,刷新标志显示
if (abs(newlocalDir - localDir) > FLASH_DEGREE) {
localDir = newlocalDir;
[self computer];
}
//更新指南针方向
[overlayView updateHeading:newHeading];
}
//方位变化的回调函数
- (BOOL)locationManagerShouldDisplayHeadingCalibration:(CLLocationManager *)manager {
return YES;
}

//GPS位置变化的回调
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation {
self.local = newLocation;
//更新经纬度表示值
[overlayView updateLocation:self.local];
if ([fileLoader isKindOfClass:[GPSLoader class]]) {

[fileLoader computerDis:allTags andLocal:local];
//重新计算当前标志点的位置
[self computer];
}
//关闭定位
//[localManager stopUpdatingLocation];
}

//GPS初始化失败
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
NSLog(@"Location manager error: %@", [error description]);
}

//方位变化的回调函数
- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading {
//初始化方位
if ([fileLoader isKindOfClass:[TestLoader class]]) {
if (localDir == 0) {
baseDir = newHeading.magneticHeading;
localDir = newHeading.magneticHeading;
}
}
float newlocalDir = newHeading.magneticHeading;
//当变化超过一定范围,刷新标志显示
if (abs(newlocalDir - localDir) > FLASH_DEGREE) {
localDir = newlocalDir;
[self computer];
}
//更新指南针方向
[overlayView updateHeading:newHeading];
}
//方位变化的回调函数
- (BOOL)locationManagerShouldDisplayHeadingCalibration:(CLLocationManager *)manager {
return YES;
}

//GPS位置变化的回调
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation {
self.local = newLocation;
//更新经纬度表示值
[overlayView updateLocation:self.local];
if ([fileLoader isKindOfClass:[GPSLoader class]]) {
[fileLoader computerDis:allTags andLocal:local];
//重新计算当前标志点的位置
[self computer];
}
//关闭定位
//[localManager stopUpdatingLocation];
}

//GPS初始化失败
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
NSLog(@"Location manager error: %@", [error description]);
}


11. 计算GPS两点间的距离的算法

一种方法通过CLLocation的实例方法getDistanceFrom方法计算两点间的距离(m),

如果没有API可用,可以通过积分的方式利用三角定律估算出两点的直线距离。

C代码

CLLocation* location1 = [[CLLocation alloc] initWithLatitude:latin1 longitude:lonin1]; CLLocation* location2 = [[CLLocation alloc] initWithLatitude:latin2 longitude:lonin2]; return [location1 getDistanceFrom:location2];

CLLocation* location1 = [[CLLocation alloc] initWithLatitude:latin1 longitude:lonin1];
CLLocation* location2 = [[CLLocation alloc] initWithLatitude:latin2 longitude:lonin2];
return [location1 getDistanceFrom:location2];


12. 计算GPS两点间的方向角度的算法

以其中一点作为原点,经过此原点的经度线作为y坐标轴,纬度线作为x坐标轴,在这个坐标系中利用atan2f三角函数取得相对于y轴夹角,再加上对应的偏移量,就可以取得相当于y轴的正方向(正北方向)的角度。

C代码

//计算GPS两点间的经度距离
+ (CGFloat) calcuLoninM:(CGFloat) latin1 withLonin:(CGFloat) lonin1
withDisLatin:(CGFloat) latin2 withDisLonin:(CGFloat) lonin2 {
CGFloat retval = 0.0;
CGFloat latin = latin1;
CGFloat latinStep = (latin1 - latin2) / MAX_LENGTH;
CGFloat loninStep = (lonin1 - lonin2) / MAX_LENGTH;
if (loninStep < 0) {
loninStep = -1.0 * loninStep;
}
for (int i = 0 ; i < MAX_LENGTH; i++) {

retval += EARTH_RADIUS * [GPSHelp toRadians:loninStep] * cos([GPSHelp toRadians:latin]);
latin += latinStep;
}
return retval;
}
//计算GPS两点间的纬度距离
+ (CGFloat) calcuLatinM:(CGFloat) latin1 withLonin:(CGFloat) lonin1
withDisLatin:(CGFloat) latin2 withDisLonin:(CGFloat) lonin2 {
CGFloat angle = latin1 - latin2;
if (angle < 0) {
angle = -1.0 * angle;
}
return [GPSHelp toRadians:angle] * EARTH_RADIUS;
}
//角度转弧度
+ (CGFloat)toRadians:(CGFloat)degree {
return degree / 180.0 * M_PI;
}
//弧度转角度
+ (CGFloat)toDegrees:(CGFloat)radian {
return radian / M_PI * 180.0;
}

@end

@implementation GPSHelp

//计算GPS两点间的角度(正北方向为0度)
+ (CGFloat) calcuAngle:(CGFloat) latin1 withLonin:(CGFloat) lonin1
withDisLatin:(CGFloat) latin2 withDisLonin:(CGFloat) lonin2 {
CGFloat loninM = [GPSHelp calcuLoninM:latin1 withLonin:lonin1 withDisLatin:latin2 withDisLonin:lonin2];
CGFloat latinM = [GPSHelp calcuLatinM:latin1 withLonin:lonin1 withDisLatin:latin2 withDisLonin:lonin2];
CGFloat radian = atan2f(loninM, latinM);
if (lonin2 >= lonin1) {
if (latin2 >= latin1) {
;
} else {
radian = M_PI - radian;
}
} else {
if (latin2 >= latin1) {
radian = 2.0 * M_PI - radian;;
} else {
radian = M_PI + radian;
}
}
return [GPSHelp toDegrees:radian];
}

//计算GPS两点间的经度距离
+ (CGFloat) calcuLoninM:(CGFloat) latin1 withLonin:(CGFloat) lonin1
withDisLatin:(CGFloat) latin2 withDisLonin:(CGFloat) lonin2 {
CGFloat retval = 0.0;
CGFloat latin = latin1;
CGFloat latinStep = (latin1 - latin2) / MAX_LENGTH;
CGFloat loninStep = (lonin1 - lonin2) / MAX_LENGTH;
if (loninStep < 0) {
loninStep = -1.0 * loninStep;
}
for (int i = 0 ; i < MAX_LENGTH; i++) {
retval += EARTH_RADIUS * [GPSHelp toRadians:loninStep] * cos([GPSHelp toRadians:latin]);
latin += latinStep;
}
return retval;
}
//计算GPS两点间的纬度距离
+ (CGFloat) calcuLatinM:(CGFloat) latin1 withLonin:(CGFloat) lonin1
withDisLatin:(CGFloat) latin2 withDisLonin:(CGFloat) lonin2 {
CGFloat angle = latin1 - latin2;
if (angle < 0) {
angle = -1.0 * angle;
}
return [GPSHelp toRadians:angle] * EARTH_RADIUS;
}
//角度转弧度
+ (CGFloat)toRadians:(CGFloat)degree {
return degree / 180.0 * M_PI;
}
//弧度转角度
+ (CGFloat)toDegrees:(CGFloat)radian {
return radian / M_PI * 180.0;
}

@end

@implementation GPSHelp

//计算GPS两点间的角度(正北方向为0度)
+ (CGFloat) calcuAngle:(CGFloat) latin1 withLonin:(CGFloat) lonin1
withDisLatin:(CGFloat) latin2 withDisLonin:(CGFloat) lonin2 {
CGFloat loninM = [GPSHelp calcuLoninM:latin1 withLonin:lonin1 withDisLatin:latin2 withDisLonin:lonin2];
CGFloat latinM = [GPSHelp calcuLatinM:latin1 withLonin:lonin1 withDisLatin:latin2 withDisLonin:lonin2];
CGFloat radian = atan2f(loninM, latinM);
if (lonin2 >= lonin1) {
if (latin2 >= latin1) {
;
} else {
radian = M_PI - radian;
}
} else {
if (latin2 >= latin1) {
radian = 2.0 * M_PI - radian;;
} else {
radian = M_PI + radian;
}
}
return [GPSHelp toDegrees:radian];
}


13. 根据GPS两点间的方向角度以及当前电子罗盘的方向角度算出对应的屏幕的2D坐标值

假设当前的可见角度范围是-90到+90之间,首先计算出电子罗盘的方向角度和GPS两点间的方向角度的角度差,再通过角度格式化成-90到+90之间的一个数值,再计算出这个角度相对于90度的比例,乘上屏幕中心点的x坐标,就可以得到此点对应于当前屏幕的x坐标值。

14. 根据两点间的距离算出对应屏幕的2D坐标值及大小

假设可见的最远距离是200m,让200m距离内的所有标志点显示在屏幕的下半部分,也就是说在>屏幕中心点的y坐标<最大的y坐标的范围内,距离越近的点显示在越靠近屏幕最下方的地方,标记大小也越接近原始大小。根据两点间的距离占200m的比例,计算出y坐标值。大小的计算类同。此算法显示的效果不是很真实,有待继续研究。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: