// // ICRNavigationViewController.m // XFFruit // // Created by freecui on 15/4/24. // Copyright (c) 2015年 Xummer. All rights reserved. // #import "ICRNavigationViewController.h" #import "UIImage+Rotate.h" #import "BMapKit.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 BMKMapManager* _mapManager; @implementation RouteAnnotation @synthesize type = _type; @synthesize degree = _degree; @end @implementation ICRNavigationViewController - (NSString*)getMyBundlePath1:(NSString *)filename { NSBundle * libBundle = MYBUNDLE ; if ( libBundle && filename ){ NSString * s=[[libBundle resourcePath ] stringByAppendingPathComponent : filename]; return s; } return nil ; } - (void)mylocation{ NSLog(@"进入普通定位态"); [_locService startUserLocationService]; _mapView.showsUserLocation = NO;//先关闭显示的定位图层 _mapView.userTrackingMode = BMKUserTrackingModeNone;//设置定位的状态 _mapView.showsUserLocation = YES;//显示定位图层 } /** *在地图View将要启动定位时,会调用此函数 *@param mapView 地图View */ - (void)willStartLocatingUser { NSLog(@"start locate"); //_locService.userLocation } /** *用户位置更新后,会调用此函数 *@param userLocation 新的用户位置 */ - (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation{ NSLog(@"UserLocation :%@",userLocation.location); CLLocation *myPosition = userLocation.location; _mylocationLongitude = [NSString stringWithFormat:@"%f",myPosition.coordinate.longitude]; _mylocationLatitude = [NSString stringWithFormat:@"%f",myPosition.coordinate.latitude]; [self reverseGeocode]; [_locService stopUserLocationService]; } // Implement viewDidLoad to do additional setup after loading the view, typically from a nib. - (void)viewDidLoad { // //UIBarButtonItem *leftBtn = [[UIBarButtonItem alloc] // initWithTitle:@"折扣信息" style:UIBarButtonItemStyleBordered // target:nil action:nil]; // // self.navigationItem.leftBarButtonItem = leftBtn; self.title = @"路径规划"; _btnBusSearch.backgroundColor = ICR_GRAY_BTN_COLOR; _btnDriveSearch.backgroundColor = ICR_GRAY_BTN_COLOR; _btnWalkSearch.backgroundColor = ICR_GRAY_BTN_COLOR; CGFloat mapViewY = CGRectGetMaxY(_btnBusSearch.frame) + 10 + 64; CGFloat mapViewH = CGRectGetHeight([UIScreen mainScreen].bounds) - mapViewY; BMKMapView* mapView = [[BMKMapView alloc]initWithFrame:CGRectMake(0,mapViewY, CGRectGetWidth(self.view.frame), mapViewH)]; _mapView = mapView; [[UIApplication sharedApplication].keyWindow addSubview:_mapView]; [super viewDidLoad]; //适配ios7 if( ([[[UIDevice currentDevice] systemVersion] doubleValue]>=7.0)) { // self.edgesForExtendedLayout=UIRectEdgeNone; self.navigationController.navigationBar.translucent = NO; } _locService = [[BMKLocationService alloc]init]; _routesearch = [[BMKRouteSearch alloc]init]; _geocodesearch = [[BMKGeoCodeSearch alloc]init]; _startAddrText.text = @"我的位置"; // _endAddrText.text = @"百度大厦"; _endAddrText.text = _m_currentStore.name; UIBarButtonItem* btnWayPoint = [[UIBarButtonItem alloc]init]; btnWayPoint.target = self; btnWayPoint.action = @selector(wayPointDemo); // btnWayPoint.title = @"途经点"; btnWayPoint.enabled=TRUE; self.navigationController.topViewController.navigationItem.rightBarButtonItem = btnWayPoint; //[self mylocation]; } -(void)viewWillAppear:(BOOL)animated { [_mapView viewWillAppear]; _mapView.delegate = self; // 此处记得不用的时候需要置nil,否则影响内存的释放 _routesearch.delegate = self; // 此处记得不用的时候需要置nil,否则影响内存的释放 _locService.delegate = self; _geocodesearch.delegate = self; // 此处记得不用的时候需要置nil,否则影响内存的释放 [self mylocation]; } -(void)viewWillDisappear:(BOOL)animated { [_mapView viewWillDisappear]; _mapView.delegate = nil; // 不用时,置nil _routesearch.delegate = nil; // 不用时,置nil _locService.delegate = nil; _geocodesearch.delegate = nil; // 不用时,置nil [_mapView removeFromSuperview]; } - (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 (_geocodesearch != nil) { _geocodesearch = 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]; } - (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; } - (BMKAnnotationView *)mapView:(BMKMapView *)view viewForAnnotation:(id )annotation { if ([annotation isKindOfClass:[RouteAnnotation class]]) { return [self getRouteAnnotationView:view viewForAnnotation:(RouteAnnotation*)annotation]; } return nil; } - (BMKOverlayView*)mapView:(BMKMapView *)map viewForOverlay:(id)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; } - (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]; if (error == BMK_SEARCH_NO_ERROR) { BMKTransitRouteLine* plan = (BMKTransitRouteLine*)[result.routes objectAtIndex:0]; // 计算路线方案中的路段数目 int 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