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

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

推荐阅读更多精彩内容

  • 各位小伙伴们大家好,今天我向大家介绍一下苹果百度地图的使用方法,因为做过一些想关的APP,感觉百度地图还是挺方便的...
    Lee0528阅读 12,082评论 18 46
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 160,183评论 24 690
  • 基础地图 开发者可利用SDK提供的接口,使用百度提供的基础地图数据。目前百度地图SDK所提供的地图等级为19级,所...
    DestinyFighter_阅读 5,219评论 6 15
  • 一.进入百度地图官方(注册应用) 1.1.百度地图官方 1.2.创建应用FBABCE88-01DC-4183-A8...
    IIronMan阅读 2,246评论 17 69
  • 神! 家里这些糟心的事情让你给我一起面对,我只想说有你真好。今天早上你和我说,瑜伽怎么办。这几乎是我最渴望的事...
    曾曾的麻麻阅读 83评论 0 0
  • 最近一直围绕的都是这个问题,好烦啊,我都不知道怎么说好了!
    瞬间灿烂阅读 82评论 0 0
  • 终于熬到了周末,筹备了一个多周的垂钓计划,终于要付诸实施了。早上起得很早,比平时上班起得还早。应该是太过兴奋的缘故...
    莽原奔马668阅读 211评论 0 3
  • 第二遍用老师讲的定位方法来画,有点感觉了,要麻烦老师再帮忙看下。不过还是找不到线条流畅的感觉。
    薇薇薇薇薇薇薇阅读 50评论 0 0