美文网首页
百度地图绘制线

百度地图绘制线

作者: CNPM | 来源:发表于2019-04-13 15:25 被阅读0次

步骤:

1.在Xcode中新建工程,通过Pods集成百度地图SDK
2.在AppDelegate.m文件中

引用头文件

#import <BaiduMapAPI_Base/BMKBaseComponent.h>//引入base相关所有的头文件

初始化BMKMapManager
在您的AppDelegate.m文件中添加对BMKMapManager的初始化,并填入您申请的授权AK(请确保此处AK与当前工程Bundle identifier匹配,否则会出现地图数据无法加载,只有方格的情况),示例如下:

- (BOOL)application:(UIApplication *)application   
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {       
    // 要使用百度地图,请先启动BaiduMapManager  
    BMKMapManager *mapManager = [[BMKMapManager alloc] init];   
    // 如果要关注网络及授权验证事件,请设定generalDelegate参数
    BOOL ret = [_mapManager start:@"在此处输入您的授权AK"  generalDelegate:nil];  
    if (!ret) {  
        NSLog(@"manager start failed!");  
    }  
    /**
     全局设置地图SDK与开发者交互时的坐标类型。不调用此方法时,
 
     设置此坐标类型意味着2个方面的约定:
     1. 地图SDK认为开发者传入的所有坐标均为此类型;
     2. 所有地图SDK返回给开发者的坐标均为此类型;

     地图SDK默认使用BD09LL(BMK_COORDTYPE_BD09LL)坐标。
     如需使用GCJ02坐标,传入参数值为BMK_COORDTYPE_COMMON即可。ß
     本方法不支持传入WGS84(BMK_COORDTYPE_GPS)坐标。
 
     @param coorType 地图SDK全局使用的坐标类型
     @return 设置成功返回YES,设置失败返回False
     */ 
    [self.window addSubview:navigationController.view];  
    [self.window makeKeyAndVisible];  
    return YES;  
}
2.在新创建的ViewController.m文件中

引用头文件

#import <BaiduMapAPI_Base/BMKBaseComponent.h>//引入base相关所有的头文件
#import <BaiduMapAPI_Map/BMKMapComponent.h>//引入地图功能所有的头文件

ViewController.m代码如下:

#import "ViewController.h"
#import <BaiduMapAPI_Base/BMKBaseComponent.h>//引入base相关所有的头文件
#import <BaiduMapAPI_Map/BMKMapComponent.h>//引入地图功能所有的头文件

@interface ViewController ()<BMKMapViewDelegate>      //需要添加代理协议
@property (nonatomic, strong) BMKMapView *mapView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //初始化并实现代理及其相关设置
    self.mapView = [[BMKMapView alloc] initWithFrame:self.view.bounds];
    self.mapView.delegate = self;
    [self.mapView setZoomLevel:17]; //缩放级别
    [self.mapView setShowMapScaleBar:YES];  //比例尺
    [self.mapView setMapType:BMKMapTypeStandard];   //地图类型(标准地图(包含3D地图):BMKMapTypeStandard; 卫星地图:BMKMapTypeSatellite; 空白地图:BMKMapTypeNone )
//    [self.mapView setTrafficEnabled:YES];   //打开实时路况图层
//    [self.mapView setShowsUserLocation:YES];   //显示定位图层
    [self.view addSubview:self.mapView];

    [self drawLineTexture];     //绘制带纹理的线
}

//注意:BMKMapView使用viewWillAppear、viewWillDisappear方法来控制BMKMapView的生命周期,并且在一个时刻只能有一个BMKMapView接受回调消息,因此在使用BMKMapView的viewController中必须需要在viewWillAppear、viewWillDisappear方法中调用BMKMapView的对应的方法。

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.mapView viewWillAppear];          //需要实现的方法
    
}
-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self.mapView viewWillDisappear];       //需要实现的方法
}

- (void)drawLineTexture{
    //构建顶点数组
    CLLocationCoordinate2D coords[5] = {0};
    coords[0].latitude = 39.965;
    coords[0].longitude = 116.404;
    coords[1].latitude = 39.925;
    coords[1].longitude = 116.454;
    coords[2].latitude = 39.955;
    coords[2].longitude = 116.494;
    coords[3].latitude = 39.905;
    coords[3].longitude = 116.654;
    coords[4].latitude = 39.965;
    coords[4].longitude = 116.704;
    //构建分段纹理索引数组
    NSArray *textureIndex = [NSArray arrayWithObjects:
                             [NSNumber numberWithInt:0],
                             [NSNumber numberWithInt:0],
                             [NSNumber numberWithInt:0],
                             [NSNumber numberWithInt:0], nil];
    
    //构建BMKPolyline,使用分段纹理
    BMKPolyline *polyLine = [BMKPolyline polylineWithCoordinates:coords count:5 textureIndex:textureIndex];
    //添加分段纹理绘制折线覆盖物
    [_mapView addOverlay:polyLine];
}

//绘制线代理方法
- (BMKOverlayView *)mapView:(BMKMapView *)mapView viewForOverlay:(id <BMKOverlay>)overlay{

    //绘制带纹理的线的设置
    
    if ([overlay isKindOfClass:[BMKPolyline class]]) {
        BMKPolylineView *polylineView = [[BMKPolylineView alloc] initWithOverlay:overlay];
        polylineView.lineWidth = 5;
        polylineView.isFocus = YES;// 是否分段纹理绘制(突出显示),默认YES
        //加载分段纹理图片,必须否则不能进行分段纹理绘制
        [polylineView loadStrokeTextureImages:
         [NSArray arrayWithObjects:[UIImage imageNamed:@"road_blue_arrow.png"],nil]];
        return polylineView;
    }
    return nil;
}

@end

相关文章

网友评论

      本文标题:百度地图绘制线

      本文链接:https://www.haomeiwen.com/subject/uazlwqtx.html