在开发中需要用到地图。有两种方法,一种是导入地图 (百度,高德)地图 SDK,一种是使用调起 客户端。
但是需要用到一些高级功能的时候,如果使用 SDK 开发,就是一件折磨人的事情。 SDK 还是不如直接使用他们原生的 APP 来的好!
以我在开发中使用导航功能为例,我就没有使用 SDK 进行开发,而是直接调用 (百度,高德,系统自带高德)APP。这样还给了客户多重选择。更加减少了引入 SDK 使 APP 臃肿的问题。如果使用百度或高德的调起方法配置起来感觉比较麻烦 ,还好他们都提供了 URL API 的方式,这种方式觉得稍微简单一点
下面将具体使用贴出代码如下:更多使用参考(百度,高德)官方文档
demo:http://download.csdn.net/detail/qq_26598821/9484647
1,App 调起百度地图路线规划进行导航。百度 URL API :http://lbsyun.baidu.com/index.php?title=uri/api/ios
- #pragma mark ------------------------------ 导航 - 百度
- -(void) onDaoHangForBaiDuMap
- {
- // 百度地图如何调起APP进行导航
- // mode 导航模式,固定为transit、driving、walking,分别表示公交、驾车和步行
- NSString * modeBaiDu = @"driving";
- switch (_seleIndex) {
- case 1:
- {
- modeBaiDu = @"transit";
- }
- break;
- case 2:
- {
- modeBaiDu = @"driving";
- }
- break;
- case 3:
- {
- modeBaiDu = @"walking";
- }
- break;
- default:
- break;
- }
- NSString *url = [[NSString stringWithFormat:@"baidumap://map/direction?origin=%lf,%lf&destination=%f,%f&mode=%@&src=公司|APP",[SingleObject shareSingleObject].currentCoordinate.latitude,[SingleObject shareSingleObject].currentCoordinate.longitude,self.location.latitude,self.location.longitude,modeBaiDu] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] ;
- // [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
- if ([[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]])// -- 使用 canOpenURL:[NSURL URLWithString:@"baidumap://"] 判断不明白为什么为否。
- {
- [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
- }else{
- [[[UIAlertView alloc]initWithTitle:@"没有安装百度地图" message:@"" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil nil] show];
- }
- }
2,App 调起高德地图路线规划进行导航。高德 URL API:http://lbs.amap.com/api/uri-api/ios-uri-explain/
- #pragma mark ------------------------------ 导航 - 高德
- -(void) onDaoHangForGaoDeMap
- {
- // m 驾车:0:速度最快,1:费用最少,2:距离最短,3:不走高速,4:躲避拥堵,5:不走高速且避免收费,6:不走高速且躲避拥堵,7:躲避收费和拥堵,8:不走高速躲避收费和拥堵 公交:0:最快捷,2:最少换乘,3:最少步行,5:不乘地铁 ,7:只坐地铁 ,8:时间短 是
- // t = 0:驾车 =1:公交 =2:步行
- NSString * t = @"0";
- switch (_seleIndex) {
- case 1:
- {
- t = @"1";
- }
- break;
- case 2:
- {
- t = @"0";
- }
- break;
- case 3:
- {
- t = @"2";
- }
- break;
- default:
- break;
- }
- //起点
- CLLocation * location = [[CLLocation alloc]initWithLatitude:[SingleObject shareSingleObject].currentCoordinate.latitude longitude:[SingleObject shareSingleObject].currentCoordinate.longitude];
- location = [location locationMarsFromBaidu];
- CLLocationCoordinate2D coor =location.coordinate;
- //目的地的位置
- CLLocation * location2 = [[CLLocation alloc]initWithLatitude:self.location.latitude longitude:self.location.longitude];
- location2 = [location2 locationMarsFromBaidu];
- CLLocationCoordinate2D coor2 =location2.coordinate;
- // 导航 URL:iosamap://navi?sourceApplication=%@&poiname=%@&lat=%lf&lon=%lf&dev=0&style=0",@"ABC"
- // 路径规划 URL:iosamap://path?sourceApplication=applicationName&sid=BGVIS1&slat=39.92848272&slon=116.39560823&sname=A&did=BGVIS2&dlat=39.98848272&dlon=116.47560823&dname=B&dev=0&m=0&t=0
- // -- 不能直接让用户进入导航,应该给用户更多的选择,所以先进行路径规划
- NSString *url = [[NSString stringWithFormat:@"iosamap://path?sourceApplication=applicationName&sid=BGVIS1&slat=%lf&slon=%lf&sname=我的位置&did=BGVIS2&dlat=%lf&dlon=%lf&dname=%@&dev=0&m=0&t=%@",coor.latitude,coor.longitude, coor2.latitude,coor2.longitude,self.mainTitle,t] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
- if ([[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]]) // -- 使用 canOpenURL:[NSURL URLWithString:@"iosamap://"] 判断不明白为什么为否。
- {
- [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
- }else{
- [[[UIAlertView alloc]initWithTitle:@"没有安装高德地图" message:@"" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil nil] show];
- }
- }
1,App 调起系统自带高德地图路线规划进行导航。
- #pragma mark ------------------------------ 导航 - iosMap
- -(void) onDaoHangForIOSMap
- {
- //起点
- CLLocation * location = [[CLLocation alloc]initWithLatitude:[SingleObject shareSingleObject].currentCoordinate.latitude longitude:[SingleObject shareSingleObject].currentCoordinate.longitude];
- location = [location locationMarsFromBaidu];
- CLLocationCoordinate2D coor =location.coordinate;
- MKMapItem *currentLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:coor addressDictionary:nil]];
- currentLocation.name =@"我的位置";
- //目的地的位置
- CLLocation * location2 = [[CLLocation alloc]initWithLatitude:self.location.latitude longitude:self.location.longitude];
- location2 = [location2 locationMarsFromBaidu];
- CLLocationCoordinate2D coor2 =location2.coordinate;
- // CLLocationCoordinate2D coords = self.location;
- MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:coor2 addressDictionary:nil]];
- toLocation.name = self.mainTitle;
- NSArray *items = [NSArray arrayWithObjects:currentLocation, toLocation, nil nil];
- NSString * mode = MKLaunchOptionsDirectionsModeDriving;
- switch (_seleIndex) {
- case 1:
- {
- mode = MKLaunchOptionsDirectionsModeTransit;
- }
- break;
- case 2:
- {
- mode = MKLaunchOptionsDirectionsModeDriving;
- }
- break;
- case 3:
- {
- mode = MKLaunchOptionsDirectionsModeWalking;
- }
- break;
- default:
- break;
- }
- NSDictionary *options = @{ MKLaunchOptionsDirectionsModeKey:mode, MKLaunchOptionsMapTypeKey: [NSNumber numberWithInteger:MKMapTypeStandard], MKLaunchOptionsShowsTrafficKey:@YES };
- //打开苹果自身地图应用,并呈现特定的item
- [MKMapItem openMapsWithItems:items launchOptions:options];
- }
(转载自)ios 百度地图,火星坐标,地球坐标互转 http://www.oschina.net/code/snippet_2296716_48678
下面是工程中地图代码:
- //
- // RouteSearchDemoViewController.h
- // BaiduMapApiDemo
- //
- // Copyright 2011 Baidu Inc. All rights reserved.
- //
- /*
- //在上一个类进入本类 (地图显示界面)
- RouteSearchDemoViewController *vc = [[RouteSearchDemoViewController alloc]init];
- vc.mainTitle = _shopDetailMOD.businessName;
- vc.subTitle = _shopDetailMOD.businessAddress;
- CLLocationCoordinate2D coor;
- coor.latitude = [_shopDetailMOD.latitude floatValue];
- coor.longitude = [_shopDetailMOD.longitude floatValue];
- vc.location = coor;
- vc.hidesBottomBarWhenPushed = YES;
- [self.navigationController pushViewController:vc animated:YES];
- */
- #import <UIKit/UIKit.h>
- @interface RouteSearchDemoViewController : UIViewController<BMKMapViewDelegate, BMKRouteSearchDelegate,UIAlertViewDelegate> {
- IBOutlet BMKMapView* _mapView;
- BMKRouteSearch* _routesearch;
- BMKPointAnnotation* _pointAnnotation;
- NSInteger _seleIndex;
- }
- -(IBAction)onClickBusSearch; // 公交
- -(IBAction)onClickDriveSearch; // 自驾
- -(IBAction)onClickWalkSearch; // 步行
- - (IBAction)onClickRidingSearch:(id)sender; //骑行
- @property(strong, nonatomic) NSString * mainTitle;
- @property(strong, nonatomic) NSString * subTitle;
- @property(assign, nonatomic) CLLocationCoordinate2D location;
- @end
- //
- // RouteSearchDemoViewController.mm
- // BaiduMapApiDemo
- //
- // Copyright 2011 Baidu Inc. All rights reserved.
- //
- #import "RouteSearchDemoViewController.h"
- #import "UIImage+Rotate.h"
- #import "CLLocation+YCLocation.h"
- #import <MapKit/MapKit.h>
- #define MYBUNDLE_NAME @ "mapapi.bundle"
- #define MYBUNDLE_PATH [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: MYBUNDLE_NAME]
- #define MYBUNDLE [NSBundle bundleWithPath: MYBUNDLE_PATH]
- @interface RouteAnnotation : BMKPointAnnotation
- {
- int _type; ///<0:起点 1:终点 2:公交 3:地铁 4:驾乘 5:途经点
- int _degree;
- }
- @property (nonatomic) int type;
- @property (nonatomic) int degree;
- @end
- @implementation RouteAnnotation
- @synthesize type = _type;
- @synthesize degree = _degree;
- @end
- @implementation RouteSearchDemoViewController
- // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- - (void)viewDidLoad {
- [super viewDidLoad];
- //适配ios7
- if( ([[[UIDevice currentDevice] systemVersion] doubleValue]>=7.0))
- {
- // self.edgesForExtendedLayout=UIRectEdgeNone;
- self.navigationController.navigationBar.translucent = NO;
- }
- // _mapView = [[BMKMapView alloc]initWithFrame:CGRectMake(0, 0, MainScreenWidth, MainScreenHeight - 64)];
- // [self.view addSubview: _mapView];
- // _mapView.delegate = self; // 此处记得不用的时候需要置nil,否则影响内存的释放
- [self addPointAnnotation:self.location];
- _routesearch = [[BMKRouteSearch alloc]init];
- _routesearch.delegate = self;
- _seleIndex = 0;
- }
- - (NSString *)title {
- return self.mainTitle;
- }
- -(void)addPointAnnotation:(CLLocationCoordinate2D )location {
- _pointAnnotation = [[BMKPointAnnotation alloc]init];
- // _pointAnnotation = [[BMKPointAnnotation alloc]init];
- _pointAnnotation.coordinate = location;
- _pointAnnotation.title = self.mainTitle;
- _pointAnnotation.subtitle = self.subTitle;
- [_mapView addAnnotation:_pointAnnotation];
- [_mapView selectAnnotation:_pointAnnotation animated:YES];
- [_mapView setRegion:BMKCoordinateRegionMake(location, BMKCoordinateSpanMake(0.01, 0.01))];
- [_mapView setCenterCoordinate:location animated:NO]; //使地图移动到定位的地方
- }
- -(void)viewWillAppear:(BOOL)animated {
- [_mapView viewWillAppear];
- _mapView.delegate = self; // 此处记得不用的时候需要置nil,否则影响内存的释放
- _routesearch.delegate = self; // 此处记得不用的时候需要置nil,否则影响内存的释放
- }
- -(void)viewWillDisappear:(BOOL)animated {
- [_mapView viewWillDisappear];
- _mapView.delegate = nil; // 不用时,置nil
- _routesearch.delegate = nil; // 不用时,置nil
- }
- - (void)didReceiveMemoryWarning {
- // Releases the view if it doesn't have a superview.
- [super didReceiveMemoryWarning];
- // Release any cached data, images, etc. that aren't in use.
- }
- - (void)viewDidUnload {
- [super viewDidUnload];
- // Release any retained subviews of the main view.
- // e.g. self.myOutlet = nil;
- }
- - (void)dealloc {
- if (_routesearch != nil) {
- _routesearch = nil;
- }
- if (_mapView) {
- _mapView = nil;
- }
- }
- - (void)wayPointDemo {
- // WayPointRouteSearchDemoViewController * wayPointCont = [[WayPointRouteSearchDemoViewController alloc]init];
- // wayPointCont.title = @"驾车途经点";
- // UIBarButtonItem *customLeftBarButtonItem = [[UIBarButtonItem alloc] init];
- // customLeftBarButtonItem.title = @"返回";
- // self.navigationItem.backBarButtonItem = customLeftBarButtonItem;
- // [self.navigationController pushViewController:wayPointCont animated:YES];
- }
- #pragma mark - BMKMapViewDelegate
- - (BMKAnnotationView *)mapView:(BMKMapView *)view viewForAnnotation:(id <BMKAnnotation>)annotation
- {
- if ([annotation isKindOfClass:[RouteAnnotation class]]) {
- return [self getRouteAnnotationView:view viewForAnnotation:(RouteAnnotation*)annotation];
- }
- return nil;
- }
- - (BMKOverlayView*)mapView:(BMKMapView *)map viewForOverlay:(id<BMKOverlay>)overlay
- {
- if ([overlay isKindOfClass:[BMKPolyline class]]) {
- BMKPolylineView* polylineView = [[BMKPolylineView alloc] initWithOverlay:overlay];
- polylineView.fillColor = [[UIColor alloc] initWithRed:0 green:1 blue:1 alpha:1];
- polylineView.strokeColor = [[UIColor alloc] initWithRed:0 green:0 blue:1 alpha:0.7];
- polylineView.lineWidth = 3.0;
- return polylineView;
- }
- return nil;
- }
- #pragma mark - BMKRouteSearchDelegate
- - (void)onGetTransitRouteResult:(BMKRouteSearch*)searcher result:(BMKTransitRouteResult*)result errorCode:(BMKSearchErrorCode)error
- {
- NSArray* array = [NSArray arrayWithArray:_mapView.annotations];
- [_mapView removeAnnotations:array];
- array = [NSArray arrayWithArray:_mapView.overlays];
- [_mapView removeOverlays:array];
- NSLog(@"%d",error);
- if (error == BMK_SEARCH_NO_ERROR) {
- BMKTransitRouteLine* plan = (BMKTransitRouteLine*)[result.routes objectAtIndex:0];
- // 计算路线方案中的路段数目
- NSInteger size = [plan.steps count];
- int planPointCounts = 0;
- for (int i = 0; i < size; i++) {
- BMKTransitStep* transitStep = [plan.steps objectAtIndex:i];
- if(i==0){
- RouteAnnotation* item = [[RouteAnnotation alloc]init];
- item.coordinate = plan.starting.location;
- item.title = @"起点";
- item.type = 0;
- [_mapView addAnnotation:item]; // 添加起点标注
- }else if(i==size-1){
- RouteAnnotation* item = [[RouteAnnotation alloc]init];
- item.coordinate = plan.terminal.location;
- item.title = @"终点";
- item.type = 1;
- [_mapView addAnnotation:item]; // 添加起点标注
- }
- RouteAnnotation* item = [[RouteAnnotation alloc]init];
- item.coordinate = transitStep.entrace.location;
- item.title = transitStep.instruction;
- item.type = 3;
- [_mapView addAnnotation:item];
- //轨迹点总数累计
- planPointCounts += transitStep.pointsCount;
- }
- //轨迹点
- BMKMapPoint * temppoints = new BMKMapPoint[planPointCounts];
- int i = 0;
- for (int j = 0; j < size; j++) {
- BMKTransitStep* 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构建BMKPolyline
- BMKPolyline* polyLine = [BMKPolyline polylineWithPoints:temppoints count:planPointCounts];
- [_mapView addOverlay:polyLine]; // 添加路线overlay
- delete []temppoints;
- [self mapViewFitPolyLine:polyLine];
- }
- }
- - (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];
- // 计算路线方案中的路段数目
- NSInteger size = [plan.steps count];
- int planPointCounts = 0;
- for (int i = 0; i < size; i++) {
- BMKDrivingStep* transitStep = [plan.steps objectAtIndex:i];
- if(i==0){
- RouteAnnotation* item = [[RouteAnnotation alloc]init];
- item.coordinate = plan.starting.location;
- item.title = @"起点";
- item.type = 0;
- [_mapView addAnnotation:item]; // 添加起点标注
- }else if(i==size-1){
- RouteAnnotation* item = [[RouteAnnotation alloc]init];
- item.coordinate = plan.terminal.location;
- item.title = @"终点";
- item.type = 1;
- [_mapView addAnnotation:item]; // 添加起点标注
- }
- //添加annotation节点
- 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];
- //轨迹点总数累计
- planPointCounts += transitStep.pointsCount;
- }
- // 添加途经点
- if (plan.wayPoints) {
- for (BMKPlanNode* tempNode in plan.wayPoints) {
- RouteAnnotation* item = [[RouteAnnotation alloc]init];
- item = [[RouteAnnotation alloc]init];
- item.coordinate = tempNode.pt;
- item.type = 5;
- item.title = tempNode.name;
- [_mapView addAnnotation:item];
- }
- }
- //轨迹点
- 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构建BMKPolyline
- BMKPolyline* polyLine = [BMKPolyline polylineWithPoints:temppoints count:planPointCounts];
- [_mapView addOverlay:polyLine]; // 添加路线overlay
- delete []temppoints;
- [self mapViewFitPolyLine:polyLine];
- }
- }
- - (void)onGetWalkingRouteResult:(BMKRouteSearch*)searcher result:(BMKWalkingRouteResult*)result errorCode:(BMKSearchErrorCode)error
- {
- NSArray* array = [NSArray arrayWithArray:_mapView.annotations];
- [_mapView removeAnnotations:array];
- array = [NSArray arrayWithArray:_mapView.overlays];
- [_mapView removeOverlays:array];
- NSLog(@"%d",error);
- if (error == BMK_SEARCH_NO_ERROR) {
- BMKWalkingRouteLine* plan = (BMKWalkingRouteLine*)[result.routes objectAtIndex:0];
- NSInteger size = [plan.steps count];
- int planPointCounts = 0;
- for (int i = 0; i < size; i++) {
- BMKWalkingStep* transitStep = [plan.steps objectAtIndex:i];
- if(i==0){
- RouteAnnotation* item = [[RouteAnnotation alloc]init];
- item.coordinate = plan.starting.location;
- item.title = @"起点";
- item.type = 0;
- [_mapView addAnnotation:item]; // 添加起点标注
- }else if(i==size-1){
- RouteAnnotation* item = [[RouteAnnotation alloc]init];
- item.coordinate = plan.terminal.location;
- item.title = @"终点";
- item.type = 1;
- [_mapView addAnnotation:item]; // 添加起点标注
- }
- //添加annotation节点
- 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];
- //轨迹点总数累计
- planPointCounts += transitStep.pointsCount;
- }
- //轨迹点
- BMKMapPoint * temppoints = new BMKMapPoint[planPointCounts];
- int i = 0;
- for (int j = 0; j < size; j++) {
- BMKWalkingStep* 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构建BMKPolyline
- BMKPolyline* polyLine = [BMKPolyline polylineWithPoints:temppoints count:planPointCounts];
- [_mapView addOverlay:polyLine]; // 添加路线overlay
- delete []temppoints;
- [self mapViewFitPolyLine:polyLine];
- }
- }
- /**
- *返回骑行搜索结果
- *@param searcher 搜索对象
- *@param result 搜索结果,类型为BMKRidingRouteResult
- *@param error 错误号,@see BMKSearchErrorCode
- */
- //- (void)onGetRidingRouteResult:(BMKRouteSearch *)searcher result:(BMKRidingRouteResult *)result errorCode:(BMKSearchErrorCode)error {
- // NSLog(@"onGetRidingRouteResult error:%d", (int)error);
- // NSArray* array = [NSArray arrayWithArray:_mapView.annotations];
- // [_mapView removeAnnotations:array];
- // array = [NSArray arrayWithArray:_mapView.overlays];
- // [_mapView removeOverlays:array];
- // if (error == BMK_SEARCH_NO_ERROR) {
- // BMKRidingRouteLine* plan = (BMKRidingRouteLine*)[result.routes objectAtIndex:0];
- // NSInteger size = [plan.steps count];
- // int planPointCounts = 0;
- // for (int i = 0; i < size; i++) {
- // BMKRidingStep* transitStep = [plan.steps objectAtIndex:i];
- // if (i == 0) {
- // RouteAnnotation* item = [[RouteAnnotation alloc]init];
- // item.coordinate = plan.starting.location;
- // item.title = @"起点";
- // item.type = 0;
- // [_mapView addAnnotation:item]; // 添加起点标注
- // } else if(i==size-1){
- // RouteAnnotation* item = [[RouteAnnotation alloc]init];
- // item.coordinate = plan.terminal.location;
- // item.title = @"终点";
- // item.type = 1;
- // [_mapView addAnnotation:item]; // 添加起点标注
- // }
- // //添加annotation节点
- // RouteAnnotation* item = [[RouteAnnotation alloc]init];
- // item.coordinate = transitStep.entrace.location;
- // item.title = transitStep.instruction;
- // item.degree = (int)transitStep.direction * 30;
- // item.type = 4;
- // [_mapView addAnnotation:item];
- //
- // //轨迹点总数累计
- // planPointCounts += transitStep.pointsCount;
- // }
- //
- // //轨迹点
- // BMKMapPoint * temppoints = new BMKMapPoint[planPointCounts];
- // int i = 0;
- // for (int j = 0; j < size; j++) {
- // BMKRidingStep* 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构建BMKPolyline
- // BMKPolyline* polyLine = [BMKPolyline polylineWithPoints:temppoints count:planPointCounts];
- // [_mapView addOverlay:polyLine]; // 添加路线overlay
- // delete []temppoints;
- // [self mapViewFitPolyLine:polyLine];
- // }
- //}
- #pragma mark ------------------------ 公交
- -(IBAction)onClickBusSearch
- {
- _seleIndex = 1;
- CLLocationCoordinate2D coor;
- coor.latitude = [SingleObject shareSingleObject].currentCoordinate.latitude;
- coor.longitude = [SingleObject shareSingleObject].currentCoordinate.longitude;
- BMKPlanNode* start = [[BMKPlanNode alloc]init];
- start.pt = coor;
- BMKPlanNode* end = [[BMKPlanNode alloc]init];
- end.pt = self.location;
- BMKTransitRoutePlanOption *transitRouteSearchOption = [[BMKTransitRoutePlanOption alloc]init];
- transitRouteSearchOption.city= [SingleObject shareSingleObject].selectedCityObject.cityName;
- transitRouteSearchOption.from = start;
- transitRouteSearchOption.to = end;
- BOOL flag = [_routesearch transitSearch:transitRouteSearchOption];
- if(flag)
- {
- NSLog(@"bus检索发送成功");
- }
- else
- {
- NSLog(@"bus检索发送失败");
- }
- }
- //-(IBAction)textFiledReturnEditing:(id)sender {
- // [sender resignFirstResponder];
- //}
- #pragma mark ------------------------------ 自驾
- -(IBAction)onClickDriveSearch
- {
- _seleIndex = 2;
- CLLocationCoordinate2D coor;
- coor.latitude = [SingleObject shareSingleObject].currentCoordinate.latitude;
- coor.longitude = [SingleObject shareSingleObject].currentCoordinate.longitude;
- BMKPlanNode* start = [[BMKPlanNode alloc]init];
- start.pt = coor;
- BMKPlanNode* end = [[BMKPlanNode alloc]init];
- end.pt = self.location;
- BMKDrivingRoutePlanOption *drivingRouteSearchOption = [[BMKDrivingRoutePlanOption alloc]init];
- drivingRouteSearchOption.from = start;
- drivingRouteSearchOption.to = end;
- BOOL flag = [_routesearch drivingSearch:drivingRouteSearchOption];
- if(flag)
- {
- NSLog(@"car检索发送成功");
- }
- else
- {
- NSLog(@"car检索发送失败");
- }
- }
- #pragma mark ------------------------------ 步行
- -(IBAction)onClickWalkSearch
- {
- _seleIndex = 3;
- CLLocationCoordinate2D coor;
- coor.latitude = [SingleObject shareSingleObject].currentCoordinate.latitude;
- coor.longitude = [SingleObject shareSingleObject].currentCoordinate.longitude;
- BMKPlanNode* start = [[BMKPlanNode alloc]init];
- start.pt = coor;
- BMKPlanNode* end = [[BMKPlanNode alloc]init];
- end.pt = self.location;
- BMKWalkingRoutePlanOption *walkingRouteSearchOption = [[BMKWalkingRoutePlanOption alloc]init];
- walkingRouteSearchOption.from = start;
- walkingRouteSearchOption.to = end;
- BOOL flag = [_routesearch walkingSearch:walkingRouteSearchOption];
- if(flag)
- {
- NSLog(@"walk检索发送成功");
- }
- else
- {
- NSLog(@"walk检索发送失败");
- }
- }
- #pragma mark ------------------------------ 导航
- - (IBAction)onClickRidingSearch:(id)sender {
- UIAlertView * aler = [[UIAlertView alloc]initWithTitle:@"请选择您要使用的导航地图" message:@"" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"百度地图",@"高德地图",@"系统(高德)地图", nil nil];
- [aler show];
- aler.tag = 1213;
- }
- - (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
- if (alertView.tag == 1213) {
- switch (buttonIndex) {
- case 0:
- {
- }
- break;
- case 1:
- {
- [self onDaoHangForBaiDuMap];
- }
- break;
- case 2:
- {
- [self onDaoHangForGaoDeMap];
- }
- break;
- case 3:
- {
- [self onDaoHangForIOSMap];
- }
- break;
- default:
- break;
- }
- }
- }
- #pragma mark ------------------------------ 导航 - 百度
- -(void) onDaoHangForBaiDuMap
- {
- // 百度地图如何调起APP进行导航
- // mode 导航模式,固定为transit、driving、walking,分别表示公交、驾车和步行
- NSString * modeBaiDu = @"driving";
- switch (_seleIndex) {
- case 1:
- {
- modeBaiDu = @"transit";
- }
- break;
- case 2:
- {
- modeBaiDu = @"driving";
- }
- break;
- case 3:
- {
- modeBaiDu = @"walking";
- }
- break;
- default:
- break;
- }
- NSString *url = [[NSString stringWithFormat:@"baidumap://map/direction?origin=%lf,%lf&destination=%f,%f&mode=%@&src=公司|APP",[SingleObject shareSingleObject].currentCoordinate.latitude,[SingleObject shareSingleObject].currentCoordinate.longitude,self.location.latitude,self.location.longitude,modeBaiDu] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] ;
- // [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
- if ([[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]])// -- 使用 canOpenURL:[NSURL URLWithString:@"baidumap://"] 判断不明白为什么为否。
- {
- [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
- }else{
- [[[UIAlertView alloc]initWithTitle:@"没有安装百度地图" message:@"" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil nil] show];
- }
- }
- #pragma mark ------------------------------ 导航 - 高德
- -(void) onDaoHangForGaoDeMap
- {
- // m 驾车:0:速度最快,1:费用最少,2:距离最短,3:不走高速,4:躲避拥堵,5:不走高速且避免收费,6:不走高速且躲避拥堵,7:躲避收费和拥堵,8:不走高速躲避收费和拥堵 公交:0:最快捷,2:最少换乘,3:最少步行,5:不乘地铁 ,7:只坐地铁 ,8:时间短 是
- // t = 0:驾车 =1:公交 =2:步行
- NSString * t = @"0";
- switch (_seleIndex) {
- case 1:
- {
- t = @"1";
- }
- break;
- case 2:
- {
- t = @"0";
- }
- break;
- case 3:
- {
- t = @"2";
- }
- break;
- default:
- break;
- }
- //起点
- CLLocation * location = [[CLLocation alloc]initWithLatitude:[SingleObject shareSingleObject].currentCoordinate.latitude longitude:[SingleObject shareSingleObject].currentCoordinate.longitude];
- location = [location locationMarsFromBaidu];
- CLLocationCoordinate2D coor =location.coordinate;
- //目的地的位置
- CLLocation * location2 = [[CLLocation alloc]initWithLatitude:self.location.latitude longitude:self.location.longitude];
- location2 = [location2 locationMarsFromBaidu];
- CLLocationCoordinate2D coor2 =location2.coordinate;
- // 导航 URL:iosamap://navi?sourceApplication=%@&poiname=%@&lat=%lf&lon=%lf&dev=0&style=0",@"APP"
- // 路径规划 URL:iosamap://path?sourceApplication=applicationName&sid=BGVIS1&slat=39.92848272&slon=116.39560823&sname=A&did=BGVIS2&dlat=39.98848272&dlon=116.47560823&dname=B&dev=0&m=0&t=0
- // -- 不能直接让用户进入导航,应该给用户更多的选择,所以先进行路径规划
- NSString *url = [[NSString stringWithFormat:@"iosamap://path?sourceApplication=applicationName&sid=BGVIS1&slat=%lf&slon=%lf&sname=我的位置&did=BGVIS2&dlat=%lf&dlon=%lf&dname=%@&dev=0&m=0&t=%@",coor.latitude,coor.longitude, coor2.latitude,coor2.longitude,self.mainTitle,t] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
- if ([[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]]) // -- 使用 canOpenURL:[NSURL URLWithString:@"iosamap://"] 判断不明白为什么为否。
- {
- [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
- }else{
- [[[UIAlertView alloc]initWithTitle:@"没有安装高德地图" message:@"" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil nil] show];
- }
- }
- #pragma mark ------------------------------ 导航 - iosMap
- -(void) onDaoHangForIOSMap
- {
- //起点
- CLLocation * location = [[CLLocation alloc]initWithLatitude:[SingleObject shareSingleObject].currentCoordinate.latitude longitude:[SingleObject shareSingleObject].currentCoordinate.longitude];
- location = [location locationMarsFromBaidu];
- CLLocationCoordinate2D coor =location.coordinate;
- MKMapItem *currentLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:coor addressDictionary:nil]];
- currentLocation.name =@"我的位置";
- //目的地的位置
- CLLocation * location2 = [[CLLocation alloc]initWithLatitude:self.location.latitude longitude:self.location.longitude];
- location2 = [location2 locationMarsFromBaidu];
- CLLocationCoordinate2D coor2 =location2.coordinate;
- // CLLocationCoordinate2D coords = self.location;
- MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:coor2 addressDictionary:nil]];
- toLocation.name = self.mainTitle;
- NSArray *items = [NSArray arrayWithObjects:currentLocation, toLocation, nil nil];
- NSString * mode = MKLaunchOptionsDirectionsModeDriving;
- switch (_seleIndex) {
- case 1:
- {
- mode = MKLaunchOptionsDirectionsModeTransit;
- }
- break;
- case 2:
- {
- mode = MKLaunchOptionsDirectionsModeDriving;
- }
- break;
- case 3:
- {
- mode = MKLaunchOptionsDirectionsModeWalking;
- }
- break;
- default:
- break;
- }
- NSDictionary *options = @{ MKLaunchOptionsDirectionsModeKey:mode, MKLaunchOptionsMapTypeKey: [NSNumber numberWithInteger:MKMapTypeStandard], MKLaunchOptionsShowsTrafficKey:@YES };
- //打开苹果自身地图应用,并呈现特定的item
- [MKMapItem openMapsWithItems:items launchOptions:options];
- }
- #pragma mark - 私有
- - (NSString*)getMyBundlePath1:(NSString *)filename
- {
- NSBundle * libBundle = MYBUNDLE ;
- if ( libBundle && filename ){
- NSString * s=[[libBundle resourcePath ] stringByAppendingPathComponent : filename];
- return s;
- }
- return nil ;
- }
- - (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.png"]];
- 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.png"]];
- view.centerOffset = CGPointMake(0, -(view.frame.size.height * 0.5));
- view.canShowCallout = TRUE;
- }
- view.annotation = routeAnnotation;
- }
- break;
- case 2:
- {
- view = [mapview dequeueReusableAnnotationViewWithIdentifier:@"bus_node"];
- if (view == nil) {
- view = [[BMKAnnotationView alloc]initWithAnnotation:routeAnnotation reuseIdentifier:@"bus_node"];
- view.image = [UIImage imageWithContentsOfFile:[self getMyBundlePath1:@"images/icon_nav_bus.png"]];
- view.canShowCallout = TRUE;
- }
- view.annotation = routeAnnotation;
- }
- break;
- case 3:
- {
- view = [mapview dequeueReusableAnnotationViewWithIdentifier:@"rail_node"];
- if (view == nil) {
- view = [[BMKAnnotationView alloc]initWithAnnotation:routeAnnotation reuseIdentifier:@"rail_node"];
- view.image = [UIImage imageWithContentsOfFile:[self getMyBundlePath1:@"images/icon_nav_rail.png"]];
- 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.png"]];
- // view.image = [image imageRotatedByDegrees:routeAnnotation.degree];
- view.annotation = routeAnnotation;
- }
- break;
- case 5:
- {
- view = [mapview dequeueReusableAnnotationViewWithIdentifier:@"waypoint_node"];
- if (view == nil) {
- view = [[BMKAnnotationView alloc]initWithAnnotation:routeAnnotation reuseIdentifier:@"waypoint_node"];
- view.canShowCallout = TRUE;
- } else {
- [view setNeedsDisplay];
- }
- UIImage* image = [UIImage imageWithContentsOfFile:[self getMyBundlePath1:@"images/icon_nav_waypoint.png"]];
- view.image = [image imageRotatedByDegrees:routeAnnotation.degree];
- view.annotation = routeAnnotation;
- }
- break;
- default:
- break;
- }
- return view;
- }
- //
- 根据polyline设置地图范围
- - (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 = 1; 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;
- }
- @end
地图界面 XIB