百度地圖demo--定位, 步行路線檢索, 畫(huà)線.

直接復(fù)制粘貼走不謝


appdelegate.h 文件


#import#import@interface AppDelegate : UIResponder

@property (strong, nonatomic) UIWindow *window;

@property (nonatomic, strong) BMKMapManager *mapManager;

@property (nonatomic, strong) UINavigationController *navigationController;

@end

appdelegate.m 文件


#import "AppDelegate.h"

@interface AppDelegate ()

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

self.mapManager = [[BMKMapManager alloc]init];

BOOL ret = [_mapManager start:@"ZDWvBohcN1G7cQUvid5rj1D7P1ilHAe0"? generalDelegate:nil];

if (!ret) {

NSLog(@"manager start failed!");

}

[self.window addSubview:self.navigationController.view];

[self.window makeKeyAndVisible];

return YES;

}

viewcontroller.m文件



#import "ViewController.h"

#import<BaiduMapAPI_Map/BMKMapComponent.h>//引入地圖功能所有的頭文件

#import<BaiduMapAPI_Location/BMKLocationComponent.h>//引入定位功能所有的頭文件

#import<BaiduMapAPI_Search/BMKRouteSearch.h>

#import "SVProgressHUD.h"

#import<BaiduMapAPI_Base/BMKTypes.h>

#import<BaiduMapAPI_Utils/BMKGeometry.h>


@interface ViewController ()<BMKMapViewDelegate,BMKRouteSearchDelegate,BMKLocationServiceDelegate>


@property (nonatomic, strong)BMKMapView *mapView;

@property (nonatomic, strong)BMKRouteSearch *routeSearch;

@property (nonatomic, strong)BMKLocationService *locService;

@property (nonatomic, strong)BMKUserLocation *userLocation;

@property (nonatomic, strong)BMKPolyline* polyline;

@property(nonatomic,weak)UIButton *backBtn;

@property(nonatomic,weak)UILabel *distanceLabel;

@end

@implementation ViewController


/**

總體流程: 開(kāi)啟實(shí)時(shí)定位-->每次得到用戶(hù)位置-->請(qǐng)求步行路線-->解析-->在地圖上畫(huà)線

*/


- (void)viewDidLoad {

[super viewDidLoad];

BMKMapView* mapView = [[BMKMapView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];

_mapView =mapView;

self.view = mapView;

//1. 定位

_mapView.showsUserLocation = YES;//顯示定位圖層

//初始化BMKLocationService

_locService = [[BMKLocationService alloc]init];

_locService.delegate = self;

//啟動(dòng)LocationService

[_locService startUserLocationService];

_mapView.userTrackingMode = BMKUserTrackingModeFollow;

//這個(gè)button用于回到當(dāng)前位置

[self setupBtn];

}


- (void)setupBtn{

UIButton *backBtn = [UIButton new];

[backBtn setTitle:@"回到當(dāng)前位置" forState:UIControlStateNormal];

[backBtn setImage:[UIImage imageNamed:@"tabbar_compose_background_icon_return"] forState:UIControlStateNormal];

[backBtn sizeToFit];

[self.mapView addSubview:backBtn];//62 135 47

backBtn.titleLabel.font = [UIFont boldSystemFontOfSize:13];

backBtn.frame = CGRectMake([UIScreen mainScreen].bounds.size.width - 150, 50, 150, 50);

[backBtn setTitleColor:[UIColor colorWithRed:62/255.0 green:135/255.0 blue:47/255.0 alpha:1] forState:UIControlStateNormal];

[backBtn addTarget:self action:@selector(goToUserLocation:) forControlEvents:UIControlEventTouchUpInside];

_backBtn = backBtn;

_backBtn.hidden = YES;

UILabel *distanceLabel = [[UILabel alloc]init];

distanceLabel.text = @"miiii";

[self.mapView addSubview:distanceLabel];

_distanceLabel = distanceLabel;

distanceLabel.frame = CGRectMake(self.mapView.center.x, self.mapView.center.y - 200, 200, 50);

}


//3. 分析路線, 畫(huà)圖

- (void)onGetWalkingRouteResult:(BMKRouteSearch*)searcher result:(BMKWalkingRouteResult*)result errorCode:(BMKSearchErrorCode)error{

if (error) {

NSLog(@"%u",error);

}else{

BMKRouteLine *line = result.routes[0];

_distanceLabel.text = [NSString stringWithFormat:@"%d",line.distance];

int count = 0;

for (int i = 0; i < line.steps.count; i++) {

BMKWalkingStep* step = line.steps[i];

count += step.pointsCount;

}

CLLocationCoordinate2D coors[count];

int count2 = 0;

for (int i = 0; i < line.steps.count; i++) {

BMKWalkingStep* step = line.steps[i];

for (int j = 0; j < step.pointsCount; j++) {

BMKMapPoint p = step.points[j];

//? ? ? ? ? ? NSLog(@"%f, %f", p.x, p.y);

CLLocationCoordinate2D annotationCoord = BMKCoordinateForMapPoint(p);

coors[count2] = annotationCoord;

count2++;

}

}

if (self.polyline) {

[self.mapView removeOverlays:self.mapView.overlays];

}

self.polyline = [BMKPolyline polylineWithCoordinates:coors count:count];

[_mapView addOverlay:_polyline];

}

}


//返回自定義的路線線條樣式- (BMKOverlayView *)mapView:(BMKMapView *)mapView viewForOverlay:(id)overlay{

if ([overlay isKindOfClass:[BMKPolyline class]]){

BMKPolylineView* polylineView = [[BMKPolylineView alloc] initWithOverlay:overlay];

polylineView.strokeColor = [[UIColor colorWithRed:arc4random_uniform(255)/255.0 green:arc4random_uniform(255)/255.0 blue:arc4random_uniform(255)/255.0 alpha:1] colorWithAlphaComponent:0.8];

polylineView.lineWidth = 3.0;

[SVProgressHUD dismiss];

self.backBtn.hidden = NO;

return polylineView;

}

return nil;

}


//2. 處理位置坐標(biāo)更新

- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation

{

//? ? NSLog(@"didUpdateUserLocation lat %f,long %f",userLocation.location.coordinate.latitude,userLocation.location.coordinate.longitude);

[_mapView updateLocationData:userLocation];

self.userLocation = userLocation;

//? ? BMKPointAnnotation* annotation = [[BMKPointAnnotation alloc]init];

//? ? annotation.coordinate = userLocation.location.coordinate;

//? ? annotation.title = @"這里是...";

//? ? [_mapView addAnnotation:annotation];

//

self.routeSearch = [[BMKRouteSearch alloc]init];

_routeSearch.delegate = self;

BMKPlanNode *start = [[BMKPlanNode alloc]init];

start.pt = self.userLocation.location.coordinate;

BMKPlanNode *end = [[BMKPlanNode alloc]init];

end.pt = CLLocationCoordinate2DMake(31.17034,121.412371);

BMKWalkingRoutePlanOption *option = [[BMKWalkingRoutePlanOption alloc]init];

option.from = start;

option.to = end;

//發(fā)起步行路線檢索(檢索成功后會(huì)調(diào)用代理方法"onGetWalkingRouteResult...")

BOOL res = [self.routeSearch walkingSearch:option];

NSLog(@"res = %d", res);

if (self.polyline == nil) {//第一次加載地圖時(shí)間比較長(zhǎng), 使用HUD, 后面不再使用.

[SVProgressHUD showWithStatus:@"路線加載中"];

}

}

-(void)viewDidAppear:(BOOL)animated{

[super viewDidAppear:animated];

}


//這里可以自定義用戶(hù)圖標(biāo)//- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id)annotation

//{

//? ? if ([annotation isKindOfClass:[BMKPointAnnotation class]]) {

//? ? ? ? BMKPinAnnotationView *newAnnotationView = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"myAnnotation"];

//? ? ? ? newAnnotationView.pinColor = BMKPinAnnotationColorPurple;

//? ? ? ? newAnnotationView.animatesDrop = YES;// 設(shè)置該標(biāo)注點(diǎn)動(dòng)畫(huà)顯示

//? ? ? ? return newAnnotationView;

//? ? }

//? ? return nil;

//}

-(void)viewWillAppear:(BOOL)animated

{

[_mapView viewWillAppear];

_mapView.delegate = self; // 此處記得不用的時(shí)候需要置nil沿量,否則影響內(nèi)存的釋放

}

-(void)viewWillDisappear:(BOOL)animated

{

[_mapView viewWillDisappear];

_mapView.delegate = nil; // 不用時(shí)霍殴,置nil

}

-(void)goToUserLocation:(id)sender {

[self.mapView setCenterCoordinate:self.userLocation.location.coordinate];

}

@end

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末员舵,一起剝皮案震驚了整個(gè)濱河市哲银,隨后出現(xiàn)的幾起案子呻畸,更是在濱河造成了極大的恐慌,老刑警劉巖赏迟,帶你破解...
    沈念sama閱讀 222,000評(píng)論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件洽故,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡笆凌,警方通過(guò)查閱死者的電腦和手機(jī)圣猎,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,745評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門(mén),熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)乞而,“玉大人送悔,你說(shuō)我怎么就攤上這事。” “怎么了放祟?”我有些...
    開(kāi)封第一講書(shū)人閱讀 168,561評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵鳍怨,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我跪妥,道長(zhǎng)鞋喇,這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 59,782評(píng)論 1 298
  • 正文 為了忘掉前任眉撵,我火速辦了婚禮侦香,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘纽疟。我一直安慰自己罐韩,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,798評(píng)論 6 397
  • 文/花漫 我一把揭開(kāi)白布污朽。 她就那樣靜靜地躺著散吵,像睡著了一般。 火紅的嫁衣襯著肌膚如雪蟆肆。 梳的紋絲不亂的頭發(fā)上矾睦,一...
    開(kāi)封第一講書(shū)人閱讀 52,394評(píng)論 1 310
  • 那天,我揣著相機(jī)與錄音炎功,去河邊找鬼枚冗。 笑死,一個(gè)胖子當(dāng)著我的面吹牛蛇损,可吹牛的內(nèi)容都是我干的赁温。 我是一名探鬼主播,決...
    沈念sama閱讀 40,952評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼淤齐,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼股囊!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起更啄,我...
    開(kāi)封第一講書(shū)人閱讀 39,852評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤稚疹,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后锈死,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體贫堰,經(jīng)...
    沈念sama閱讀 46,409評(píng)論 1 318
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡穆壕,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,483評(píng)論 3 341
  • 正文 我和宋清朗相戀三年待牵,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片喇勋。...
    茶點(diǎn)故事閱讀 40,615評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡缨该,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出川背,到底是詐尸還是另有隱情贰拿,我是刑警寧澤蛤袒,帶...
    沈念sama閱讀 36,303評(píng)論 5 350
  • 正文 年R本政府宣布,位于F島的核電站膨更,受9級(jí)特大地震影響妙真,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜荚守,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,979評(píng)論 3 334
  • 文/蒙蒙 一珍德、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧矗漾,春花似錦锈候、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 32,470評(píng)論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至誊役,卻和暖如春获列,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背势木。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,571評(píng)論 1 272
  • 我被黑心中介騙來(lái)泰國(guó)打工蛛倦, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人啦桌。 一個(gè)月前我還...
    沈念sama閱讀 49,041評(píng)論 3 377
  • 正文 我出身青樓溯壶,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親甫男。 傳聞我的和親對(duì)象是個(gè)殘疾皇子且改,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,630評(píng)論 2 359

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