博客> iOS 仿系统指南针
iOS 仿系统指南针
2019-10-11 12:32 评论:0 阅读:509 wslcmk
ios 磁力计 指南针

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

一、运行效果

总效果.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 xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed self.locationManager.delegate=self; xss=removed xss=removed xss=removed> 通过实现定位装置的代理方法:
-(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) { //地磁航向数据-》magneticHeading float magneticHeading =[self heading:newHeading.magneticHeading fromOrirntation:device.orientation]; //地理航向数据-》trueHeading float 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.currLocation
                   completionHandler:^(NSArray *placemarks, NSError *error) {

                       if ([placemarks count] > 0) {

                           CLPlacemark *placemark = placemarks[0];

                           NSDictionary *addressDictionary =  placemark.addressDictionary;

                           NSString *street = [addressDictionary
                                               objectForKey:(NSString *)kABPersonAddressStreetKey];
                           street = street == nil ? @"": street;

                           NSString *country = placemark.country;

                           NSString * subLocality = placemark.subLocality;

                           NSString *city = [addressDictionary
                                             objectForKey:(NSString *)kABPersonAddressCityKey];
                           city = city == nil ? @"": city;

                           NSLog(@"%@",[NSString stringWithFormat:@"%@ \n%@ \n%@  %@ ",country, city,subLocality ,street]);

                           _positionLabel.text = [NSString stringWithFormat:@" %@\n %@ %@%@ " ,country, city,subLocality ,street];

                       }

                   }];
}

本汪.gif

示例Github:iOS 仿系统指南针

点个赞吧.jpg

欢迎扫描下方二维码关注——iOS开发进阶之路——微信公众号:iOS2679114653 本公众号是一个iOS开发者们的分享,交流,学习平台,会不定时的发送技术干货,源码,也欢迎大家积极踊跃投稿,(择优上头条) ^_^分享自己开发攻城的过程,心得,相互学习,共同进步,成为攻城狮中的翘楚!

iOS开发进阶之路.jpg

收藏
1
sina weixin mail 回到顶部