前言
需要實(shí)現(xiàn)員工巡查功能硫狞,展示地圖,并顯示員工當(dāng)前所在位置晃痴。當(dāng)該員工點(diǎn)擊“開(kāi)始巡查”按鈕残吩,則追隨其腳步,進(jìn)行軌跡繪制倘核,直到員工點(diǎn)擊“結(jié)束巡查”按鈕泣侮,完成軌跡的繪制,并截圖上傳至后臺(tái)紧唱。
注冊(cè)和獲取百度地圖的秘鑰
引入第三方庫(kù)
pod 'BMKLocationKit'
pod 'BaiduMapKit'
//引入base相關(guān)所有的頭文件
#import <BaiduMapAPI_Base/BMKBaseComponent.h>
//引入地圖功能所有的頭文件
#import <BaiduMapAPI_Map/BMKMapComponent.h>
@interface RouteViewController ()<BMKMapViewDelegate, BMKLocationManagerDelegate>
@end
地圖設(shè)置
#pragma mark - 地圖懶加載
-(BMKMapView *)mapView
{
if (!_mapView) {
_mapView = [[BMKMapView alloc] initWithFrame:CGRectMake(0, kTopHeight, KScreenWidth, KScreenHeight-kTopHeight-100)];
//地圖放縮大小活尊,4-21
_mapView.zoomLevel = 17;
//定位模式,定位跟隨模式才會(huì)有個(gè)箭頭跟著人走漏益,羅盤(pán)的話會(huì)有一個(gè)圈圈
_mapView.userTrackingMode = BMKUserTrackingModeFollow;
//設(shè)定地圖View能否支持所有手勢(shì)操作
_mapView.gesturesEnabled= YES;
_mapView.delegate= self;
//是否顯示定位圖層蛹锰,默認(rèn)為NO,則不顯示跟著人走的箭頭绰疤。
_mapView.showsUserLocation = YES;
//此類(lèi)表示定位圖層自定義樣式參數(shù)
BMKLocationViewDisplayParam *param = [[BMKLocationViewDisplayParam alloc] init];
//不顯示定位圖層的圈圈
param.isAccuracyCircleShow = NO;
[_mapView updateLocationViewWithParam:param];
//設(shè)置地圖模式為標(biāo)準(zhǔn)地圖
[_mapView setMapType:BMKMapTypeStandard];
}
return _mapView;
}
定位設(shè)置
#pragma mark - 設(shè)置locationManager宁仔,定位到當(dāng)前位置
-(void)setupLocationManager
{
//初始化實(shí)例
self.locationManager = [[BMKLocationManager alloc] init];
self.locationManager.delegate = self;
//設(shè)置返回位置的坐標(biāo)系類(lèi)型
self.locationManager.coordinateType = BMKLocationCoordinateTypeBMK09LL;
//設(shè)置距離過(guò)濾參數(shù),超過(guò)10米才更新位置參數(shù)
self.locationManager.distanceFilter = 10;
//設(shè)置預(yù)期精度參數(shù)
self.locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
//設(shè)置應(yīng)用位置類(lèi)型
self.locationManager.activityType = CLActivityTypeAutomotiveNavigation;
//設(shè)置是否自動(dòng)停止位置更新
self.locationManager.pausesLocationUpdatesAutomatically = NO;
//設(shè)置是否允許后臺(tái)定位
self.locationManager.allowsBackgroundLocationUpdates = YES;
//設(shè)置位置獲取超時(shí)時(shí)間
self.locationManager.locationTimeout = 10;
//設(shè)置獲取地址信息超時(shí)時(shí)間
self.locationManager.reGeocodeTimeout = 10;
[self.locationManager startUpdatingHeading];
//打開(kāi)這個(gè)才能在地圖加載后定位到當(dāng)前位置峦睡,不然坐標(biāo)點(diǎn)箭頭會(huì)跑到幾內(nèi)亞灣去
[self.locationManager startUpdatingLocation];
}
定位代理
-(void)BMKLocationManager:(BMKLocationManager *)manager didUpdateHeading:(CLHeading *)heading
{
if (!heading) {
return;
}
if (!self.userLocation) {
self.userLocation = [[BMKUserLocation alloc] init];
}
self.userLocation.heading = heading;
[self.mapView updateLocationData:self.userLocation];
}
-(void)BMKLocationManager:(BMKLocationManager *)manager didUpdateLocation:(BMKLocation *)location orError:(NSError *)error
{
if (error) {
NSLog(@"locError:{%ld - %@};", (long)error.code, error.localizedDescription);
}
if (!location) {
return ;
}
if (!self.userLocation) {
self.userLocation = [[BMKUserLocation alloc] init];
}
self.userLocation.location = location.location;
[self.mapView updateLocationData:self.userLocation];
//設(shè)置一個(gè)開(kāi)始繪制軌跡的flag翎苫,當(dāng)flag為真的時(shí)候,開(kāi)始繪制軌跡榨了,在此處寫(xiě)代碼來(lái)繪制
}
地圖代理
#pragma mark - 添加線圖
-(BMKOverlayView *)mapView:(BMKMapView *)mapView viewForOverlay:(id<BMKOverlay>)overlay
{
if ([overlay isKindOfClass:[BMKPolyline class]]) {
BMKPolylineView *polylineView = [[BMKPolylineView alloc] initWithOverlay:overlay];
polylineView.strokeColor = CSecondaryColor;
polylineView.lineWidth = 2.0;
return polylineView;
}
return nil;
}
#pragma mark - 添加完線圖
-(void)mapView:(BMKMapView *)mapView didAddOverlayViews:(NSArray *)overlayViews
{
//每次添加完線圖煎谍,把地圖的中心設(shè)置成起始點(diǎn)和當(dāng)前位置點(diǎn)的中點(diǎn)
CLLocationDegrees longitude = (self.startLocation.location.coordinate.longitude + self.userLocation.location.coordinate.longitude)/2;
CLLocationDegrees latitude = (self.startLocation.location.coordinate.latitude + self.userLocation.location.coordinate.latitude)/2;
_mapView.centerCoordinate = CLLocationCoordinate2DMake(latitude, longitude);
}
自定義Annotation
-(BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id<BMKAnnotation>)annotation
{
if ([annotation isKindOfClass:[BMKPointAnnotation class]]) {
static NSString *reuseID = @"anotationReuseIndetifier";
BMKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:reuseID];
if (annotationView == nil) {
annotationView = [[BMKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:reuseID];
}
UILabel *lblStart = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
if (!self.isStartTrace) {
lblStart.text = @"始";
}
else{
lblStart.text = @"終";
}
lblStart.backgroundColor = CSecondaryColor;
lblStart.font = [UIFont systemFontOfSize:14.0];
lblStart.textAlignment = NSTextAlignmentCenter;
lblStart.textColor = [UIColor whiteColor];
lblStart.layer.cornerRadius = 15;
lblStart.layer.borderWidth = 1;
lblStart.layer.masksToBounds = YES;
lblStart.layer.borderColor = [UIColor darkGrayColor].CGColor;
[annotationView addSubview:lblStart];
return annotationView;
}
return nil;
}