iOS百度地图 源码分享

百度地图集成 ,划线定位 ,修改地图图标时时划线 ,源码分享 话不多说直接源码。

注意:导入 sdk,如有方法报错 注意inforplist 和 viewcontroller.mm
1.AppDelegate 添加头文件 定义属性

@interface AppDelegate (){
    BMKMapManager* _mapManager;
}

2.didFinishLaunchingWithOptions 方法里添加


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    _mapManager = [[BMKMapManager alloc]init];
    // 如果要关注网络及授权验证事件,请设定  generalDelegate参数
    BOOL ret = [_mapManager start:@"imlNRyuFnypuLPFe7WCAjnSPSLjC2EE1"  generalDelegate:nil];
    if (!ret) {
        NSLog(@"manager start failed!");
    }
    // Add the navigation controller's view to the window and display.
    [self.window makeKeyAndVisible];
    return YES;
}

viewController里添加

//  ViewController.m
//  baidumap
//  Created by 陈贺 on 16/8/26.
//  Copyright © 2016年 itcast. All rights reserved.
#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>
#import "CoorModel.h"
#define W [UIScreen mainScreen].bounds.size.width
#define H [UIScreen mainScreen].bounds.size.height
#import <BaiduMapAPI_Base/BMKBaseComponent.h>//引入base相关所有的头文件
#import <BaiduMapAPI_Map/BMKMapComponent.h>//引入地图功能所有的头文件
#import <BaiduMapAPI_Search/BMKSearchComponent.h>//引入检索功能所有的头文件
#import <BaiduMapAPI_Cloud/BMKCloudSearchComponent.h>//引入云检索功能所有的头文件
#import <BaiduMapAPI_Location/BMKLocationComponent.h>//引入定位功能所有的头文件
#import <BaiduMapAPI_Utils/BMKUtilsComponent.h>//引入计算工具所有的头文件
#import <BaiduMapAPI_Radar/BMKRadarComponent.h>//引入周边雷达功能所有的头文件
#import <BaiduMapAPI_Map/BMKMapView.h>//只引入所需的单个头文件
#import "CoorModel.h"
@interface ViewController ()<BMKPoiSearchDelegate,BMKGeoCodeSearchDelegate ,BMKMapViewDelegate,BMKLocationServiceDelegate,CLLocationManagerDelegate>
{
    BMKMapView *_mapView;
    BMKPoiSearch *_searcher;
    BMKLocationService *_locService;
    NSMutableArray *dataArr;
}
@property(nonatomic,strong)CLLocationManager *locationManager;

@end
@implementation ViewController
-(void)viewWillAppear:(BOOL)animated
{
    [_mapView viewWillAppear];
    _mapView.delegate = self; // 此处记得不用的时候需要置nil,否则影响内存的释放
    _locService.delegate = self;
    dataArr = [NSMutableArray array];
}
-(void)viewWillDisappear:(BOOL)animated
{
    [_mapView viewWillDisappear];
    _mapView.delegate = nil; //不用时,置nil
    _locService.delegate = nil;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    //创建地图
    _mapView = [BMKMapView new];
    // 主动请求权限
    //    重新设置百度图片的图标
    BMKLocationViewDisplayParam *displayParam = [BMKLocationViewDisplayParam new];
    displayParam.isRotateAngleValid =YES;
    displayParam.isAccuracyCircleShow =NO;
    displayParam.locationViewImgName = @"touxiang";
    displayParam.locationViewOffsetX =0;
    displayParam.locationViewOffsetY =0;
    [_mapView updateLocationViewWithParam:displayParam];//    BMKCoordinateRegion region ;//表示范围的结构体
//        region.center = coordinate;//中心点
//        region.span.latitudeDelta = 0.3;//经度范围(设置为0.1表示显示范围为0.2的纬度范围)
//        region.span.longitudeDelta = 0.3;//纬度范围
//        [_mapView setRegion:region animated:YES];
    //设置约束
    _mapView.frame=CGRectMake(0, 0, W, H);
    //切换为普通地图样式
//    _mapView.zoomLevel=11.0;
    _mapView.showMapScaleBar=YES;
//    _mapView.userTrackingMode = BMKUserTrackingModeFollow;

    _mapView.userTrackingMode=BMKUserTrackingModeFollow;

    [_mapView setMapType:BMKMapTypeStandard];
    
    _mapView.delegate=self;
    //显示定位图层
    _mapView.showsUserLocation = YES;
    //打开实时路况图层
    [_mapView setTrafficEnabled:YES];
    ///设定地图是否现显示3D楼块效果
    _mapView.buildingsEnabled=YES;
    //6. 设置地图显示的层级
    [_mapView setZoomLevel:12];
    //添加
    //    self.view =_mapView;
    [self.view addSubview:_mapView];
    //设置线路位置经纬度
    CLLocationCoordinate2D coords[5] = {0};
    //    39.864523,long 116.451585
    coords[0].latitude = 39.864523;
    coords[0].longitude = 116.451585;
    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.554;
    coords[4].latitude = 39.965;
    coords[4].longitude = 116.604;
    //构建分段颜色索引数组
    NSArray *colorIndexs = [NSArray arrayWithObjects:
                            [NSNumber numberWithInt:2],
                            [NSNumber numberWithInt:0],
                            [NSNumber numberWithInt:1],
                            [NSNumber numberWithInt:2], nil];
    //构建BMKPolyline,使用分段颜色索引,其对应的BMKPolylineView必须设置colors属性
    BMKPolyline *colorfulPolyline = [BMKPolyline polylineWithCoordinates:coords count:5 textureIndex:colorIndexs];
    [_mapView addOverlay:colorfulPolyline];
    //初始化BMKLocationService
    if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
        _locationManager = [[CLLocationManager alloc] init];
        //获取授权验证
        [_locationManager requestAlwaysAuthorization];
        [_locationManager requestWhenInUseAuthorization];
    }
    _locService = [[BMKLocationService alloc] init];
    _locService.delegate = self;
    [_locService startUserLocationService];
    _mapView.showsUserLocation = NO;
    _mapView.userTrackingMode = BMKUserTrackingModeFollow;
    _mapView.showsUserLocation = YES;
//        _mapView.userTrackingMode = BMKUserTrackingModeFollowWithHeading;
    //添加大头针经纬度
    CLLocationCoordinate2D coordss[5] = {0};
    //循环添加大头针    //    39.864523,long 116.451585
    coordss[0].latitude = 39.864523;
    coordss[0].longitude = 116.451585;
    coordss[1].latitude = 39.925;
    coordss[1].longitude = 116.454;
    coordss[2].latitude = 39.955;
    coordss[2].longitude = 116.494;
    coordss[3].latitude = 39.905;
    coordss[3].longitude = 116.554;
    coordss[4].latitude = 39.965;
    coordss[4].longitude = 116.604;
    for (int i=0; i<5; i++) {
        BMKPointAnnotation* annotation = [[BMKPointAnnotation alloc]init];
        annotation.coordinate = coordss[i];
        annotation.title = @"这里是北京";
        [_mapView addAnnotation:annotation];
    }
    //定位
    _locService = [[BMKLocationService alloc]init];
    //设置代理
    _locService.delegate = self;
    //启动LocationService
    [_locService startUserLocationService];
}

//自定义大头针
- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id <BMKAnnotation>)annotation
{   BMKAnnotationView *annotationView=[[BMKAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@"myAnnotation"];
    annotationView.image =[UIImage imageNamed:@"canyin"];
    //自定义内容气泡
    UIView *areaPaoView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
    areaPaoView.layer.cornerRadius=8;
    areaPaoView.layer.masksToBounds=YES;
    areaPaoView.layer.contents =(id)[UIImage imageNamed:@" "].CGImage;//这张图片是做好的透明
    areaPaoView.backgroundColor=[UIColor whiteColor];
//    if ([annotation.title isEqualToString:@"1"]) { //假设title的标题为1,那么就把添加上这个自定义气泡内容
        UILabel * labelNo = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, 200, 30)];
        labelNo.text =[NSString stringWithFormat:@"站点编号:%@",@"faf"];
        labelNo.textColor = [UIColor blackColor];
        labelNo.backgroundColor = [UIColor clearColor];
        [areaPaoView addSubview:labelNo];
        UILabel * labelStationName = [[UILabel alloc]initWithFrame:CGRectMake(10, 20, 200, 30)];
        labelStationName.text = [NSString stringWithFormat:@"站点名称:昆山中学"];
        labelStationName.textColor = [UIColor blackColor];
        labelStationName.backgroundColor = [UIColor clearColor];
        [areaPaoView addSubview:labelStationName];
        UILabel * labelSumNum = [[UILabel alloc]initWithFrame:CGRectMake(10, 40, 200, 30)];
        labelSumNum.text = [NSString stringWithFormat:@"总桩数:30"];
        labelSumNum.textColor = [UIColor blackColor];
        labelSumNum.backgroundColor = [UIColor clearColor];
        [areaPaoView addSubview:labelSumNum];
        UILabel * labelBicycleNum = [[UILabel alloc]initWithFrame:CGRectMake(10, 60, 200, 30)];
        labelBicycleNum.text = [NSString stringWithFormat:@"可借车:20"];
        labelBicycleNum.textColor = [UIColor blackColor];
        labelBicycleNum.backgroundColor = [UIColor clearColor];
        [areaPaoView addSubview:labelBicycleNum];
//    }
    BMKActionPaopaoView *paopao=[[BMKActionPaopaoView alloc]initWithCustomView:areaPaoView];
    annotationView.paopaoView=paopao;
    return annotationView;
}
//根据overlay生成对应的View
- (BMKOverlayView *)mapView:(BMKMapView *)mapView viewForOverlay:(id <BMKOverlay>)overlay
{
    BMKPolylineView * polyLineView = [[BMKPolylineView alloc] initWithOverlay:overlay];
    polyLineView.lineWidth = 5;
    /// 使用分段颜色绘制时,必须设置(内容必须为UIColor)
    polyLineView.colors = [NSArray arrayWithObjects:[UIColor colorWithRed:0.42 green:0.42 blue:0.42 alpha:1.00], nil];
    if (dataArr.count>2) {
        polyLineView.strokeColor = [ [UIColor colorWithRed:0.32 green:0.69 blue:0.75 alpha:1.00] colorWithAlphaComponent:1];
        polyLineView.lineWidth = 5.0;
    }
    return polyLineView;
    return nil;
}
//处理位置坐标更新
- (void)didUpdateUserHeading:(BMKUserLocation *)userLocation
{
    [_mapView updateLocationData:userLocation];
}
//处理位置坐标更新
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation
{
    [_mapView updateLocationData:userLocation];
    //获取中心位置,设置居中
    CLLocationCoordinate2D coordss;
    coordss.latitude = 39.905;
    coordss.longitude = 116.554;
//    _mapView.centerCoordinate = coordss;//移动到中心点
    CLLocationCoordinate2D pt = CLLocationCoordinate2DMake(userLocation.location.coordinate.latitude, userLocation.location.coordinate.longitude);
    //    _mapView.centerCoordinate = pt;//移动到中心点
    CoorModel *model = [[CoorModel alloc] init];
    model.latitude = pt.latitude;
    model.longitude = pt.longitude;
    [dataArr addObject:model];
    [self makeline];
}
//地图划线的方法
-(void)makeline{
    if ( dataArr.count < 2 ) {
        return;
    }
    BMKMapPoint *pointarr = new BMKMapPoint[dataArr.count];
    for (int i = 0; i < dataArr.count; i++) {
        CoorModel *model = dataArr[i];
        CLLocationCoordinate2D pt = CLLocationCoordinate2DMake(model.latitude, model.longitude);
        BMKMapPoint point = BMKMapPointForCoordinate(pt);
        pointarr[i] = point;
    }
    BMKPolyline *polyline;
    if (polyline) {
        [_mapView removeOverlay:polyline];
    }
    polyline = [BMKPolyline polylineWithPoints:pointarr count:dataArr.count];
    if (nil != polyline) {
        [_mapView addOverlay:polyline];
    }
    delete [] pointarr;
}

@end

个人总结 依据百度地图官方文档所做。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 199,636评论 5 468
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 83,890评论 2 376
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 146,680评论 0 330
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,766评论 1 271
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,665评论 5 359
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,045评论 1 276
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,515评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,182评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,334评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,274评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,319评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,002评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,599评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,675评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,917评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,309评论 2 345
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,885评论 2 341

推荐阅读更多精彩内容