iOS百度地圖 源碼分享

百度地圖集成 ,劃線定位 ,修改地圖圖標(biāo)時時劃線 害幅,源碼分享 話不多說直接源碼刨秆。

注意:導(dǎo)入 sdk,如有方法報(bào)錯 注意inforplist 和 viewcontroller.mm
1.AppDelegate 添加頭文件 定義屬性

@interface AppDelegate (){
    BMKMapManager* _mapManager;
}

2.didFinishLaunchingWithOptions 方法里添加


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    _mapManager = [[BMKMapManager alloc]init];
    // 如果要關(guān)注網(wǎng)絡(luò)及授權(quán)驗(yàn)證事件,請?jiān)O(shè)定  generalDelegate參數(shù)
    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相關(guān)所有的頭文件
#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>//引入計(jì)算工具所有的頭文件
#import <BaiduMapAPI_Radar/BMKRadarComponent.h>//引入周邊雷達(dá)功能所有的頭文件
#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酌毡,否則影響內(nèi)存的釋放
    _locService.delegate = self;
    dataArr = [NSMutableArray array];
}
-(void)viewWillDisappear:(BOOL)animated
{
    [_mapView viewWillDisappear];
    _mapView.delegate = nil; //不用時,置nil
    _locService.delegate = nil;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    //創(chuàng)建地圖
    _mapView = [BMKMapView new];
    // 主動請求權(quán)限
    //    重新設(shè)置百度圖片的圖標(biāo)
    BMKLocationViewDisplayParam *displayParam = [BMKLocationViewDisplayParam new];
    displayParam.isRotateAngleValid =YES;
    displayParam.isAccuracyCircleShow =NO;
    displayParam.locationViewImgName = @"touxiang";
    displayParam.locationViewOffsetX =0;
    displayParam.locationViewOffsetY =0;
    [_mapView updateLocationViewWithParam:displayParam];//    BMKCoordinateRegion region ;//表示范圍的結(jié)構(gòu)體
//        region.center = coordinate;//中心點(diǎn)
//        region.span.latitudeDelta = 0.3;//經(jīng)度范圍(設(shè)置為0.1表示顯示范圍為0.2的緯度范圍)
//        region.span.longitudeDelta = 0.3;//緯度范圍
//        [_mapView setRegion:region animated:YES];
    //設(shè)置約束
    _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;
    //打開實(shí)時路況圖層
    [_mapView setTrafficEnabled:YES];
    ///設(shè)定地圖是否現(xiàn)顯示3D樓塊效果
    _mapView.buildingsEnabled=YES;
    //6. 設(shè)置地圖顯示的層級
    [_mapView setZoomLevel:12];
    //添加
    //    self.view =_mapView;
    [self.view addSubview:_mapView];
    //設(shè)置線路位置經(jīng)緯度
    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;
    //構(gòu)建分段顏色索引數(shù)組
    NSArray *colorIndexs = [NSArray arrayWithObjects:
                            [NSNumber numberWithInt:2],
                            [NSNumber numberWithInt:0],
                            [NSNumber numberWithInt:1],
                            [NSNumber numberWithInt:2], nil];
    //構(gòu)建BMKPolyline,使用分段顏色索引,其對應(yīng)的BMKPolylineView必須設(shè)置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];
        //獲取授權(quán)驗(yàn)證
        [_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;
    //添加大頭針經(jīng)緯度
    CLLocationCoordinate2D coordss[5] = {0};
    //循環(huán)添加大頭針    //    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];
    //設(shè)置代理
    _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"];
    //自定義內(nèi)容氣泡
    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"]) { //假設(shè)title的標(biāo)題為1告材,那么就把添加上這個自定義氣泡內(nèi)容
        UILabel * labelNo = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, 200, 30)];
        labelNo.text =[NSString stringWithFormat:@"站點(diǎn)編號:%@",@"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:@"站點(diǎn)名稱:昆山中學(xué)"];
        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:@"總樁數(shù):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;
}
//根據(jù)overlay生成對應(yīng)的View
- (BMKOverlayView *)mapView:(BMKMapView *)mapView viewForOverlay:(id <BMKOverlay>)overlay
{
    BMKPolylineView * polyLineView = [[BMKPolylineView alloc] initWithOverlay:overlay];
    polyLineView.lineWidth = 5;
    /// 使用分段顏色繪制時,必須設(shè)置(內(nèi)容必須為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;
}
//處理位置坐標(biāo)更新
- (void)didUpdateUserHeading:(BMKUserLocation *)userLocation
{
    [_mapView updateLocationData:userLocation];
}
//處理位置坐標(biāo)更新
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation
{
    [_mapView updateLocationData:userLocation];
    //獲取中心位置古劲,設(shè)置居中
    CLLocationCoordinate2D coordss;
    coordss.latitude = 39.905;
    coordss.longitude = 116.554;
//    _mapView.centerCoordinate = coordss;//移動到中心點(diǎn)
    CLLocationCoordinate2D pt = CLLocationCoordinate2DMake(userLocation.location.coordinate.latitude, userLocation.location.coordinate.longitude);
    //    _mapView.centerCoordinate = pt;//移動到中心點(diǎn)
    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

個人總結(jié) 依據(jù)百度地圖官方文檔所做斥赋。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市产艾,隨后出現(xiàn)的幾起案子疤剑,更是在濱河造成了極大的恐慌,老刑警劉巖闷堡,帶你破解...
    沈念sama閱讀 216,372評論 6 498
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件隘膘,死亡現(xiàn)場離奇詭異,居然都是意外死亡缚窿,警方通過查閱死者的電腦和手機(jī)棘幸,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,368評論 3 392
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來倦零,“玉大人误续,你說我怎么就攤上這事∩” “怎么了蹋嵌?”我有些...
    開封第一講書人閱讀 162,415評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長葫隙。 經(jīng)常有香客問我栽烂,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,157評論 1 292
  • 正文 為了忘掉前任腺办,我火速辦了婚禮焰手,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘怀喉。我一直安慰自己书妻,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,171評論 6 388
  • 文/花漫 我一把揭開白布躬拢。 她就那樣靜靜地躺著躲履,像睡著了一般。 火紅的嫁衣襯著肌膚如雪聊闯。 梳的紋絲不亂的頭發(fā)上工猜,一...
    開封第一講書人閱讀 51,125評論 1 297
  • 那天,我揣著相機(jī)與錄音菱蔬,去河邊找鬼篷帅。 笑死,一個胖子當(dāng)著我的面吹牛汗销,可吹牛的內(nèi)容都是我干的犹褒。 我是一名探鬼主播,決...
    沈念sama閱讀 40,028評論 3 417
  • 文/蒼蘭香墨 我猛地睜開眼弛针,長吁一口氣:“原來是場噩夢啊……” “哼叠骑!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起削茁,我...
    開封第一講書人閱讀 38,887評論 0 274
  • 序言:老撾萬榮一對情侶失蹤宙枷,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后茧跋,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體慰丛,經(jīng)...
    沈念sama閱讀 45,310評論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,533評論 2 332
  • 正文 我和宋清朗相戀三年瘾杭,在試婚紗的時候發(fā)現(xiàn)自己被綠了诅病。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,690評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡粥烁,死狀恐怖贤笆,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情讨阻,我是刑警寧澤芥永,帶...
    沈念sama閱讀 35,411評論 5 343
  • 正文 年R本政府宣布,位于F島的核電站钝吮,受9級特大地震影響埋涧,放射性物質(zhì)發(fā)生泄漏板辽。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,004評論 3 325
  • 文/蒙蒙 一棘催、第九天 我趴在偏房一處隱蔽的房頂上張望劲弦。 院中可真熱鬧,春花似錦巧鸭、人聲如沸瓶您。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,659評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至贸毕,卻和暖如春郑叠,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背明棍。 一陣腳步聲響...
    開封第一講書人閱讀 32,812評論 1 268
  • 我被黑心中介騙來泰國打工乡革, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人摊腋。 一個月前我還...
    沈念sama閱讀 47,693評論 2 368
  • 正文 我出身青樓沸版,卻偏偏與公主長得像,于是被迫代替她去往敵國和親兴蒸。 傳聞我的和親對象是個殘疾皇子视粮,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,577評論 2 353

推薦閱讀更多精彩內(nèi)容

  • 各位小伙伴們大家好,今天我向大家介紹一下蘋果百度地圖的使用方法橙凳,因?yàn)樽鲞^一些想關(guān)的APP蕾殴,感覺百度地圖還是挺方便的...
    Lee0528閱讀 14,679評論 18 46
  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,071評論 25 707
  • 基礎(chǔ)地圖 開發(fā)者可利用SDK提供的接口,使用百度提供的基礎(chǔ)地圖數(shù)據(jù)岛啸。目前百度地圖SDK所提供的地圖等級為19級钓觉,所...
    DestinyFighter_閱讀 5,801評論 6 15
  • 一.進(jìn)入百度地圖官方(注冊應(yīng)用) 1.1.百度地圖官方 1.2.創(chuàng)建應(yīng)用FBABCE88-01DC-4183-A8...
    IIronMan閱讀 2,705評論 17 69
  • 神! 家里這些糟心的事情讓你給我一起面對坚踩,我只想說有你真好荡灾。今天早上你和我說,瑜伽怎么辦瞬铸。這幾乎是我最渴望的事...
    曾曾的麻麻閱讀 380評論 0 0