怎么在iOS中實(shí)現(xiàn)百度地圖定位簽到功能

本篇文章給大家分享的是有關(guān)怎么在iOS中實(shí)現(xiàn)百度地圖定位簽到功能,小編覺得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。

我們注重客戶提出的每個(gè)要求,我們充分考慮每一個(gè)細(xì)節(jié),我們積極的做好成都做網(wǎng)站、成都網(wǎng)站建設(shè)服務(wù),我們努力開拓更好的視野,通過不懈的努力,創(chuàng)新互聯(lián)建站贏得了業(yè)內(nèi)的良好聲譽(yù),這一切,也不斷的激勵(lì)著我們更好的服務(wù)客戶。 主要業(yè)務(wù):網(wǎng)站建設(shè),網(wǎng)站制作,網(wǎng)站設(shè)計(jì),重慶小程序開發(fā)公司,網(wǎng)站開發(fā),技術(shù)開發(fā)實(shí)力,DIV+CSS,PHP及ASP,ASP.Net,SQL數(shù)據(jù)庫的技術(shù)開發(fā)工程師。

一、在APPdelegate.m文件中引入:

#import <BaiduMapAPI_Base/BMKBaseComponent.h>
#import <BMKLocationKit/BMKLocationComponent.h>

加入功能代碼:

#pragma mark 百度地圖設(shè)置
- (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相關(guān)所有的頭文件
#import <BaiduMapAPI_Map/BMKMapComponent.h>//引入地圖功能所有的頭文件

遵循協(xié)議<BMKMapViewDelegate,BMKLocationManagerDelegate>

聲明全局變量

@property (nonatomic, strong) BMKUserLocation *userLocation; //當(dāng)前位置對象
@property (nonatomic, strong) BMKLocationManager *locationManager;/** locationManager*/
@property (nonatomic, strong) BMKMapView *mapView;/** 百度地圖*/
//@property (nonatomic, strong) BMKPointAnnotation* annotation ;/** 標(biāo)記*/
@property (nonatomic, strong) NSMutableArray *annotationArr;/** 標(biāo)記數(shù)組*/
@property (nonatomic, strong) NSMutableArray *circleArr;/** 圓形數(shù)組*/

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

- (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;
 }
}

初始化數(shù)組,這兩個(gè)數(shù)組在接下來會(huì)用到

- (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;
  
 }
}

初始化地圖定位:這里我用的是一次定位而沒有選擇持續(xù)定位。

#pragma mark 初始化locationManager
- (void)initUserLocationManager {
 //因?yàn)閙apView是在一個(gè)分離出來的view中創(chuàng)建的,所以在這里將signSetTypeView中的mapView賦給當(dāng)前viewcontroller的mapView;
 self.mapView = self.mainView.signSetTypeView.mapView;
 self.mapView.delegate = self;
// self.annotation = [[BMKPointAnnotation alloc] init];
 
 // self.mapView是BMKMapView對象
 //精度圈設(shè)置
 BMKLocationViewDisplayParam *param = [[BMKLocationViewDisplayParam alloc] init];
 //設(shè)置顯示精度圈,默認(rèn)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];
 //初始化實(shí)例
 _locationManager = [[BMKLocationManager alloc] init];
 //設(shè)置delegate
 _locationManager.delegate = self;
 //設(shè)置返回位置的坐標(biāo)系類型
 _locationManager.coordinateType = BMKLocationCoordinateTypeBMK09LL;
 //設(shè)置距離過濾參數(shù)
 _locationManager.distanceFilter = kCLDistanceFilterNone;
 //設(shè)置預(yù)期精度參數(shù)
 _locationManager.desiredAccuracy = kCLLocationAccuracyBest;
 //設(shè)置應(yīng)用位置類型
 _locationManager.activityType = CLActivityTypeAutomotiveNavigation;
 //設(shè)置是否自動(dòng)停止位置更新
 _locationManager.pausesLocationUpdatesAutomatically = NO;
 //設(shè)置是否允許后臺定位
 //_locationManager.allowsBackgroundLocationUpdates = YES;
 //設(shè)置位置獲取超時(shí)時(shí)間
 _locationManager.locationTimeout = 15;
 //設(shè)置獲取地址信息超時(shí)時(shí)間
 _locationManager.reGeocodeTimeout = 15;
 //請求一次定位
 [self requestLocation];
}

請求定位,獲取經(jīng)緯度

#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";
   
   //打印經(jīng)緯度
   NSLog(@"didUpdateUserLocation lat %f,long %f",location.location.coordinate.latitude,location.location.coordinate.longitude);
  }
  NSLog(@"netstate = %d",state);
 }];
}

地圖長按選點(diǎn)功能實(shí)現(xiàn):

//長按地圖選點(diǎn)
- (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];
}

選點(diǎn)后彈出選擇定位范圍彈框

#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];
 }];
}

設(shè)置好定位點(diǎn)以及半徑范圍后繪制范圍圈,開始的時(shí)候聲明的circleArr在這里用來盛放添加的區(qū)域圓形,在添加新的圓圈的時(shí)候,將之前舊的移除,保證每次繪制的范圍都是最新的,同理annotationArr也是這個(gè)功能,因?yàn)锳PI有提供的- (void)addOverlays:(NSArray *)overlays;這個(gè)方法:/** *向地圖窗口添加一組Overlay,需要實(shí)現(xiàn)BMKMapViewDelegate的-mapView:viewForOverlay:函數(shù)來生成標(biāo)注對應(yīng)的View *@param overlays 要添加的overlay數(shù)組 */

#pragma mark 添加區(qū)域圓形覆蓋
- (void)circleWithCenterWithCoordinate2D:(CLLocationCoordinate2D )coor radius:(CGFloat)radius {
 
 NSLog(@"coordinate lat %f,long %f",coor.latitude,coor.longitude);
 //賦予點(diǎn)擊選點(diǎn)值
 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;
}

以上就是怎么在iOS中實(shí)現(xiàn)百度地圖定位簽到功能,小編相信有部分知識點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見到或用到的。希望你能通過這篇文章學(xué)到更多知識。更多詳情敬請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

名稱欄目:怎么在iOS中實(shí)現(xiàn)百度地圖定位簽到功能
URL鏈接:http://muchs.cn/article30/gdcpso.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供面包屑導(dǎo)航、網(wǎng)站改版企業(yè)建站、微信小程序、品牌網(wǎng)站制作、標(biāo)簽優(yōu)化

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

微信小程序開發(fā)