// // MapVC.m // jiaPei // // Created by apple on 16/2/18. // Copyright © 2016年 JCZ. All rights reserved. // #import "MapVC.h" #import //引入地图功能所有的头文件 #import //引入检索功能所有的头文件 #import //引入定位功能所有的头文件(新) @interface MapVC () { BMKMapView *_mapView; BMKLocationManager *_locService; BMKGeoCodeSearch *_searcher; CLLocation *meLocation; CLLocationCoordinate2D schLocation; } @property (nonatomic, strong) BMKUserLocation *userLocation;//当前位置对象 @end @implementation MapVC - (void)viewDidLoad { [super viewDidLoad]; self.title = @"地图"; self.view.backgroundColor = KBackGroundColor; [self goBackByNavigation]; [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 _searcher.delegate = nil; } -(void)myInit { _mapView = [[BMKMapView alloc]initWithFrame:[UIScreen mainScreen].bounds]; _mapView.mapType = BMKMapTypeStandard; //定位功能 _locService = [[BMKLocationManager alloc]init]; _locService.delegate = self; [_locService startUpdatingLocation]; //这个设置重复是为了消除一个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) { BMKPointAnnotation *annotation = [[BMKPointAnnotation alloc]init]; CLLocationCoordinate2D coor; coor.latitude = _lat; coor.longitude = _lng; annotation.coordinate = coor; annotation.title = _siteName; [_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]; } //NSLog(@"cityStr-->%@--addressStr-->%@",cityStr,addressStr); _searcher =[[BMKGeoCodeSearch alloc]init]; _searcher.delegate = self; BMKGeoCodeSearchOption *geoCodeSearchOption = [[BMKGeoCodeSearchOption alloc]init]; geoCodeSearchOption.city= cityStr; geoCodeSearchOption.address = addressStr; [_searcher geoCode:geoCodeSearchOption]; } } - (void)onGetGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKGeoCodeSearchResult *)result errorCode:(BMKSearchErrorCode)error { if (error == BMK_SEARCH_NO_ERROR) { //在此处理正常结果 BMKPointAnnotation* annotation = [[BMKPointAnnotation alloc]init]; ; annotation.coordinate = result.location; annotation.title = self.jxdz; [_mapView addAnnotation:annotation]; _mapView.centerCoordinate = result.location; schLocation = result.location; //[self getDistance]; } else { //如果没找到驾校位置 就找自己的位置 _mapView.userTrackingMode = BMKUserTrackingModeFollow; } } //标注的代理 - (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id)annotation { if ([annotation isKindOfClass:[BMKPointAnnotation class]]) { BMKPinAnnotationView *newAnnotationView = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"myAnnotation"]; newAnnotationView.pinColor = BMKPinAnnotationColorPurple; // 设置该标注点动画显示 newAnnotationView.animatesDrop = YES; return newAnnotationView; } return nil; } //处理位置坐标更新 - (void)BMKLocationManager:(BMKLocationManager *)manager didUpdateLocation:(BMKLocation *)location orError:(NSError *)error { self.userLocation.location = location.location; [_mapView updateLocationData:_userLocation]; meLocation = _userLocation.location; } //-(CGFloat)getDistance //{ // CLLocation* dist=[[CLLocation alloc] initWithLatitude:schLocation.latitude longitude:schLocation.longitude]; // CLLocationDistance kilometers=[meLocation distanceFromLocation:dist]/1000; // // NSLog(@"距离-->%f",kilometers); // return kilometers; //} - (BMKUserLocation *)userLocation { if (!_userLocation) { _userLocation = [[BMKUserLocation alloc] init]; } return _userLocation; } @end