iOS 指南针

news/2024/10/22 23:50:14/

代码地址如下:
http://www.demodashi.com/demo/11107.html

高仿系统指南针,方向数据是地磁航向数据,有定位地理位置信息和地磁方向信息,可以和系统的指南针对比看一看。

一、运行效果预览

总效果.gif
效果.png

二、实现过程

1.继承于UIView创建一个带刻度标注的视图ScaleView,利用UIBezierPath和CAShapeLayer、UILabel,默认0刻度(北)在最上方。

//化刻度表
- (void)paintingScale{CGFloat perAngle = M_PI/(90);NSArray *array = @[@"北",@"东",@"南",@"西"];//画圆环,每隔2°画一个弧线,总共180条for (int i = 0; i < 180; i++) {CGFloat startAngle = (-(M_PI_2+M_PI/180/2)+perAngle*i);CGFloat endAngle = startAngle+perAngle/2;UIBezierPath *bezPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(self.frame.size.width/2, self.frame.size.height/2)radius:(self.frame.size.width/2 - 50)startAngle:startAngleendAngle:endAngleclockwise:YES];CAShapeLayer *shapeLayer = [CAShapeLayer layer];//每隔30°画一个白条刻度if (i%15 == 0) {shapeLayer.strokeColor = [[UIColor whiteColor] CGColor];shapeLayer.lineWidth = 20;}else{shapeLayer.strokeColor = [[UIColor grayColor] CGColor];shapeLayer.lineWidth = 10;}shapeLayer.path = bezPath.CGPath;shapeLayer.fillColor = [UIColor clearColor].CGColor;[_backgroundView.layer addSublayer:shapeLayer];//每隔30°画一个刻度的标注 0 30 60...if (i % 15 == 0){NSString *tickText = [NSString stringWithFormat:@"%d",i * 2];CGFloat textAngel = startAngle+(endAngle-startAngle)/2;CGPoint point = [self calculateTextPositonWithArcCenter:CGPointMake(self.frame.size.width/2, self.frame.size.height/2)Angle:textAngel andScale:1.2];UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(point.x, point.y, 30, 15)];label.center = point;label.text = tickText;label.textColor = [UIColor whiteColor];label.font = [UIFont systemFontOfSize:15];label.textAlignment = NSTextAlignmentCenter;[_backgroundView addSubview:label];//标注 北 东 南 西if (i%45 == 0){tickText = array[i/45];CGPoint point2 = [self calculateTextPositonWithArcCenter:CGPointMake(self.frame.size.width/2, self.frame.size.height/2)Angle:textAngel andScale:0.8];UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(point2.x, point2.y, 30, 20)];label.center = point2;label.text = tickText;label.textColor = [UIColor whiteColor];label.font = [UIFont systemFontOfSize:20];label.textAlignment = NSTextAlignmentCenter;if ([tickText isEqualToString:@"北"]) {UILabel * markLabel = [[UILabel alloc]initWithFrame:CGRectMake(point.x, point.y, 8, 8)];markLabel.center = CGPointMake(point.x, point.y + 12);markLabel.clipsToBounds = YES;markLabel.layer.cornerRadius = 4;markLabel.backgroundColor = [UIColor redColor];[_backgroundView addSubview:markLabel];   }[_backgroundView addSubview:label];}}} //画十字线,参照线UIView *  levelView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width/2/2, 1)];levelView.center = CGPointMake(self.frame.size.width/2, self.frame.size.height/2);levelView.backgroundColor = [UIColor whiteColor];[self addSubview:levelView];UIView *  verticalView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, self.frame.size.width/2/2)];verticalView.center = CGPointMake(self.frame.size.width/2, self.frame.size.height/2);verticalView.backgroundColor = [UIColor whiteColor];[self addSubview:verticalView];UIView * lineView = [[UIView alloc] initWithFrame:CGRectMake(self.frame.size.width/2 -1.5, self.frame.size.height/2 - (self.frame.size.width/2 - 50) - 50, 3, 30 + 30)];lineView.backgroundColor = [UIColor whiteColor];[self addSubview:lineView];
}

2、利用CLLocationManager初始化定位装置,并设置代理 ,记得在info.plist中加入隐私定位权限关键字 Privacy - Location When In Use Usage Description

  // 注意开启手机的定位服务,隐私那里的self.locationManager = [[CLLocationManager alloc]init];self.locationManager.delegate=self;//  定位频率,每隔多少米定位一次// 距离过滤器,移动了几米之后,才会触发定位的代理函数self.locationManager.distanceFilter = 0;// 定位的精度,越精确,耗电量越高self.locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;//导航//请求允许在前台获取用户位置的授权[self.locationManager requestWhenInUseAuthorization];//允许后台定位更新,进入后台后有蓝条闪动self.locationManager.allowsBackgroundLocationUpdates = YES;//判断定位设备是否能用和能否获得导航数据if ([CLLocationManager locationServicesEnabled]&&[CLLocationManager headingAvailable]){[self.locationManager startUpdatingLocation];//开启定位服务[self.locationManager startUpdatingHeading];//开始获得航向数据}else{NSLog(@"不能获得航向数据");}

通过实现定位装置的代理方法:
-(void)locationManager:(CLLocationManager )manager didUpdateHeading:(CLHeading )newHeading 来获得地理和地磁航向数据,从而转动地理刻度表以及表上的文字标注;
方法-(BOOL)locationManagerShouldDisplayHeadingCalibration:(CLLocationManager *)manager返回Yes是为了受到外来磁场干扰时,设备会自动进行校验。

#pragma mark - CLLocationManagerDelegate
//获得地理和地磁航向数据,从而转动地理刻度表
-(void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading{//获得当前设备UIDevice *device =[UIDevice currentDevice];//   判断磁力计是否有效,负数时为无效,越小越精确if (newHeading.headingAccuracy>0){//地磁航向数据-》magneticHeadingfloat magneticHeading =[self heading:newHeading.magneticHeading fromOrirntation:device.orientation];//地理航向数据-》trueHeadingfloat trueHeading =[self heading:newHeading.trueHeading fromOrirntation:device.orientation];//地磁北方向float heading = -1.0f *M_PI *newHeading.magneticHeading /180.0f;_angleLabel.text = [NSString stringWithFormat:@"%3.1f°",magneticHeading];//旋转变换[_scaView resetDirection:heading]; //返回当前手机(摄像头)朝向方向[self updateHeading:newHeading]; }
}
//判断设备是否需要校验,受到外来磁场干扰时
-(BOOL)locationManagerShouldDisplayHeadingCalibration:(CLLocationManager *)manager
{return YES;
}
//旋转重置刻度标志的方向
- (void)resetDirection:(CGFloat)heading{_backgroundView.transform = CGAffineTransformMakeRotation(heading);for (UILabel * label in _backgroundView.subviews) {label.transform = CGAffineTransformMakeRotation(-heading);}
}

3、通过代理方法获得经纬度以及海拔数据,然后利用经纬度进行地理反编码获得地理位置信息。

// 定位成功之后的回调方法,只要位置改变,就会调用这个方法
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{self.currLocation = [locations lastObject];//维纬度NSString * latitudeStr = [NSString stringWithFormat:@"%3.2f",_currLocation.coordinate.latitude];//经度NSString * longitudeStr  = [NSString stringWithFormat:@"%3.2f",_currLocation.coordinate.longitude];//高度NSString * altitudeStr  = [NSString stringWithFormat:@"%3.2f",_currLocation.altitude];NSLog(@"纬度 %@  经度 %@  高度 %@", latitudeStr, longitudeStr, altitudeStr);_latitudlongitudeLabel.text = [NSString stringWithFormat:@"纬度:%@  经度:%@  海拔:%@", latitudeStr, longitudeStr, altitudeStr];CLGeocoder *geocoder = [[CLGeocoder alloc] init];[geocoder reverseGeocodeLocation:self.currLocationcompletionHandler:^(NSArray *placemarks, NSError *error) {if ([placemarks count] > 0) {CLPlacemark *placemark = placemarks[0];NSDictionary *addressDictionary =  placemark.addressDictionary;NSString *street = [addressDictionaryobjectForKey:(NSString *)kABPersonAddressStreetKey];street = street == nil ? @"": street;NSString *country = placemark.country;NSString * subLocality = placemark.subLocality;NSString *city = [addressDictionaryobjectForKey:(NSString *)kABPersonAddressCityKey];city = city == nil ? @"": city;NSLog(@"%@",[NSString stringWithFormat:@"%@ \n%@ \n%@  %@ ",country, city,subLocality ,street]);_positionLabel.text = [NSString stringWithFormat:@" %@\n %@ %@%@ " ,country, city,subLocality ,street];}}];
}

三、项目结构截图

项目结构列表

iOS 指南针

代码地址如下:
http://www.demodashi.com/demo/11107.html

注:本文著作权归作者,由demo大师发表,拒绝转载,转载需要作者授权


http://www.ppmy.cn/news/707116.html

相关文章

绝逼实用又美观的指南针应用(App)

很多应用花钱购买下载量&#xff0c;买评论&#xff0c;制造一个个虚假繁荣。看似光鲜的外表下一个个丑陋的UI&#xff0c;垃圾般的功能。这就是在各大应用市场搜索关键字“指南针”找到的应用的真实写照。更恶心的是那些二次打包别个英文软件的打包党&#xff0c;一个个缺陷横…

iOS指南针

前言&#xff1a; 这个小项目使用到了CoreLocation框架里面的设备朝向功能&#xff0c;对CoreLocation感兴趣的可以翻一下之前的文章 有朋友发现一个尴尬的问题&#xff08;图片的东西2个方向是不对的&#xff09;&#xff0c;原谅我的大意&#xff0c;赶时间就直接百度了张图…

指南针

一 原理分析 移动设备中一般都包括方向传感器&#xff0c;而方向传感器是用于感应设备的摆放状态&#xff0c;方向传感器可以为我们返回三个角度&#xff0c;这样一来就可以知道设备的摆放状态&#xff0c;下面逐一说明下这三个角度&#xff1a; 第一个角度&#xff1a;表示手机…

Android之指南针学习

点我下载源码 5月12日更新到V5版&#xff1a;http://download.csdn.net/detail/weidi1989/5364243 今天&#xff0c;在小米的开源项目中下载了一个指南针源码学习了一下&#xff0c;感觉不仅界面做得很漂亮&#xff0c;代码也是很精致&#xff0c;所以我在研究了之后&#xff…

指南针为什么不叫指北针

相信大家对于指南针都会有个疑问&#xff0c;明明是指北&#xff0c;为什么要叫指南针呢&#xff1f;以下是我从网上查阅的资料解释这个问题的。以下资料来自&#xff08;http://history.gmw.cn/2016-02/20/content_18952179.htm&#xff09; 指南针为什么不叫指北针&#xff…

手机上的指南针怎么用?分享两个指南针使用方法给你

方向感是大部分“路痴人”的难题&#xff0c;当你一个人在外面&#xff0c;对于眼前的方位一无所知时&#xff0c;你会怎么办呢&#xff1f;这里要提醒大家不要忘了&#xff0c;其实我们手机中有一个“指南针”&#xff0c;我们可以借助它来帮助我们获取方向指引&#xff0c;那…

用指针将整型数组的值逆序存放(二)

下面是一个使用冒泡排序法对整型数组进行升序排序的示例代码&#xff1a; #include <stdio.h>// 使用冒泡排序法对整型数组进行升序排序 void bubbleSort(int *array, int length) {for (int i 0; i < length - 1; i) {for (int j 0; j < length - i - 1; j) {i…

adb多设备调试

文章目录 adb操作多个在线设备&#x1f47a;例-s-t adb操作多个在线设备&#x1f47a; PS C:\Users\cxxu\Desktop> adb --help Android Debug Bridge version 1.0.41 Version 33.0.2-8557947 Installed as D:\exes\platform-tools\adb.exeglobal options:-a …