项目中是在一个bgScrollview上放两个纵向滑动的scrollview,然后再在其上放地图,这样有很多个scrollview,主要解决的是手势冲突问题 导入相关头文件
#import <BaiduMapAPI_Base/BMKBaseComponent.h>//引入base相关所有的头文件
#import <BaiduMapAPI_Location/BMKLocationComponent.h>//引入定位功能所有的头文件
#import <BaiduMapAPI_Search/BMKSearchComponent.h>//引入检索功能所有的头文件
#import <baidumapapi_map/BMKPointAnnotation.h>
#import <BaiduMapAPI_Map/BMKMapView.h>
#import <BaiduMapAPI_Map/BMKPinAnnotationView.h>
复制代码
-(void)viewWillAppear:(BOOL)animated{
//先注册一下代理
_mapView.delegate = self;
}
复制代码
出去的时候记得置空
-(void)viewWillDisappear:(BOOL)animated{
_mapView.delegate = nil;
}
复制代码
- (void)viewDidLoad {
[super viewDidLoad];
_locService = [[BMKLocationService alloc]init];//定位功能的初始化
_locService.delegate = self;//设置代理位self
[_locService startUserLocationService];//启动定位服务
}
复制代码
先讲下思路,其实也很简单,我们啥也没干,百度提供了将地图点击坐标转化为地图的经纬度的方法,所以首先要放上一个手势 在创建UI的时候如下设置
_mapView = [[BMKMapView alloc] initWithFrame:Frame(0, CGRectGetMaxY(thrView.frame), IPHONE_WIDTH, 0.6*IPHONE_WIDTH)];
_mapView.zoomLevel = 16;
_mapView.userTrackingMode = BMKUserTrackingModeNone;//设置定位的状态为普通定位模式
_mapView.showsUserLocation = NO;
[_leftScroView addSubview:_mapView];
UITapGestureRecognizer *provinceMapTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(provinceMapTapped:)];
provinceMapTap.delegate = self;
[_mapView addGestureRecognizer:provinceMapTap];
/*下方这段是将单例类里的经纬度取出放一个大头针*/
BMKPointAnnotation *point1 = [[BMKPointAnnotation alloc]init];
VicSingleObject * single = [VicSingleObject getInstance];
CLLocationCoordinate2D coordinate;
coordinate.latitude = [single.userLat doubleValue];
coordinate.longitude = [single.userLong doubleValue];
point1.coordinate = coordinate;
[_mapView addAnnotation:point1];
复制代码
其实到这就结束了,进入最重要的一块代码:坐标转经纬度
-(void)provinceMapTapped:(UITapGestureRecognizer*)tap{
CGPoint point = [tap locationInView:tap.view];
VLog(@"%f",point.x);
CLLocationCoordinate2D coordinate =[_mapView convertPoint:point toCoordinateFromView:tap.view];
[_mapView removeAnnotations:_mapView.annotations];//移除之前添加的所有大头针
BMKPointAnnotation *point1 = [[BMKPointAnnotation alloc]init];
point1.coordinate = coordinate;
[_mapView addAnnotation:point1];//放置新大头针
[self getNewAddress:coordinate];//这行不用管
}
复制代码
#pragma mark 手势代理方法 ,判断触摸的是地图还是外层的view
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
//判断如果是百度地图的view 既可以实现手势拖动 scrollview 的滚动关闭
if ([gestureRecognizer.view isKindOfClass:[BMKMapView class]] ){
_leftScroView.scrollEnabled = NO;
return YES;
}else{
_leftScroView.scrollEnabled = YES;
return NO;
}
}
复制代码