iOS 指南針


代碼地址如下:
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:startAngle
                                                             endAngle:endAngle
                                                            clockwise: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)
    {
        //地磁航向數據-》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];
                           
                       }
                       
                   }];
}

三、項目結構截圖

項目結構列表

iOS 指南針

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

注:本文著作權歸作者,由demo大師代發,拒絕轉載,轉載需要作者授權


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM