示例程序
使用百度地圖SDK版本為2.9.1。
需要將改
代碼:
#import <MapKit/MapKit.h>
#import <BaiduMapAPI_Map/BMKMapView.h>
#import <BaiduMapAPI_Location/BMKLocationService.h>
#import <BaiduMapAPI_Search/BMKSearchComponent.h>
#import <BaiduMapAPI_Map/BMKPolylineView.h>
#import <BaiduMapAPI_Utils/BMKGeometry.h>
#import "HXRouteViewController.h"
#import "UIView+Extension.h"
#import "HXAnnotationView.h"
#import "HXAnnotation.h"
#import "HXMenDDetail.h"
#import "UIImage+Rotate.h"
#import "UIColor+HexString.h"
#import "FMActionSheet.h"
#import "ZoomInOrOutButton.h"
#define MYBUNDLE_NAME @ "mapapi.bundle"
#define MYBUNDLE_PATH [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: MYBUNDLE_NAME]
#define MYBUNDLE [NSBundle bundleWithPath: MYBUNDLE_PATH]
/**
* 路線的標(biāo)注
*/
@interface RouteAnnotation : BMKPointAnnotation
{
int _type; ///<0:起點(diǎn) 1:終點(diǎn) 2:公交 3:地鐵 4:駕乘 5:途經(jīng)點(diǎn)
int _degree;
}
@property (nonatomic) int type;
@property (nonatomic) int degree;
@end
@implementation RouteAnnotation
@synthesize type = _type;
@synthesize degree = _degree;
@end
@interface HXRouteViewController ()<BMKMapViewDelegate, BMKRouteSearchDelegate>{
BMKMapView *_mapView;
NSString *_cityName;
BMKRouteSearch *_routesearch;
}
/**
* 返回按鈕
*/
//@property (nonatomic , weak)UIButton *preBtn;
/**
* 存放標(biāo)注數(shù)組
*/
@property(nonatomic , strong) NSMutableArray *annotations;
/**
* 用戶當(dāng)前位置
*/
@property(nonatomic , strong) BMKUserLocation *userLocation;
@property(nonatomic , strong) HXAnnotation *anno;
@end
@implementation HXRouteViewController
- (NSString*)getMyBundlePath1:(NSString *)filename
{
NSBundle * libBundle = MYBUNDLE ;
if ( libBundle && filename ){
NSString * s=[[libBundle resourcePath ] stringByAppendingPathComponent:filename];
return s;
}
return nil ;
}
- (void)viewDidLoad
{
[super viewDidLoad];
UIView *statusView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREENW, 20)];
statusView.backgroundColor = [UIColor colorWithR:46 G:139 B:84 alpha:1.];
[self.view addSubview:statusView];
_routesearch = [[BMKRouteSearch alloc] init];
_routesearch.delegate = self;
//初始化BMKMapView并設(shè)置代理
_mapView = [[BMKMapView alloc] initWithFrame:CGRectMake(0, 20, SCREENW, SCREENH - 140)];
[self.view addSubview:_mapView];
_mapView.delegate = self;
_mapView.buildingsEnabled = YES;
_mapView.overlookEnabled = YES;
_mapView.showMapScaleBar = YES;
_mapView.overlooking = -45;
//設(shè)置放大/縮小按鈕
[self setupNavButton];
//返回按鈕
[self backButton];
//設(shè)置導(dǎo)航按鈕
// [self setupNavButton];
//駕車路線
[self showDriveSearch];
//設(shè)置放大/縮小按鈕
[self setupZoomButton];
}
#pragma mark 設(shè)置放大/縮小按鈕
-(void)setupZoomButton{
ZoomInOrOutButton *zoomInOrOutBtn = [[[NSBundle mainBundle] loadNibNamed:@"ZoomInOrOutButton" owner:nil options:nil] lastObject];
zoomInOrOutBtn.layer.cornerRadius = 5;
zoomInOrOutBtn.x = SCREENW - 110;
zoomInOrOutBtn.y = _mapView.height - 50;
[_mapView addSubview:zoomInOrOutBtn];
[zoomInOrOutBtn.zoomInBtn addTarget:self action:@selector(zoomInBtnClick:) forControlEvents:UIControlEventTouchUpInside];
zoomInOrOutBtn.zoomInBtn.tag = 0;
[zoomInOrOutBtn.zoomOutBtn addTarget:self action:@selector(zoomInBtnClick:) forControlEvents:UIControlEventTouchUpInside];
zoomInOrOutBtn.zoomInBtn.tag = 1;
}
#pragma mark 點(diǎn)擊放大按鈕
-(void)zoomInBtnClick:(UIButton *)sender{
_mapView.zoomLevel = sender.tag ? _mapView.zoomLevel++ : _mapView.zoomLevel--;
}
#pragma mark 返回按鈕
-(void)backButton{
UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(10, 30, 30, 40)];
[backButton setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
backButton.backgroundColor = [UIColor blackColor];
backButton.layer.cornerRadius = 5;
backButton.alpha = 0.7;
[backButton bringSubviewToFront:_mapView];
[backButton initSpringTouch];
[self.view addSubview:backButton];
[backButton addTarget:self action:@selector(goBack) forControlEvents:UIControlEventTouchUpInside];
}
#pragma mark 返回
-(void)goBack{
[self dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark 導(dǎo)航按鈕
-(void)setupNavButton{
UIView *bgView = [[UIView alloc] init];
bgView.frame = CGRectMake(0, SCREENH - 120, SCREENW, 120);
bgView.backgroundColor = [UIColor whiteColor];
[self.view addSubview:bgView];
CGFloat viewX = 15;
UILabel *shopLabel = [[UILabel alloc] initWithFrame:CGRectMake(viewX, 5, SCREENW - 30, 25)];
shopLabel.text = [NSString stringWithFormat:@"門店名稱:%@",self.mendDetail.shopName];
shopLabel.textColor = [UIColor blackColor];
shopLabel.font = [UIFont systemFontOfSize:15];
[bgView addSubview:shopLabel];
UILabel *addressLabel = [[UILabel alloc] initWithFrame:CGRectMake(viewX, 30, SCREENW - 30, 25)];
addressLabel.text = [NSString stringWithFormat:@"門店地址:%@",self.mendDetail.address];
addressLabel.textColor = [UIColor lightGrayColor];
addressLabel.font = [UIFont systemFontOfSize:14];
[bgView addSubview:addressLabel];
UIButton *navButton = [[UIButton alloc] init];
navButton.frame = CGRectMake(15, 65, SCREENW - 30, 40);
[navButton setTitle:@"導(dǎo)航" forState:UIControlStateNormal];
[navButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[navButton.layer setMasksToBounds:YES];
[navButton.layer setCornerRadius:6];
[navButton.layer setBorderWidth:1.0];
[navButton initSpringTouch];
//設(shè)置按鈕的邊界顏色
CGColorRef color = [UIColor colorWithR:46 G:139 B:84 alpha:1.].CGColor;
[navButton.layer setBorderColor:color];
[navButton addTarget:self action:@selector(navButtonClick) forControlEvents:UIControlEventTouchUpInside];
[bgView addSubview:navButton];
}
#pragma mark彈出選擇框
-(void)navButtonClick{
//彈出框
FMActionSheet *sheet = [[FMActionSheet alloc] initWithTitle:@"導(dǎo)航方式" buttonTitles:[NSArray arrayWithObjects:@"用手機(jī)自帶地圖導(dǎo)航", @"百度地圖", nil] cancelButtonTitle:@"取消" delegate:(id<FMActionSheetDelegate>)self];
sheet.tag = 103;
sheet.titleFont = [UIFont systemFontOfSize:15];
sheet.titleBackgroundColor = [UIColor colorWithHexString:@"f4f5f8"];
sheet.titleColor = [UIColor colorWithHexString:@"666666"];
sheet.lineColor = [UIColor colorWithHexString:@"dbdce4"];
[sheet show];
}
- (void)actionSheet:(FMActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex == 0) {
//打開系統(tǒng)自帶的地圖
[self openIOSMapNav];
} else if (buttonIndex == 1) {
//打開百度地圖客戶端導(dǎo)航
[self opsenBaiduMap];
}
}
#pragma mark 打開系統(tǒng)自帶的地圖
-(void)openIOSMapNav{
CLLocationCoordinate2D endCoor = CLLocationCoordinate2DMake([self.mendDetail.dimensions doubleValue], [self.mendDetail.longitude doubleValue]);
MKMapItem *currentLocation = [MKMapItem mapItemForCurrentLocation];
MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:endCoor addressDictionary:nil]];
toLocation.name = self.mendDetail.shopName;
[MKMapItem openMapsWithItems:@[currentLocation, toLocation] launchOptions:@{MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving,MKLaunchOptionsShowsTrafficKey: [NSNumber numberWithBool:YES]}];
}
-(void)opsenBaiduMap{
double latitude =self.coordinate.latitude;
double longitude =self.coordinate.longitude;
NSString *urlString = [NSString stringWithFormat:@"baidumap://map/direction?origin=%f,%f&destination=%@,%@&mode=driving®ion=北京",latitude,longitude,self.mendDetail.dimensions,self.mendDetail.longitude];
urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *mapUrl = [NSURL URLWithString:urlString];
if ([[UIApplication sharedApplication] canOpenURL:mapUrl]) {
[[UIApplication sharedApplication] openURL:mapUrl];
} else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"您的手機(jī)沒有安裝百度地圖" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"確定", nil];
[alert show];
}
}
- (UIColor *)actionSheet:(FMActionSheet *)actionSheet buttonTextColorAtIndex:(NSInteger)bottonIndex{
UIColor *textColor = [UIColor colorWithR:41 G:140 B:82 alpha:1.];
return textColor;
}
#pragma mark -顯示大頭針
- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id <BMKAnnotation>)annotation{
if (![annotation isKindOfClass:[RouteAnnotation class]]) return nil;
return [self getRouteAnnotationView:mapView viewForAnnotation:(RouteAnnotation *)annotation];
}
#pragma mark 獲取路線的標(biāo)注督怜,顯示到地圖
- (BMKAnnotationView*)getRouteAnnotationView:(BMKMapView *)mapview viewForAnnotation:(RouteAnnotation*)routeAnnotation{
BMKAnnotationView *view = nil;
switch (routeAnnotation.type) {
case 0:
{
view = [mapview dequeueReusableAnnotationViewWithIdentifier:@"start_node"];
if (view == nil) {
view = [[BMKAnnotationView alloc] initWithAnnotation:routeAnnotation reuseIdentifier:@"start_node"];
view.image = [UIImage imageWithContentsOfFile:[self getMyBundlePath1:@"images/icon_nav_start"]];
view.centerOffset = CGPointMake(0, -(view.frame.size.height * 0.5));
view.canShowCallout = true;
}
view.annotation = routeAnnotation;
}
break;
case 1:
{
view = [mapview dequeueReusableAnnotationViewWithIdentifier:@"end_node"];
if (view == nil) {
view = [[BMKAnnotationView alloc] initWithAnnotation:routeAnnotation reuseIdentifier:@"end_node"];
view.image = [UIImage imageWithContentsOfFile:[self getMyBundlePath1:@"images/icon_nav_end"]];
view.centerOffset = CGPointMake(0, -(view.frame.size.height * 0.5));
view.canShowCallout = true;
}
view.annotation =routeAnnotation;
}
break;
case 4:
{
view = [mapview dequeueReusableAnnotationViewWithIdentifier:@"route_node"];
if (view == nil) {
view = [[BMKAnnotationView alloc] initWithAnnotation:routeAnnotation reuseIdentifier:@"route_node"];
view.canShowCallout = true;
} else {
[view setNeedsDisplay];
}
UIImage *image = [UIImage imageWithContentsOfFile:[self getMyBundlePath1:@"images/icon_direction"]];
view.image = [image imageRotatedByDegrees:routeAnnotation.degree];
view.annotation = routeAnnotation;
}
break;
default:
break;
}
return view;
}
#pragma mark 駕車路線
-(void)showDriveSearch{
//線路檢索節(jié)點(diǎn)信息
BMKPlanNode *start = [[BMKPlanNode alloc] init];
start.pt = self.coordinate;
start.cityName = @"北京市";
BMKPlanNode *end = [[BMKPlanNode alloc] init];
CLLocationCoordinate2D endCoordinate = CLLocationCoordinate2DMake([self.mendDetail.dimensions doubleValue], [self.mendDetail.longitude doubleValue]);
end.pt = endCoordinate;
end.cityName = @"北京市";
BMKDrivingRoutePlanOption *drivingRouteSearchOption = [[BMKDrivingRoutePlanOption alloc] init];
drivingRouteSearchOption.from = start;
drivingRouteSearchOption.to = end;
BOOL flag = [_routesearch drivingSearch:drivingRouteSearchOption];
if (flag) {
}
}
#pragma mark 返回駕乘搜索結(jié)果
- (void)onGetDrivingRouteResult:(BMKRouteSearch*)searcher result:(BMKDrivingRouteResult*)result errorCode:(BMKSearchErrorCode)error
{
NSArray* array = [NSArray arrayWithArray:_mapView.annotations];
[_mapView removeAnnotations:array];
array = [NSArray arrayWithArray:_mapView.overlays];
[_mapView removeOverlays:array];
if (error == BMK_SEARCH_NO_ERROR) {
//表示一條駕車路線
BMKDrivingRouteLine* plan = (BMKDrivingRouteLine*)[result.routes objectAtIndex:0];
// 計(jì)算路線方案中的路段數(shù)目
int size = (int)[plan.steps count];
int planPointCounts = 0;
for (int i = 0; i < size; i++) {
//表示駕車路線中的一個(gè)路段
BMKDrivingStep* transitStep = [plan.steps objectAtIndex:i];
if(i==0){
RouteAnnotation* item = [[RouteAnnotation alloc]init];
item.coordinate = plan.starting.location;
item.title = @"起點(diǎn)";
item.type = 0;
[_mapView addAnnotation:item]; // 添加起點(diǎn)標(biāo)注
}else if(i==size-1){
RouteAnnotation* item = [[RouteAnnotation alloc]init];
item.coordinate = plan.terminal.location;
item.title = @"終點(diǎn)";
item.type = 1;
[_mapView addAnnotation:item]; // 添加終點(diǎn)標(biāo)注
}
//添加annotation節(jié)點(diǎn)
RouteAnnotation* item = [[RouteAnnotation alloc]init];
item.coordinate = transitStep.entrace.location;
item.title = transitStep.entraceInstruction;
item.degree = transitStep.direction * 30;
item.type = 4;
[_mapView addAnnotation:item];
//軌跡點(diǎn)總數(shù)累計(jì)
planPointCounts += transitStep.pointsCount;
}
// 添加途經(jīng)點(diǎn)
if (plan.wayPoints) {
for (BMKPlanNode* tempNode in plan.wayPoints) {
RouteAnnotation* item = [[RouteAnnotation alloc]init];
item.coordinate = tempNode.pt;
item.type = 5;
item.title = tempNode.name;
[_mapView addAnnotation:item];
}
}
//軌跡點(diǎn)
BMKMapPoint * temppoints = new BMKMapPoint[planPointCounts];
int i = 0;
for (int j = 0; j < size; j++) {
BMKDrivingStep* transitStep = [plan.steps objectAtIndex:j];
int k=0;
for(k=0;k<transitStep.pointsCount;k++) {
temppoints[i].x = transitStep.points[k].x;
temppoints[i].y = transitStep.points[k].y;
i++;
}
}
// 通過points構(gòu)建BMKPolyline
BMKPolyline* polyLine = [BMKPolyline polylineWithPoints:temppoints count:planPointCounts];
[_mapView addOverlay:polyLine]; // 添加路線overlay
delete []temppoints;
[self mapViewFitPolyLine:polyLine];
}
}
#pragma mark 根據(jù)overlay生成對(duì)應(yīng)的View
-(BMKOverlayView *)mapView:(BMKMapView *)mapView viewForOverlay:(id<BMKOverlay>)overlay{
if ([overlay isKindOfClass:[BMKPolyline class]]) {
BMKPolylineView* polylineView = [[BMKPolylineView alloc] initWithOverlay:overlay];
polylineView.fillColor = [[UIColor cyanColor] colorWithAlphaComponent:1];
polylineView.strokeColor = [[UIColor blueColor] colorWithAlphaComponent:0.7];
polylineView.lineWidth = 3.0;
return polylineView;
}
return nil;
}
#pragma mark 根據(jù)polyline設(shè)置地圖范圍
- (void)mapViewFitPolyLine:(BMKPolyline *) polyLine {
CGFloat ltX, ltY, rbX, rbY;
if (polyLine.pointCount < 1) return;
BMKMapPoint pt = polyLine.points[0];
ltX = pt.x, ltY = pt.y;
rbX = pt.x, rbY = pt.y;
for (int i = 0; i < polyLine.pointCount; i++) {
BMKMapPoint pt = polyLine.points[i];
if (pt.x < ltX) {
ltX = pt.x;
}
if (pt.x > rbX) {
rbX = pt.x;
}
if (pt.y > ltY) {
ltY = pt.y;
}
if (pt.y < rbY) {
rbY = pt.y;
}
}
BMKMapRect rect;
rect.origin = BMKMapPointMake(ltX , ltY);
rect.size = BMKMapSizeMake(rbX - ltX, rbY - ltY);
[_mapView setVisibleMapRect:rect];
_mapView.zoomLevel = _mapView.zoomLevel - 0.3;
}
-(void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[_mapView viewWillDisappear];
_mapView.delegate = nil; // 不用時(shí)抒和,置nil
_routesearch.delegate = nil;
}
@end