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

直接復(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


/**

總體流程: 開啟實時定位-->每次得到用戶位置-->請求步行路線-->解析-->在地圖上畫線

*/


- (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;

//啟動LocationService

[_locService startUserLocationService];

_mapView.userTrackingMode = BMKUserTrackingModeFollow;

//這個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. 分析路線, 畫圖

- (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ā)起步行路線檢索(檢索成功后會調(diào)用代理方法"onGetWalkingRouteResult...")

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

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

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

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

}

}

-(void)viewDidAppear:(BOOL)animated{

[super viewDidAppear:animated];

}


//這里可以自定義用戶圖標(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)注點動畫顯示

//? ? ? ? return newAnnotationView;

//? ? }

//? ? return nil;

//}

-(void)viewWillAppear:(BOOL)animated

{

[_mapView viewWillAppear];

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

}

-(void)viewWillDisappear:(BOOL)animated

{

[_mapView viewWillDisappear];

_mapView.delegate = nil; // 不用時,置nil

}

-(void)goToUserLocation:(id)sender {

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

}

@end

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容