image.png
#import "JXActivityScopeTwoVC.h"
#import <BaiduMapAPI_Map/BMKMapComponent.h>//引入地图功能所有的头文件
#import <BMKLocationKit/BMKLocationComponent.h>//引入定位功能所有的头文件
//#import <BaiduMapAPI_Search/BMKSearchComponent.h>//引入检索功能所有的头文件
//#import <BaiduMapAPI_Base/BMKBaseComponent.h>//引入base相关所有的头文件
//#import <BaiduMapAPI_Utils/BMKUtilsComponent.h>//引入计算工具所有的头文件
@interface JXActivityScopeTwoVC ()<BMKMapViewDelegate,BMKLocationManagerDelegate>
@property (nonatomic, strong) BMKMapView *mapView;
@property (nonatomic, strong) BMKLocationManager *locationManager; //定位对象
@property (nonatomic, strong) BMKLocationViewDisplayParam *param; //定位图层自定义样式参数
@property (nonatomic, strong) BMKUserLocation *userLocation; //当前位置对象
@end
@implementation JXActivityScopeTwoVC
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[_mapView viewWillAppear];
}
-(void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[_mapView viewWillDisappear];
}
- (void)viewDidLoad {
[super viewDidLoad];
[self.view addSubview:self.myNavigationBar];
self.myNavigationBar.titleLabel.text = @"活动范围";
[self setupMapView];
[self setupAppearance];
[self setupLocationManager];
}
- (void)setupAppearance {
//关闭侧滑返回
id traget = self.navigationController.interactivePopGestureRecognizer.delegate;
UIPanGestureRecognizer * pan = [[UIPanGestureRecognizer alloc]initWithTarget:traget action:nil];
[self.view addGestureRecognizer:pan];
//复位
UIButton * resetLoca = [UIButton buttonWithType:UIButtonTypeCustom];
[resetLoca setImage:[UIImage imageNamed:@"ic_greeting_checked"] forState:UIControlStateNormal];
[resetLoca addTarget:self action:@selector(resetLocation) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:resetLoca];
[resetLoca mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.mas_equalTo(CGSizeMake(30, 30));
make.right.bottom.equalTo(_mapView).offset(-30);
}];
}
- (void)setupMapView {
_mapView = [[BMKMapView alloc]initWithFrame:CGRectMake(0, JX_SCREEN_TOP, JX_SCREEN_WIDTH, JX_SCREEN_HEIGHT-JX_SCREEN_TOP)];
_mapView.delegate = self;
[_mapView setZoomLevel:17];// 将当前地图显示缩放等级设置为17级
//显示比例尺
// _mapView.showMapScaleBar = YES;
[self.view addSubview:_mapView];
//显示定位图层
_mapView.showsUserLocation = YES;
_mapView.userTrackingMode = BMKUserTrackingModeNone;//设定定位模式
//禁用平移手势
_mapView.scrollEnabled = YES;
//禁止双指拉伸缩放地图
_mapView.zoomEnabled = YES;
//禁止单指双击缩放地图
_mapView.zoomEnabledWithTap= YES;
//支持旋转
_mapView.rotateEnabled = YES;
//支持俯仰角
_mapView.overlookEnabled = YES;
}
- (void)setupLocationManager {
//开启定位服务
[self.locationManager startUpdatingLocation];
//设置显示定位图层
_mapView.showsUserLocation = YES;
//配置定位图层个性化样式,初始化BMKLocationViewDisplayParam的实例
BMKLocationViewDisplayParam *param = [[BMKLocationViewDisplayParam alloc] init];
//设置定位图标(屏幕坐标)X轴偏移量为0
param.locationViewOffsetX = 0;
//设置定位图标(屏幕坐标)Y轴偏移量为0
param.locationViewOffsetY = 0;
//设置定位图层locationView在最上层(也可设置为在下层)
param.locationViewHierarchy = LOCATION_VIEW_HIERARCHY_TOP;
//设置显示精度圈
param.isAccuracyCircleShow = YES;
//更新定位图层个性化样式
[_mapView updateLocationViewWithParam:param];
self.param = param;
}
#pragma mark - 定位当前位置方法
///连续定位回调函数
//- (void)BMKLocationManager:(BMKLocationManager *)manager didUpdateLocation:(BMKLocation *)location orError:(NSError *)error {
// if (error) {
// NSLog(@"locError:{%ld - %@};", (long)error.code, error.localizedDescription);
// }
// if (!location) {
// return;
// }
//
// self.userLocation.location = location.location;
//
// // 显示定位点 必须设置地图的中心点,地图才会切换显示到定位位置
// //_mapView.centerCoordinate = CLLocationCoordinate2DMake(24.496, 118.184);
// _mapView.centerCoordinate = self.userLocation.location.coordinate;
//
// //实现该方法,否则定位图标不出现
// [_mapView updateLocationData:self.userLocation];
//
//}
////用户位置更新后,会调用此函数
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation {
NSLog(@"当前位置%f,%f",userLocation.location.coordinate.latitude,userLocation.location.coordinate.longitude);
self.userLocation.location = userLocation.location;
// 显示定位点 必须设置地图的中心点,地图才会切换显示到定位位置
_mapView.centerCoordinate = userLocation.location.coordinate;
//设置缩放比例,不设置一次,测试第一次定位时中心总显示在天安门
_mapView.zoomLevel = 17;
[_mapView updateLocationData:userLocation];
}
#pragma mark - 响应事件
//复位按钮事件
-(void)resetLocation {
_mapView.centerCoordinate = self.userLocation.location.coordinate;
}
#pragma mark - Lazy loading
- (BMKLocationManager *)locationManager {
if (!_locationManager) {
//初始化BMKLocationManager(定位)的实例
_locationManager = [[BMKLocationManager alloc] init];
//设置BMKLocationService的代理
_locationManager.delegate = self;
//设定定位坐标系类型,默认为 BMKLocationCoordinateTypeGCJ02
_locationManager.coordinateType = BMKLocationCoordinateTypeBMK09LL;
//设定定位精度,默认为 kCLLocationAccuracyBest
_locationManager.desiredAccuracy = kCLLocationAccuracyBest;
//设定定位类型,默认为 CLActivityTypeAutomotiveNavigation
_locationManager.activityType = CLActivityTypeAutomotiveNavigation;
//指定定位是否会被系统自动暂停,默认为NO
_locationManager.pausesLocationUpdatesAutomatically = NO;
/**
是否允许后台定位,默认为NO。只在iOS 9.0及之后起作用。
设置为YES的时候必须保证 Background Modes 中的 Location updates 处于选中状态,否则会抛出异常。
由于iOS系统限制,需要在定位未开始之前或定位停止之后,修改该属性的值才会有效果。
*/
_locationManager.allowsBackgroundLocationUpdates = NO;
/**
指定单次定位超时时间,默认为10s,最小值是2s。注意单次定位请求前设置。
注意: 单次定位超时时间从确定了定位权限(非kCLAuthorizationStatusNotDetermined状态)
后开始计算。
*/
_locationManager.locationTimeout = 10;
//单次定位
WS(weakSelf);
[_locationManager requestLocationWithReGeocode:NO withNetworkState:NO completionBlock:^(BMKLocation * _Nullable location, BMKLocationNetworkState state, NSError * _Nullable error) {
weakSelf.userLocation.location = location.location;
// 显示定位点 必须设置地图的中心点,地图才会切换显示到定位位置
_mapView.centerCoordinate = weakSelf.userLocation.location.coordinate;
//实现该方法,否则定位图标不出现
[_mapView updateLocationData:weakSelf.userLocation];
}];
}
return _locationManager;
}
- (BMKUserLocation *)userLocation {
if (!_userLocation) {
//初始化BMKUserLocation的实例
_userLocation = [[BMKUserLocation alloc] init];
}
return _userLocation;
}
@end
网友评论