// // MapVC.m // jiaPei // // Created by apple on 16/2/18. // Copyright © 2016年 JCZ. All rights reserved. // #import "MapVC.h" #import "NeighbourBMKPointAnnotation.h" #import "NeighbourBMKAnnotationView.h" #import //引入地图功能所有的头文件 #import //引入检索功能所有的头文件 #import //引入定位功能所有的头文件(新) #import @interface MapVC () { BMKMapView *_mapView; CLLocation *meLocation; CLLocationCoordinate2D schLocation; } @property (nonatomic, strong) BMKUserLocation *userLocation; @end @implementation MapVC - (void)viewDidLoad { [super viewDidLoad]; self.title = @"地图"; self.view.backgroundColor = backGroundColor; [self configNavigationBar]; [self myInit]; } -(void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [_mapView viewWillAppear]; _mapView.delegate = self; // 此处记得不用的时候需要置nil,否则影响内存的释放 } -(void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; [_mapView viewWillDisappear]; _mapView.delegate = nil; // 不用时,置nil } -(void)myInit { _mapView = [[BMKMapView alloc]initWithFrame:[UIScreen mainScreen].bounds]; _mapView.mapType = BMKMapTypeStandard; //定位功能 [[MapManager sharedManager] updateLocationWithCompleteBlock:^(BOOL success, CLLocation * _Nonnull location) { if (success) { self.userLocation.location = location; [self->_mapView updateLocationData:self->_userLocation]; self->meLocation = location; } }]; //这个设置重复是为了消除一个bug 记得 _mapView.showsUserLocation = NO; /* BMKUserTrackingModeNone 普通定位模式 BMKUserTrackingModeFollow 定位跟随模式 BMKUserTrackingModeFollowWithHeading 定位罗盘模式 */ _mapView.userTrackingMode = BMKUserTrackingModeNone; _mapView.zoomLevel = 17; _mapView.showsUserLocation = YES; BMKLocationViewDisplayParam *displayParam = [[BMKLocationViewDisplayParam alloc]init]; displayParam.isRotateAngleValid = true;//跟随态旋转角度是否生效 displayParam.isAccuracyCircleShow = false;//精度圈是否显示 //displayParam.locationViewImgName= @"icon";//定位图标名称 displayParam.locationViewOffsetX = 0;//定位偏移量(经度) displayParam.locationViewOffsetY = 0;//定位偏移量(纬度) [_mapView updateLocationViewWithParam:displayParam]; [self.view addSubview:_mapView]; if (_isJustLoad) { NeighbourBMKPointAnnotation *annotation = [[NeighbourBMKPointAnnotation alloc]init]; CLLocationCoordinate2D coor; coor.latitude = _lat; coor.longitude = _lng; annotation.coordinate = coor; annotation.title = _siteName; _mapView.delegate = self; [_mapView addAnnotation:annotation]; _mapView.centerCoordinate = coor; [_mapView selectAnnotation:annotation animated:NO]; return; } if (!self.jxdz) { //如果驾校没地址就显示自己位置 _mapView.userTrackingMode = BMKUserTrackingModeFollow; return; } //驾校位置地理反编码 if (self.jxdz.length > 0) { NSMutableString* mstr = [NSMutableString stringWithString:self.jxdz]; NSRange substr = [mstr rangeOfString:@"省"]; NSRange substr2 = [mstr rangeOfString:@"市"]; NSString *cityStr,*addressStr; if (substr2.location == NSNotFound) { cityStr = @""; addressStr = mstr; } else { if (substr.location == NSNotFound) { cityStr = [mstr substringWithRange:NSMakeRange(0, substr2.location + 1)]; } else { cityStr = [mstr substringWithRange:NSMakeRange(substr.location + substr.length, substr2.location - substr.location - substr.length + 1)]; } addressStr = [mstr substringFromIndex:substr2.location + 1]; } [[MapManager sharedManager] onGetGeoCodeWithAddress:addressStr City:cityStr completeBlock:^(BOOL success, BMKGeoCodeSearchResult * _Nonnull result, BMKSearchErrorCode error) { if (success) { if (error == BMK_SEARCH_NO_ERROR) { //在此处理正常结果 NeighbourBMKPointAnnotation* annotation = [[NeighbourBMKPointAnnotation alloc]init]; ; annotation.coordinate = result.location; annotation.title = self.jxdz; [self->_mapView addAnnotation:annotation]; self->_mapView.centerCoordinate = result.location; self->schLocation = result.location; //[self getDistance]; } else { //如果没找到驾校位置 就找自己的位置 self->_mapView.userTrackingMode = BMKUserTrackingModeFollow; } } }]; } } //标注的代理 -(BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id)annotation { if ([annotation isKindOfClass:[NeighbourBMKPointAnnotation class]]) { NeighbourBMKAnnotationView *view = [NeighbourBMKAnnotationView annotationViewWithMapView:mapView Annotation:(BMKPointAnnotation *)annotation]; view.size = 1; view.block = ^(NeighbourBMKPointAnnotation *clickedAnnotation) { BMKOpenDrivingRouteOption *opt = [[BMKOpenDrivingRouteOption alloc] init]; opt.appScheme = @"baidumapsdk://mapsdk.baidu.com"; opt.isSupportWeb = YES;//调起百度地图客户端失败后,是否支持调起web地图,默认:YES //指定起点 BMKPlanNode *start = [[BMKPlanNode alloc]init]; start.pt = self->meLocation.coordinate; opt.startPoint = start; //指定终点名称 BMKPlanNode *end = [[BMKPlanNode alloc]init]; end.pt = CLLocationCoordinate2DMake(clickedAnnotation.coordinate.latitude, clickedAnnotation.coordinate.longitude); opt.endPoint = end; BMKOpenErrorCode code = [BMKOpenRoute openBaiduMapDrivingRoute:opt]; if (code == 1 || code == 0) { }else{ ShowMsg(@"打开百度地图失败"); } }; return view; } return nil; } //-(CGFloat)getDistance //{ // CLLocation* dist=[[CLLocation alloc] initWithLatitude:schLocation.latitude longitude:schLocation.longitude]; // CLLocationDistance kilometers=[meLocation distanceFromLocation:dist]/1000; // // NSLog(@"距离-->%f",kilometers); // return kilometers; //} - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (BMKUserLocation *)userLocation { if (!_userLocation) { _userLocation = [[BMKUserLocation alloc] init]; } return _userLocation; } @end