iOS 바 이 두 맵 포 지 셔 닝 출석 기능 구현

앞 에 쓰기:
프로젝트 수 요 는 이 기능 을 사용 하 는데 주요 목적 은 선생님 이 위치 출석 범 위 를 설정 하고 학생 들 이 일정한 범위 내 에서 출석 하 는 기능 을 실현 하 는 것 이다.
아래 캡 처 기능:

화면 스냅 샷 2019-01-28 오전 10.29.26.png
간단 한 소개:
다음은 주요 한 실현 절 차 를 기록 하 겠 습 니 다.기능 의 실현 은 주로 바 이 두 지도 개발 자 홈 페이지 에서 제공 하 는 api 문서,각종 기능 간 의 조합 입 니 다.바 이 두 맵 의 SDK 는 현재 맵 기능 과 포 지 셔 닝 기능 이 서로 다른 SDK 로 나 뉘 어 져 있 으 며,Baidu MapAPI 는 기본 적 인 맵 기능 이 며,BMKLocationKit 는 포 지 셔 닝 기능 이다.프로젝트 에서 포 지 셔 닝 서명 기능 을 실현 하 는 SDK 는 위 에서 말 한 두 모듈 을 포함 하기 때문에 cocopods 로 framework 를 도입 할 때 도입 해 야 합 니 다.# pod 'BMKLocationKit' pod 'BaiduMapKit'기능 실현
1.APpdegate.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!");
 } 
}
2.지도 포 지 셔 닝 기능 을 사용 하 는 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;
}
지도 보기 추가

#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];
 }];
}
포 지 셔 닝 포인트 와 반경 범 위 를 설정 한 후 범위 권 을 그립 니 다.시작 할 때 설명 한 circle Arr 는 추 가 된 영역 원형 을 담 는 데 사 용 됩 니 다.새로운 원 을 추가 할 때 이전 것 을 제거 하고 매번 그 리 는 범위 가 최신 이 되도록 합 니 다.같은 원리 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;
}
이로써 지도 에서 지점 을 선택 하여 서명 하 는 기능 이 기본적으로 실현 되 었 습 니 다.또한 사용자 정의 범위 동그라미 의 색상 에 대해 서 는 테두리 크기 를 사용자 정의 할 수 있 고 선택 한 태그 도 사용자 정의 할 수 있 습 니 다.공식 문서 에 설명 이 있 습 니 다.
총결산
이상 은 이 글 의 전체 내용 입 니 다.본 논문 의 내용 이 여러분 의 학습 이나 업무 에 어느 정도 참고 학습 가치 가 있 기 를 바 랍 니 다.궁금 한 점 이 있 으 시 면 댓 글 을 남 겨 주 셔 서 저희 에 대한 지지 에 감 사 드 립 니 다.

좋은 웹페이지 즐겨찾기