直接复制粘贴走不谢
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用于回到当前位置
[self setupBtn];
}
- (void)setupBtn{
UIButton *backBtn = [UIButton new];
[backBtn setTitle:@"回到当前位置" 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. 处理位置坐标更新
- (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;
//发起步行路线检索(检索成功后会调用代理方法"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];
}
//这里可以自定义用户图标//- (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;// 设置该标注点动画显示
// return newAnnotationView;
// }
// return nil;
//}
-(void)viewWillAppear:(BOOL)animated
{
[_mapView viewWillAppear];
_mapView.delegate = self; // 此处记得不用的时候需要置nil,否则影响内存的释放
}
-(void)viewWillDisappear:(BOOL)animated
{
[_mapView viewWillDisappear];
_mapView.delegate = nil; // 不用时,置nil
}
-(void)goToUserLocation:(id)sender {
[self.mapView setCenterCoordinate:self.userLocation.location.coordinate];
}
@end