应为开启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坐标值。大小的计算类同。 此算法显示的效果不是很真实,有待继续研究。
+++++