iOS 百度地图

来源:互联网 发布:车辆数据采集系统 编辑:程序博客网 时间:2024/06/05 23:43
#import "ViewController.h"
//使用地图的全部功能
#import 
@interface ViewController ()<</span>BMKGeneralDelegate,BMKMapViewDelegate, BMKLocationServiceDelegate,BMKGeoCodeSearchDelegate, BMKRouteSearchDelegate>
//百度地图管理对象属性, 管理所有的百度地图服务
@property (nonatomic, strong)BMKMapManager *mapManager;
@property (nonatomic, strong)UITextField *startCityTF;
@property (nonatomic, strong)UITextField *endCityTF;
@property (nonatomic, strong)UITextField *endAddressTF;
@property (nonatomic, strong)UITextField *startAddressTF;
@property (nonatomic, strong)BMKMapView *mapView;
//百度地图定位
@property (nonatomic, strong)BMKLocationService*locationService;
//百度地图地理编码对象 (用于正向和反向编码)
@property (nonatomic, strong)BMKGeoCodeSearch *geoSeacher;
//开始节点
@property (nonatomic, strong)BMKPlanNode *startNode;
//目标节点
@property (nonatomic, strong)BMKPlanNode *endNode;
//路线查询对象
@property (nonatomic, strong)BMKRouteSearch *routSeacher;
@end

@implementation ViewController

- (
void)viewDidLoad {
    [
super viewDidLoad];
   
 self.edgesForExtendedLayout = UIRectEdgeNone;
   
 //在使用百度地图功能之前, 先启动百度地图服务
   
 self.mapManager = [[BMKMapManager alloc] init];
    [
_mapManager start:@"o7LPSCyWqQfrgO83LB3OxoLu"generalDelegate:self];
   
 //UI配置
    [
self addSubviews];
}
- (
void)addSubviews
{
   
 //  设置BarButtonItem
   
 UIBarButtonItem *left = [[UIBarButtonItem alloc]initWithTitle:@"开始定位" style:UIBarButtonItemStylePlaintarget:self action:@selector(leftAction)];
   
 self.navigationItem.leftBarButtonItem = left;
   
   
 //
   
 UIBarButtonItem *right = [[UIBarButtonItem alloc]initWithTitle:@"关闭定位" style:UIBarButtonItemStylePlaintarget:self action:@selector(rightAction)];
   
 self.navigationItem.rightBarButtonItem = right;
   
   
 self.startCityTF = [[UITextField alloc]initWithFrame:CGRectMake(20, 30, 100, 30)];
   
 self.startCityTF.text = @"开始城市";
    [
self.view addSubview:_startCityTF];
   
   
 self.startAddressTF = [[UITextField alloc]initWithFrame:CGRectMake(CGRectGetMaxX(_startCityTF.frame) +30, CGRectGetMinY(_startCityTF.frame),CGRectGetWidth(_startCityTF.frame),CGRectGetHeight(_startCityTF.frame))];
   
 self.startAddressTF.text = @"开始地址";
    [
self.view addSubview:_startAddressTF];
   
   
 self.endCityTF = [[UITextField alloc]initWithFrame:CGRectMake(CGRectGetMinX(_startCityTF.frame),CGRectGetMaxY(_startCityTF.frame) + 10,CGRectGetWidth(_startCityTF.frame),CGRectGetHeight(_startCityTF.frame))];
   
 self.endCityTF.text = @"目的城市";
    [
self.view addSubview:_endCityTF];
   
   
 //  目的地址
   
 self.endAddressTF = [[UITextField alloc]initWithFrame:CGRectMake(CGRectGetMaxX(_endCityTF.frame) + 30,CGRectGetMaxY(_startCityTF.frame) + 10,CGRectGetWidth(_startCityTF.frame),CGRectGetHeight(_startCityTF.frame))];
   
 self.endAddressTF.text = @"目的地址";
    [
self.view addSubview:_endAddressTF];
   
   
 //  添加路线规划按钮
   
 UIButton *routeSearch = [UIButtonbuttonWithType:UIButtonTypeSystem];
    [routeSearch
 setTitle:@"路线规划"forState:UIControlStateNormal];
    routeSearch.
frame =CGRectMake(CGRectGetMaxX(_startAddressTF.frame) + 10,CGRectGetMaxY(_startAddressTF.frame), 100, 30);
    [routeSearch
 setTitleColor:[UIColor blackColor]forState:UIControlStateNormal];
   
 //  设置点击事件
    [routeSearch
 addTarget:selfaction:@selector(routeSearchAction)forControlEvents:UIControlEventTouchUpInside];
     [
self.view addSubview:routeSearch];
   
 //  添加地图
   
 self.mapView = [[BMKMapView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(_endAddressTF.frame) + 5, [UIScreen mainScreen].bounds.size.width, [UIScreenmainScreen].bounds.size.height -CGRectGetMaxY(_endAddressTF.frame) - 5)];
   
   
 //  设置当前类为mapView的代理对象
   
 self.mapView.delegate = self;
   
   
 //  添加到父视图上
    [
self.view addSubview:_mapView];
   
 //1.定位 1.创建定位服务对象
   
 self.locationService = [[BMKLocationService alloc] init];
   
 //2.设置代理对象
   
 self.locationService.delegate = self;
   
 //3.设置最小的刷新距离
    [
BMKLocationService setLocationDistanceFilter:10];
   
 //地理编码
   
 self.geoSeacher = [[BMKGeoCodeSearch alloc] init];
   
 //指定代理
   
 self.geoSeacher.delegate = self;

   
 //创建路线检索对象
   
 self.routSeacher = [[BMKRouteSearch alloc] init];
   
 self.routSeacher.delegate = self;

}
//联网失败的回调方法
- (
void)onGetNetworkState:(int)iError{
   
 if (iError) {
       
 NSLog(@"联网失败");
    }
   
   
}
//获取授权失败的回调方法
- (
void)onGetPermissionState:(int)iError{
   
 if (iError) {
       
 NSLog(@"授权失败");
    }
   
   
}
//开始定位
- (
void)leftAction{
       [
self.locationService startUserLocationService];
   
 //2.在地图上显示用户位置
   
 self.mapView.showsUserLocation = YES;
   
   
}
//关闭定位
- (
void)rightAction{
    [
self.locationService stopUserLocationService];
   
 //
   
 self.mapView.showsUserLocation = NO;
   
 //移除大头针标注
   
 NSArray *annotations = self.mapView.annotations;
    [
self.mapView removeAnnotations:annotations];
   
}

#pragma  mark -- 定位方法的代理回调
//将要定位
- (
void)willStartLocatingUser{
   
}
//定位失败的回调方法
- (
void)didFailToLocateUserWithError:(NSError *)error{
   
 NSLog(@"----%@", error);
}
//定位成功的方法
- (
void)didUpdateBMKUserLocation:(BMKUserLocation*)userLocation{
   
 //
   
 
   
 //1.创建反向编码选项
   
 BMKReverseGeoCodeOption *option = [[BMKReverseGeoCodeOptionalloc] init];
   
 //2.给反向地理编码选项指定坐标点
    option.
reverseGeoPoint = userLocation.location.coordinate;
   
 //3.让地编码选项执行改选项
    [
self.geoSeacher reverseGeoCode:option];
    
}
#pragma mark -- 反向地理编码的回调方法
- (void)onGetReverseGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKReverseGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error{
   
 //添加大头针标注
   
 BMKPointAnnotation *annotation = [[BMKPointAnnotationalloc] init];
   
 //标注的位置
    annotation.
coordinate = result.location;
   
 //设置title. 点击大头针将来显示的内容
    annotation.
title = result.address;
   
 //添加标注
    [
self.mapView addAnnotation:annotation];
   
 //让地图显示到该区域
    [
self.mapView setCenterCoordinate:result.locationanimated:YES];
   
}
#pragma mark - 搜索路线的点击事件
- (void)routeSearchAction{
   
 //1.创建正向地理编码选项对象
   
 BMKGeoCodeSearchOption *option = [[BMKGeoCodeSearchOptionalloc] init];
   
 //2.给正向编码选项的cityaddress赋值(正向地理编码必须通过这两个属性)
    option.
city = self.startCityTF.text;
    option.
address = self.startAddressTF.text;
   
 //3.
    [
self.geoSeacher geoCode:option];
   
}

#pragma mark -- 正向地理编码的回调方法
- (void)onGetGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error{
   
 //1.
   
 if ([result.addressisEqualToString:self.startAddressTF.text]) {
       
 //说明当前编码的对象是起始的位置
       
 self.startNode = [[BMKPlanNode alloc] init];
//        // 1.方案一.路线规划的额时候, 通过城市名和地址名来确定路线节点的位置, 不太准确, 有时候失败
//        self.startNode.cityName = self.startCityTF.text;
       
       
 //2.方案二. 利用地理编码赋值
       
 self.startNode.pt = result.location;
       
       
 //正向编码目标节点
       
 ///1.
       
 BMKGeoCodeSearchOption *option = [[BMKGeoCodeSearchOption alloc] init];
       
 //2..
        option.
city = self.endCityTF.text;
        option.
address = self.endAddressTF.text;
       
 //3.
        [
_geoSeacher geoCode:option];
       
 _endNode = nil;
    }
else{
       
 self.endNode = [[BMKPlanNode alloc] init];
       
 _endNode.pt = result.location;
       
    }
   
 if (self.startNode != nil && _endNode != nil) {
       
 //开始路线检索
       
 //1.创建对应路线选项  ---   开车的路线
       
 BMKDrivingRoutePlanOption *drivingRoute = [[BMKDrivingRoutePlanOption  alloc] init];
       
 //2.告诉该选项起始位置和目标位置
        drivingRoute.
from = _startNode;
        drivingRoute.
to = _endNode;
       
 //3.执行该选项
        [
self.routSeacher drivingSearch:drivingRoute];
       
    }
   
   
}
#pragma mark -- 路线搜索回调
//驾车路线的回调方法
- (
void)onGetDrivingRouteResult:(BMKRouteSearch *)searcher result:(BMKDrivingRouteResult *)result errorCode:(BMKSearchErrorCode)error{
   
 if (error == BMK_SEARCH_NO_ERROR) {
       
 //1.选取一条路线(result.routes包含所有 的路线)
       
 BMKDrivingRouteLine *plan = [result.routesfirstObject];
       
 //获取路线中路段的总个数
       
 NSInteger size = [plan.steps count];
       
 //声明一个整型变量用来计算所有轨迹点的总数
       
 int planPointCount = 0;
       
 //获取每条路段中的轨迹点
       
 for (int i = 0; i < size; i++) {
           
 BMKDrivingStep *step = plan.steps[i];
           
 if (i == 0) {
               
 //让地图显示第一条路段的起始位置
                [
self.mapViewsetRegion:BMKCoordinateRegionMake(step.entrace.location,BMKCoordinateSpanMake(0.001, 0.001)) animated:YES];
            }
           
 //累计轨迹点
            planPointCount += step.
pointsCount;
        }
       
 //声明一个结构体数组, 用来保存每个路段中的轨迹点
       
 //C++的语法
       
 BMKMapPoint *tempPoints = newBMKMapPoint[planPointCount];
       
 int i = 0;
       
 //利用双重for循环把路段中的每个轨迹点放到结构体数组中
       
 for (int j = 0; j < size; j++) {
           
 BMKDrivingStep *transitStep = plan.steps[j];
           
 for (int k = 0; k < transitStep.pointsCount; k++) {
                tempPoints[i].
x = transitStep.points[k].x;
                tempPoints[i].
y = transitStep.points[k].y;
                i++;
             }
        }
       
 //让地图显示折线(可以理解为折现标注)
       
 BMKPolyline *line = [BMKPolylinepolylineWithPoints:tempPoints count:planPointCount];
       
 //告诉地图怎样显示
        [
self.mapView addOverlay:line];
    }
}
#pragma mark - 显示路线的回调方法
- (BMKOverlayView *)mapView:(BMKMapView *)mapView viewForOverlay:(id<</span>BMKOverlay>)overlay{
   
 //
   
 if ([overlay isKindOfClass:[BMKPolyline class]]) {
       
 //创建显示的折线
       
 BMKPolylineView *polyLineView = [[BMKPolylineViewalloc] initWithOverlay:overlay];
       
 //设置线条的颜色
        polyLineView.
strokeColor = [UIColor redColor];
       
 //设置线条的粗细
        polyLineView.
lineWidth = 3.0;
       
 return polyLineView;
    }
   
 return nil;
}

//步行方案路线
- (
void)onGetWalkingRouteResult:(BMKRouteSearch *)searcher result:(BMKWalkingRouteResult *)result errorCode:(BMKSearchErrorCode)error{
   
   
   
}
//公交路线方法
- (
void)onGetTransitRouteResult:(BMKRouteSearch *)searcher result:(BMKTransitRouteResult *)result errorCode:(BMKSearchErrorCode)error{
   
   
}


- (void)didReceiveMemoryWarning {
    [
super didReceiveMemoryWarning];
   
 // Dispose of any resources that can be recreated.
}

@end
0 0
原创粉丝点击