iOS 百度地圖定位簽到實現

寫在前面:
項目需求用到這個功能,主要目的是實現老師設置位置簽到範圍,學生在一定範圍內進行簽到的功能。功能如下方截圖:


簡要介紹:

下面記錄一下主要的實現流程,功能的實現主要是根據百度地圖開發者官網提供的api文檔,各項功能之間組合。百度地圖的SDK現在分成了地圖功能和定位功能兩塊不同的SDK,BaiduMapAPI這個是基礎的地圖功能,BMKLocationKit這個是定位功能。項目裏實現定位簽到功能用的的SDK包括上面說的這兩個模塊,所以在用cocopods引入framework的時候,需要引入:#百度地圖 pod 'BMKLocationKit' pod 'BaiduMapKit'

功能實現

一、在APPdelegate.m文件中引入:
#import <BaiduMapAPI_Base/BMKBaseComponent.h>
#import <BMKLocationKit/BMKLocationComponent.h>

加入功能代碼:

#pragma mark 百度地圖設置
- (void)configBaiduMap {
    NSString *ak = @"xxxx";
    BMKMapManager *mapManager = [[BMKMapManager alloc] init];
    self.mapManager = mapManager;
    BOOL ret = [mapManager start:ak generalDelegate:nil];
    [[BMKLocationAuth sharedInstance] checkPermisionWithKey:ak authDelegate:self];
    if (!ret) {
        NSLog(@"manager start failed!");
    } 
}
二、在用到地圖定位功能的viewController中
#import <BMKLocationKit/BMKLocationComponent.h>
#import <BaiduMapAPI_Base/BMKBaseComponent.h>//引入base相關所有的頭文件
#import <BaiduMapAPI_Map/BMKMapComponent.h>//引入地圖功能所有的頭文件
遵循協議<BMKMapViewDelegate,BMKLocationManagerDelegate>
聲明全局變量
@property (nonatomic, strong) BMKUserLocation *userLocation; //當前位置對象
@property (nonatomic, strong) BMKLocationManager *locationManager;/** locationManager*/
@property (nonatomic, strong) BMKMapView *mapView;/** 百度地圖*/
//@property (nonatomic, strong) BMKPointAnnotation* annotation ;/** 標記*/
@property (nonatomic, strong) NSMutableArray *annotationArr;/** 標記數組*/
@property (nonatomic, strong) NSMutableArray *circleArr;/** 圓形數組*/

地圖SDK文檔中建議在以下代碼中如此設置, 目的是控制內存

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [_mapView viewWillAppear];
    _mapView.delegate = self;
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [_mapView viewWillDisappear];
    _mapView.delegate = nil;
}

- (void)dealloc {
    if (_mapView) {
        _mapView = nil;
    }
}

初始化數組,這兩個數組在接下來會用到

- (NSMutableArray *)annotationArr {
 
    if (!_annotationArr) {
        _annotationArr = [NSMutableArray array];
    }
    return _annotationArr;
}

- (NSMutableArray *)circleArr {
    if (!_circleArr) {
        _circleArr = [NSMutableArray array];
    }
    return _circleArr;
}

添加地圖view

#pragma mark 添加地圖
- (void)addSignMapBgView {
    if (!self.mapBgView) {
        UIView *mapBgView = [UIView new];
        self.mapBgView = mapBgView;
        mapBgView.backgroundColor = [CommUtls colorWithHexString:APP_BgColor];
        [self addSubview:mapBgView];
        [mapBgView makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.tipView.bottom);
            make.left.right.bottom.equalTo(0);
        }];
        
        _mapView = [[BMKMapView alloc] initWithFrame:CGRectZero];
//        _mapView.delegate = self;
        [_mapView setZoomLevel:21];//精確到5米
        _mapView.showsUserLocation = YES;//顯示定位圖層
        [mapBgView addSubview:_mapView];
        [_mapView makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(0);
        }];
        _mapView.userTrackingMode = BMKUserTrackingModeNone;
        
    }

}

初始化地圖定位:這裏我用的是一次定位而沒有選擇持續定位。

#pragma mark 初始化locationManager
- (void)initUserLocationManager {
    //因爲mapView是在一個分離出來的view中創建的,所以在這裏將signSetTypeView中的mapView賦給當前viewcontroller的mapView;
    self.mapView = self.mainView.signSetTypeView.mapView;
    self.mapView.delegate = self;
//    self.annotation = [[BMKPointAnnotation alloc] init];
    
    // self.mapView是BMKMapView對象
    //精度圈設置
    BMKLocationViewDisplayParam *param = [[BMKLocationViewDisplayParam alloc] init];
    //設置顯示精度圈,默認YES
    param.isAccuracyCircleShow = YES;
    //精度圈 邊框顏色
    param.accuracyCircleStrokeColor = [UIColor colorWithRed:242/255.0 green:129/255.0 blue:126/255.0 alpha:1];
    //精度圈 填充顏色
    param.accuracyCircleFillColor = [UIColor colorWithRed:242/255.0 green:129/255.0 blue:126/255.0 alpha:0.3];
    [self.mapView updateLocationViewWithParam:param];
    
    self.userLocation = [[BMKUserLocation alloc] init];
    //初始化實例
    _locationManager = [[BMKLocationManager alloc] init];
    //設置delegate
    _locationManager.delegate = self;
    //設置返回位置的座標系類型
    _locationManager.coordinateType = BMKLocationCoordinateTypeBMK09LL;
    //設置距離過濾參數
    _locationManager.distanceFilter = kCLDistanceFilterNone;
    //設置預期精度參數
    _locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    //設置應用位置類型
    _locationManager.activityType = CLActivityTypeAutomotiveNavigation;
    //設置是否自動停止位置更新
    _locationManager.pausesLocationUpdatesAutomatically = NO;
    //設置是否允許後臺定位
    //_locationManager.allowsBackgroundLocationUpdates = YES;
    //設置位置獲取超時時間
    _locationManager.locationTimeout = 15;
    //設置獲取地址信息超時時間
    _locationManager.reGeocodeTimeout = 15;
    //請求一次定位
    [self requestLocation];
}

請求定位,獲取經緯度

#pragma mark 請求定位
- (void)requestLocation {
    
    [_locationManager requestLocationWithReGeocode:YES withNetworkState:YES completionBlock:^(BMKLocation * _Nullable location, BMKLocationNetworkState state, NSError * _Nullable error) {
        if (error)
        {
            NSLog(@"locError:{%ld - %@};", (long)error.code, error.localizedDescription);
        }
        if (location) {//得到定位信息,添加annotation
            
            if (location.location) {
                NSLog(@"LOC = %@",location.location);
            }
            if (location.rgcData) {
                NSLog(@"rgc = %@",[location.rgcData description]);
            }
            
            if (!location) {
                return;
            }
            if (!self.userLocation) {
                self.userLocation = [[BMKUserLocation alloc] init];
            }
            self.userLocation.location = location.location;
            [self.mapView updateLocationData:self.userLocation];
            CLLocationCoordinate2D mycoordinate = location.location.coordinate;
            self.mapView.centerCoordinate = mycoordinate;
            
            //賦予初始值
            self.viewModel.lat = [NSString stringWithFormat:@"%f", location.location.coordinate.latitude];
            self.viewModel.lng = [NSString stringWithFormat:@"%f",location.location.coordinate.longitude];
            self.viewModel.radius = @"50";
            
            //打印經緯度
            NSLog(@"didUpdateUserLocation lat %f,long %f",location.location.coordinate.latitude,location.location.coordinate.longitude);
        }
        NSLog(@"netstate = %d",state);
    }];
}

地圖長按選點功能實現:

//長按地圖選點
- (void)mapview:(BMKMapView *)mapView onLongClick:(CLLocationCoordinate2D)coordinate {
    
    if (self.annotationArr.count > 0) {
        [mapView removeAnnotations:self.annotationArr];
        [self.annotationArr removeAllObjects];
        
        BMKPointAnnotation *annotation = [[BMKPointAnnotation alloc]init];
        annotation.coordinate = coordinate;
        [self.annotationArr addObject:annotation];
        [mapView addAnnotations:self.annotationArr];
    } else {
        BMKPointAnnotation *annotation = [[BMKPointAnnotation alloc]init];
        annotation.coordinate = coordinate;
        [self.annotationArr addObject:annotation];
        [mapView addAnnotations:self.annotationArr];
    }
    //彈出半徑選擇框
    [self showLocationSelectRadiusViewWithCoordinate:coordinate];
}

選點後彈出選擇定位範圍彈框

#pragma mark 彈出位置彈框
- (void)showLocationSelectRadiusViewWithCoordinate:(CLLocationCoordinate2D)coordinate {
    ExtraActLocationSignPopView *popView = [ExtraActLocationSignPopView new];
    [popView show];
    @weakify(self);
    [popView.locatioonSureSignal subscribeNext:^(NSString *x) {
        @strongify(self);
        self.viewModel.radius = x;
        CGFloat radius = [x floatValue];
        [self circleWithCenterWithCoordinate2D:coordinate radius:radius];
    }];
}

設置好定位點以及半徑範圍後繪製範圍圈,開始的時候聲明的circleArr在這裏用來盛放添加的區域圓形,在添加新的圓圈的時候,將之前舊的移除,保證每次繪製的範圍都是最新的,同理annotationArr也是這個功能,因爲API有提供的- (void)addOverlays:(NSArray *)overlays;這個方法:/** *向地圖窗口添加一組Overlay,需要實現BMKMapViewDelegate的-mapView:viewForOverlay:函數來生成標註對應的View *@param overlays 要添加的overlay數組 */

#pragma mark 添加區域圓形覆蓋
- (void)circleWithCenterWithCoordinate2D:(CLLocationCoordinate2D )coor radius:(CGFloat)radius {
    
    NSLog(@"coordinate lat %f,long %f",coor.latitude,coor.longitude);
    //賦予點擊選點值
    self.viewModel.lat = [NSString stringWithFormat:@"%f", coor.latitude];
    self.viewModel.lng = [NSString stringWithFormat:@"%f",coor.longitude];
    
    if (self.circleArr.count > 0) {
        [_mapView removeOverlays:self.circleArr];
        [self.circleArr removeAllObjects];
        
        BMKCircle *circle = [BMKCircle circleWithCenterCoordinate:coor radius:radius];
        [self.circleArr addObject:circle];
        [_mapView addOverlays:self.circleArr];
    } else {
        BMKCircle *circle = [BMKCircle circleWithCenterCoordinate:coor radius:radius];
        [self.circleArr addObject:circle];
        [_mapView addOverlays:self.circleArr];
    }
}

#pragma mark 重繪overlay
- (BMKOverlayView *)mapView:(BMKMapView *)mapView viewForOverlay:(id <BMKOverlay>)overlay{
    if ([overlay isKindOfClass:[BMKCircle class]]){
        BMKCircleView* circleView = [[BMKCircleView alloc] initWithOverlay:overlay];
        circleView.fillColor = [UIColor colorWithRed:33/255.0 green:196/255.0 blue:206/255.0 alpha:0.3];
        circleView.strokeColor = [UIColor colorWithRed:33/255.0 green:196/255.0 blue:206/255.0 alpha:1];
        circleView.lineWidth = 1.0;
        return circleView;
    }
    return nil;
}

至此,在地圖上選點進行簽到功能基本實現,另外,關於 自定義的範圍圓圈的顏色,邊框大小都是可以自定義的,選點的標記也是可以自定義的,官方文檔有說明

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章