#import "JXActivityScopeVC.h"
#import <BaiduMapAPI_Map/BMKMapComponent.h>//引入地图功能所有的头文件
#import <BMKLocationKit/BMKLocationComponent.h>//引入定位功能所有的头文件
@interface JXActivityScopeVC ()<BMKMapViewDelegate,BMKLocationManagerDelegate>
@property (nonatomic, strong) BMKMapView *mapView;
@property (nonatomic, strong) BMKPolygon *polygon; //当前界面的多边形
@property (nonatomic, strong) BMKLocationManager *locationManager; //定位对象
@property (nonatomic, strong) BMKLocationViewDisplayParam *param; //定位图层自定义样式参数
@property (nonatomic, strong) BMKUserLocation *userLocation; //当前位置对象
@end
@implementation JXActivityScopeVC
-(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);
}];
UIButton *deleteButton = [UIButton buttonWithWithTitle:@"清除标记" titleColor:[UIColor whiteColor] font:SYSFONT(15) backgroundColor:CHAT_TITLE_GREEN cornerRadius:5 target:self action:@selector(deleteButtonResponseEvent)];
[self.view addSubview:deleteButton];
[deleteButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(_mapView.mas_bottom).offset(10);
make.size.mas_equalTo(CGSizeMake(JX_SCREEN_WIDTH-40, 40));
make.centerX.equalTo(self.view);
}];
}
- (void)setupMapView {
_mapView = [[BMKMapView alloc]initWithFrame:CGRectMake(0, JX_SCREEN_TOP+70, JX_SCREEN_WIDTH, JX_SCREEN_HEIGHT-JX_SCREEN_TOP-60-70)];
_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 = NO;
}
- (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 - BMKMapViewDelegate
/**
*根据overlay生成对应的View
*@param mapView 地图View
*@param overlay 指定的overlay
*@return 生成的覆盖物View
*/
- (BMKOverlayView *)mapView:(BMKMapView *)mapView viewForOverlay:(id <BMKOverlay>)overlay{
if ([overlay isKindOfClass:[BMKPolygon class]]) {
//初始化一个overlay并返回相应的BMKPolygonView的实例
BMKPolygonView* polygonView = [[BMKPolygonView alloc] initWithPolygon:overlay];
//设置polygonView的画笔(边框)颜色
polygonView.strokeColor = [[UIColor alloc] initWithRed:0 green:0 blue:0.5 alpha:1];
//设置polygonView的填充色
polygonView.fillColor = [[UIColor alloc] initWithRed:0 green:1 blue:1 alpha:0.5];
//设置polygonView的线宽度
polygonView.lineWidth = 2.0;
//设置polygonView为虚线样式
//lineDash属性已废弃,since5.0.0请使用lineDashType配置虚线样式
// polygonView.lineDash = YES;
polygonView.lineDashType = kBMKLineDashTypeSquare;
return polygonView;
}
return nil;
}
//2
#pragma mark - BMKLocationServiceDelegate 实现相关delegate 处理位置信息更新
//设置标注样式
-(BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id<BMKAnnotation>)annotation{
//大头针系统样式
if ([annotation isKindOfClass:[BMKPointAnnotation class]]) {
static NSString *pointReuseIndentifier = @"pointReuseIndentifier";
BMKPinAnnotationView*annotationView = (BMKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:pointReuseIndentifier];
if (annotationView == nil) {
annotationView = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:pointReuseIndentifier];
}
// annotationView.pinColor = BMKPinAnnotationColorPurple;
// annotationView.canShowCallout= YES; //设置气泡可以弹出,默认为NO
// annotationView.animatesDrop=YES; //设置标注动画显示,默认为NO
annotationView.draggable = YES; //设置标注可以拖动,默认为NO
// annotationView.image = [UIImage imageNamed:@"mine_map_starting"];//自定义标记
annotationView.image = [UIImage imageWithView:imgV];
return annotationView;
}
return nil;
}
#pragma mark - Responding events
/**
点击地图标注会回调此方法
@param mapView 地图View
@param mapPoi 返回点击地图地图坐标点的经纬度
*/
- (void)mapView:(BMKMapView *)mapView onClickedMapPoi:(BMKMapPoi *)mapPoi {
BMKPointAnnotation* annotation = [[BMKPointAnnotation alloc]init];
annotation.coordinate = CLLocationCoordinate2DMake(mapPoi.pt.latitude, mapPoi.pt.longitude);
[_mapView addAnnotation:annotation];
[self alertMessage];
}
/**
点击地图非标注区域会回调此方法
@param mapView 地图View
@param coordinate 返回点击地图非标注区域地图坐标点的经纬度
*/
- (void)mapView:(BMKMapView *)mapView onClickedMapBlank:(CLLocationCoordinate2D)coordinate {
BMKPointAnnotation* annotation = [[BMKPointAnnotation alloc]init];
annotation.coordinate = CLLocationCoordinate2DMake(coordinate.latitude, coordinate.longitude);
[_mapView addAnnotation:annotation];
[self alertMessage];
}
/**
*拖动annotation view时,若view的状态发生变化,会调用此函数。ios3.2以后支持
*@param mapView 地图View
*@param view annotation view
*@param newState 新状态
*@param oldState 旧状态
*/
- (void)mapView:(BMKMapView *)mapView annotationView:(BMKAnnotationView *)view didChangeDragState:(BMKAnnotationViewDragState)newState
fromOldState:(BMKAnnotationViewDragState)oldState {
[self alertMessage];
}
- (void)alertMessage {
NSArray *arrayA = [NSArray arrayWithArray:_mapView.overlays];
[_mapView removeOverlays:arrayA];
NSArray* array = [NSArray arrayWithArray:_mapView.annotations];
CLLocationCoordinate2D coords[100] = {0};
for (int i = 0; i < array.count; i++) {
BMKPointAnnotation* annotation = array[I];
coords[i] = annotation.coordinate;
}
_polygon = [BMKPolygon polygonWithCoordinates:coords count:array.count];
[_mapView addOverlay:self.polygon];
}
#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;
}
-(void)deleteButtonResponseEvent {
//百度地图删除地图上所有的标注和所有的覆盖物
NSArray* array = [NSArray arrayWithArray:_mapView.annotations];
[_mapView removeAnnotations:array];
array = [NSArray arrayWithArray:_mapView.overlays];
[_mapView removeOverlays:array];
}
#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
iOS 百度地图自定义多边形-覆盖物View
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 首先说下因为我从事的Android开发,百度地图jforAndroid在项目中接触过,没有细细研究过,一直用的高德...
- Android 高德地图上添加覆盖物最简单的是直接添加Marker AMap aMap =mapView.getM...
- 以下内容转载自多多洛爱学习的文章《JSAPI-在地图上添加自定义覆盖物》作者:多多洛爱学习链接:https://j...
- 由于项目需求,需要将高德地图api 中的,覆盖物,自定义。但是由于官方教程是由oc 语言编写,于是,不得不将oc ...