美文网首页
iOS百度地图 源码分享

iOS百度地图 源码分享

作者: 陈贺 | 来源:发表于2017-07-23 14:37 被阅读0次

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

注意:导入 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

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

相关文章

  • iOS百度地图 源码分享

    百度地图集成 ,划线定位 ,修改地图图标时时划线 ,源码分享 话不多说直接源码。 注意:导入 sdk,如有方法报错...

  • iOS集成百度地图实现在线建议地址查询

    今天和大家分享:集成百度地图iOS SDK,实现在线建议查询地址。这是我的github源码,使用oc纯代码编写,部...

  • RN-地图导航

    调起百度网页地图路径导航 调起高德网页地图路径导航 iOS调起百度APP地图路径导航 iOS调起高德app地图路径...

  • 地图集成调研

    地图集成调研 主要地图API有百度地图、高德地图、腾讯地图、搜狗地图(android、IOS暂不开放)。 百度地图...

  • 百度地图

    IOS第三方地图 百度地图高德地图腾讯地图苹果自带地图谷歌地图(中国屏蔽了) 接下来我们说一下百度地图百度地图SD...

  • 022-地图系列02-iOS百度地图版本及分类

    上一篇:021-地图系列01-iOS百度地图集成 一、iOS百度地图分类 按照大模块可以分为五大块 地图 定位 鹰...

  • 百度地图之官方demo的坑

    参考 iOS百度地图 Demo IOS SDK百度地图不能正常显示,只显示网格 刚下下来的是报错的,要执行以下几步...

  • 常用网址

    环信:http://www.easemob.com/customer/im IOS百度地图开发系列-百度地图不能正...

  • iOS百度地图使用时的注意问题

    iOS使用百度地图时会发现,API本身不带定位功能的,定位是通过iOS系统自身的定位实现的,百度地图API只是封装...

  • iOS百度地图反编码个人见解

    iOS百度地图反编码个人见解

网友评论

      本文标题:iOS百度地图 源码分享

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