标签:
先去百度地图开发者中心申请APPkey:http://developer.baidu.com/map/index.php?title=iossdk ,下载百度地图的SDK。
导入SDK的framework文件,BaiduMapAPI_Base.framework是基础包,是必须要导入的,根据自己的需求导入需要的百度SDK的framework,新版的SDK已经可以选择下载哪些framework,减小了包的大小。
引入所需的系统库,在Xcode工程中引入CoreLocation.framework
QuartzCore.framework
OpenGLES.framework
SystemConfiguration.framework
CoreGraphics.framework
Security.framework
libsqlite3.0.tbd(xcode7以前为 libsqlite3.0.dylib)
CoreTelephony.framework
libstdc++.6.0.9.tbd(xcode7以前为libstdc++.6.0.9.dylib)。
根据需求引入头文件,我们只要定位,搜索和路线绘制,所以不必要全部引入。
1 #import <BaiduMapAPI_Base/BMKBaseComponent.h>//引入base相关所有的头文件 2 3 #import <BaiduMapAPI_Map/BMKMapComponent.h>//引入地图功能所有的头文件 4 5 #import <BaiduMapAPI_Search/BMKSearchComponent.h>//引入检索功能所有的头文件 6 7 #import <BaiduMapAPI_Cloud/BMKCloudSearchComponent.h>//引入云检索功能所有的头文件 8 9 #import <BaiduMapAPI_Location/BMKLocationComponent.h>//引入定位功能所有的头文件 10 11 #import <BaiduMapAPI_Utils/BMKUtilsComponent.h>//引入计算工具所有的头文件 12 13 #import <BaiduMapAPI_Radar/BMKRadarComponent.h>//引入周边雷达功能所有的头文件 14 15 #import < BaiduMapAPI_Map/BMKMapView.h>//只引入所需的单个头文件
下面开始进行代码的实践:
首先引用代理方法:
BMKMapViewDelegate//基础代理 BMKRouteSearchDelegate//搜索代理 BMKLocationServiceDelegate//定位代理 BMKGeoCodeSearchDelegate//地理反编码代理
定义声明文件:
@interface RouteSearchDemoViewController : BaseViewCtrl<BMKMapViewDelegate, BMKRouteSearchDelegate,BMKLocationServiceDelegate,BMKGeoCodeSearchDelegate> { IBOutlet BMKMapView* _mapView;//地图view IBOutlet UITextField* _startCityText;//开始的城市 IBOutlet UITextField* _startAddrText;//开始的位置 IBOutlet UITextField* _endCityText;//终点城市 IBOutlet UITextField* _endAddrText;//终点位置 BMKRouteSearch* _routesearch;//搜索 BMKLocationService *locService;//定位 } -(IBAction)onClickBusSearch;//公交路线 -(IBAction)onClickDriveSearch;//开车路线 -(IBAction)onClickWalkSearch;//不行路线 - (IBAction)textFiledReturnEditing:(id)sender; @property(nonatomic,retain)MapAlertView *mapview;//一个遮盖view @end
使用的是类中使用了XIB文件,所以根据自己的需要进行控件的拖放、位置及大小设置。
在实现文件中添加一个图片出的函数,有些路线的绘制和图标的变化需要用到(百度的Demo中就有这个函数)
1 @implementation UIImage(InternalMethod) 2 3 - (UIImage*)imageRotatedByDegrees:(CGFloat)degrees 4 { 5 6 CGFloat width = CGImageGetWidth(self.CGImage); 7 CGFloat height = CGImageGetHeight(self.CGImage); 8 9 CGSize rotatedSize; 10 11 rotatedSize.width = width; 12 rotatedSize.height = height; 13 14 UIGraphicsBeginImageContext(rotatedSize); 15 CGContextRef bitmap = UIGraphicsGetCurrentContext(); 16 CGContextTranslateCTM(bitmap, rotatedSize.width/2, rotatedSize.height/2); 17 CGContextRotateCTM(bitmap, degrees * M_PI / 180); 18 CGContextRotateCTM(bitmap, M_PI); 19 CGContextScaleCTM(bitmap, -1.0, 1.0); 20 CGContextDrawImage(bitmap, CGRectMake(-rotatedSize.width/2, -rotatedSize.height/2, rotatedSize.width, rotatedSize.height), self.CGImage); 21 UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext(); 22 UIGraphicsEndImageContext(); 23 return newImage; 24 } 25 26 @end
下面就到了地图的真正实施阶段:
封装一个获取资源文件的函数,从百度的bundle中获取图片等等:
1 - (NSString*)getMyBundlePath1:(NSString *)filename 2 { 3 4 NSBundle * libBundle = MYBUNDLE ; 5 if ( libBundle && filename ){ 6 NSString * s=[[libBundle resourcePath ] stringByAppendingPathComponent : filename]; 7 return s; 8 } 9 return nil ; 10 }
在viewdidload中初始化定位等其他服务:
1 - (void)viewDidLoad { 2 [super viewDidLoad]; 3 //适配ios7 4 if( ([[[UIDevice currentDevice] systemVersion] doubleValue]>=7.0)) 5 { 6 self.navigationController.navigationBar.translucent = NO; 7 } 8 _routesearch = [[BMKRouteSearch alloc] init]; 9 _routesearch.delegate = self; 10 [_mapView setZoomEnabled:YES]; 11 [_mapView setZoomLevel:13];//级别,3-19 12 _mapView.showMapScaleBar = YES;//比例尺 13 _mapView.mapScaleBarPosition = CGPointMake(10,_mapView.frame.size.height-45);//比例尺的位置 14 _mapView.showsUserLocation=YES;//显示当前设备的位置 15 _mapView.userTrackingMode = BMKUserTrackingModeFollow;//定位跟随模式 16 _mapView.delegate = self; 17 [_mapView setMapType:BMKMapTypeStandard];//地图的样式(标准地图) 18 19 //定位 20 locService = [[BMKLocationService alloc] init]; 21 locService.delegate = self; 22 //启动LocationService 23 [locService startUserLocationService]; 24 }
开始定位,并且地理反编码,获取到位置信息:
1 /** 2 * 更新定位信息 3 * 4 * @param userLocation 获取到的位置数据 5 */ 6 -(void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation { 7 8 CLLocationCoordinate2D coordinate = userLocation.location.coordinate; 9 // 缩放 10 BMKCoordinateSpan span = BMKCoordinateSpanMake(0.1, 0.1); 11 BMKCoordinateRegion region = BMKCoordinateRegionMake(coordinate, span); 12 [_mapView setRegion:region]; 13 14 BMKReverseGeoCodeOption *option = [[BMKReverseGeoCodeOption alloc] init]; 15 option.reverseGeoPoint = CLLocationCoordinate2DMake(userLocation.location.coordinate.latitude, userLocation.location.coordinate.longitude); 16 BMKGeoCodeSearch *geoCode = [[BMKGeoCodeSearch alloc] init]; 17 geoCode.delegate = self; 18 [geoCode reverseGeoCode:option]; 19 [option release]; 20 [geoCode release]; 21 } 22 /** 23 * 反编码地理位置返回代理 24 * 25 * @param result 返回结果 26 */ 27 -(void)onGetReverseGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKReverseGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error { 28 if (result) { 29 _startCityText.text = result.addressDetail.city;; 30 _startAddrText.text = result.addressDetail.streetName; 31 //已经获取到经纬度,可以停止定位服务 32 [locService stopUserLocationService]; 33 } 34 }
经过上面的两个函数,已经可以获取到所在的城市跟位置地址,具体更加详细的信息,可以在addressDetail中看到,一般就是所在的区,街道,街道号码等。
在view上自己添加一个按钮,当点击的时候弹出一个自定义alertview,上面有三个按钮,分别是公交,行车,步行,并且添加一个手势,点击按钮之外的地方,remove或者隐藏alertview。
点击需要进行的导航线路绘制,有对应的搜索类:
1 -(IBAction)onClickBusSearch 2 { 3 if ([_startCityText.text isEqualToString:@""]) { 4 [self AddStatusLabelWithText:@"正在获取位置"]; 5 return; 6 } 7 [self.mapview removeFromSuperview]; 8 BMKPlanNode* start = [[[BMKPlanNode alloc]init] autorelease]; 9 start.name = _startAddrText.text; 10 BMKPlanNode* end = [[[BMKPlanNode alloc]init] autorelease]; 11 end.name = _endAddrText.text; 12 13 //公交 14 BMKTransitRoutePlanOption *transitRouteSearchOption = [[BMKTransitRoutePlanOption alloc]init]; 15 transitRouteSearchOption.city = _startCityText.text; 16 transitRouteSearchOption.from = start; 17 transitRouteSearchOption.to = end; 18 BOOL flag = [_routesearch transitSearch:transitRouteSearchOption]; 19 20 [transitRouteSearchOption release]; 21 if(flag) 22 { 23 NSLog(@"bus检索发送成功"); 24 } 25 else 26 { 27 NSLog(@"bus检索发送失败"); 28 } 29 } 30
35 -(IBAction)onClickDriveSearch 36 { 37 if ([_startCityText.text isEqualToString:@""]) { 38 [self AddStatusLabelWithText:@"正在获取位置"]; 39 return; 40 } 41 [self.mapview removeFromSuperview]; 42 BMKPlanNode* start = [[[BMKPlanNode alloc]init] autorelease]; 43 start.name = _startAddrText.text; 44 BMKPlanNode* end = [[[BMKPlanNode alloc]init] autorelease]; 45 end.name = _endAddrText.text; 46 47 /// 驾车查询基础信息类 48 BMKDrivingRoutePlanOption *drivingRouteSearchOption = [[BMKDrivingRoutePlanOption alloc]init]; 49 drivingRouteSearchOption.from = start; 50 drivingRouteSearchOption.to = end; 51 BOOL flag = [_routesearch drivingSearch:drivingRouteSearchOption]; 52 [drivingRouteSearchOption release]; 53 if(flag) 54 { 55 NSLog(@"car检索发送成功"); 56 } 57 else 58 { 59 NSLog(@"car检索发送失败"); 60 } 61 62 } 63 64 -(IBAction)onClickWalkSearch 65 { 66 if ([_startCityText.text isEqualToString:@""]) { 67 [self AddStatusLabelWithText:@"正在获取位置"]; 68 return; 69 } 70 [self.mapview removeFromSuperview]; 71 BMKPlanNode* start = [[[BMKPlanNode alloc]init] autorelease]; 72 start.name = _startAddrText.text; 73 BMKPlanNode* end = [[[BMKPlanNode alloc]init] autorelease]; 74 end.name = _endAddrText.text; 75 76 /// 步行查询基础信息类 77 BMKWalkingRoutePlanOption *walkingRouteSearchOption = [[BMKWalkingRoutePlanOption alloc]init]; 78 walkingRouteSearchOption.from = start; 79 walkingRouteSearchOption.to = end; 80 BOOL flag = [_routesearch walkingSearch:walkingRouteSearchOption]; 81 [walkingRouteSearchOption release]; 82 if(flag) 83 { 84 NSLog(@"walk检索发送成功"); 85 } 86 else 87 { 88 NSLog(@"walk检索发送失败"); 89 } 90 }
每个start的城市和地址都可以设置,其中公交线路的start城市和transitRouteSearchOption的城市是必须要有的,否则会检索失败或者检索不到线路;
发送检索成功后会回调对应的绘制线路代理方法:
1 - (void)onGetTransitRouteResult:(BMKRouteSearch*)searcher result:(BMKTransitRouteResult*)result errorCode:(BMKSearchErrorCode)error 2 { 3 MapExplainViewController *exp=[[[MapExplainViewController alloc]init]autorelease]; 4 exp.array=result.routes; 5 [self.navigationController pushViewController:exp animated:YES]; 6 NSArray* array = [NSArray arrayWithArray:_mapView.annotations]; 7 [_mapView removeAnnotations:array]; 8 array = [NSArray arrayWithArray:_mapView.overlays]; 9 [_mapView removeOverlays:array]; 10 if (error == BMK_SEARCH_NO_ERROR) { 11 BMKTransitRouteLine* plan = (BMKTransitRouteLine*)[result.routes objectAtIndex:0]; 12 // 计算路线方案中的路段数目 13 int size = [plan.steps count]; 14 int planPointCounts = 0; 15 for (int i = 0; i < size; i++) { 16 BMKTransitStep* transitStep = [plan.steps objectAtIndex:i]; 17 if(i==0){ 18 RouteAnnotation* item = [[RouteAnnotation alloc]init]; 19 item.coordinate = plan.starting.location; 20 item.title = @"起点"; 21 item.type = 0; 22 [_mapView addAnnotation:item]; // 添加起点标注 23 [item release]; 24 25 }else if(i==size-1){ 26 RouteAnnotation* item = [[RouteAnnotation alloc]init]; 27 item.coordinate = plan.terminal.location; 28 item.title = @"终点"; 29 item.type = 1; 30 [_mapView addAnnotation:item]; // 添加起点标注 31 [item release]; 32 } 33 RouteAnnotation* item = [[RouteAnnotation alloc]init]; 34 item.coordinate = transitStep.entrace.location; 35 item.title = transitStep.instruction; 36 item.type = 3; 37 [_mapView addAnnotation:item]; 38 [item release]; 39 40 //轨迹点总数累计 41 planPointCounts += transitStep.pointsCount; 42 } 43 44 //轨迹点 45 BMKMapPoint * temppoints = new BMKMapPoint[planPointCounts]; 46 int i = 0; 47 for (int j = 0; j < size; j++) { 48 BMKTransitStep* transitStep = [plan.steps objectAtIndex:j]; 49 int k=0; 50 for(k=0;k<transitStep.pointsCount;k++) { 51 temppoints[i].x = transitStep.points[k].x; 52 temppoints[i].y = transitStep.points[k].y; 53 i++; 54 } 55 56 } 57 // 通过points构建BMKPolyline 58 BMKPolyline* polyLine = [BMKPolyline polylineWithPoints:temppoints count:planPointCounts]; 59 [_mapView addOverlay:polyLine]; // 添加路线overlay 60 delete []temppoints; 61 } 62 63 } 64 //驾车的路线绘制 65 - (void)onGetDrivingRouteResult:(BMKRouteSearch*)searcher result:(BMKDrivingRouteResult*)result errorCode:(BMKSearchErrorCode)error 66 { 67 MapExplainViewController *exp=[[[MapExplainViewController alloc]init]autorelease]; 68 exp.array=result.routes; 69 [self.navigationController pushViewController:exp animated:YES]; 70 NSArray* array = [NSArray arrayWithArray:_mapView.annotations]; 71 [_mapView removeAnnotations:array]; 72 array = [NSArray arrayWithArray:_mapView.overlays]; 73 [_mapView removeOverlays:array]; 74 if (error == BMK_SEARCH_NO_ERROR) { 75 BMKDrivingRouteLine* plan = (BMKDrivingRouteLine*)[result.routes objectAtIndex:0]; 76 // 计算路线方案中的路段数目 77 int size = [plan.steps count]; 78 int planPointCounts = 0; 79 for (int i = 0; i < size; i++) { 80 BMKDrivingStep* transitStep = [plan.steps objectAtIndex:i]; 81 if(i==0){ 82 RouteAnnotation* item = [[RouteAnnotation alloc]init]; 83 item.coordinate = plan.starting.location; 84 item.title = @"起点"; 85 item.type = 0; 86 [_mapView addAnnotation:item]; // 添加起点标注 87 [item release]; 88 89 }else if(i==size-1){ 90 RouteAnnotation* item = [[RouteAnnotation alloc]init]; 91 item.coordinate = plan.terminal.location; 92 item.title = @"终点"; 93 item.type = 1; 94 [_mapView addAnnotation:item]; // 添加起点标注 95 [item release]; 96 } 97 //添加annotation节点 98 RouteAnnotation* item = [[RouteAnnotation alloc]init]; 99 item.coordinate = transitStep.entrace.location; 100 item.title = transitStep.entraceInstruction; 101 item.degree = transitStep.direction * 30; 102 item.type = 4; 103 [_mapView addAnnotation:item]; 104 [item release]; 105 //轨迹点总数累计 106 planPointCounts += transitStep.pointsCount; 107 } 108 // 添加途经点 109 if (plan.wayPoints) { 110 for (BMKPlanNode* tempNode in plan.wayPoints) { 111 RouteAnnotation* item = [[RouteAnnotation alloc]init]; 112 item = [[RouteAnnotation alloc]init]; 113 item.coordinate = tempNode.pt; 114 item.type = 5; 115 item.title = tempNode.name; 116 [_mapView addAnnotation:item]; 117 [item release]; 118 } 119 } 120 //轨迹点 121 BMKMapPoint * temppoints = new BMKMapPoint[planPointCounts]; 122 int i = 0; 123 for (int j = 0; j < size; j++) { 124 BMKDrivingStep* transitStep = [plan.steps objectAtIndex:j]; 125 int k=0; 126 for(k=0;k<transitStep.pointsCount;k++) { 127 temppoints[i].x = transitStep.points[k].x; 128 temppoints[i].y = transitStep.points[k].y; 129 i++; 130 } 131 132 } 133 // 通过points构建BMKPolyline 134 BMKPolyline* polyLine = [BMKPolyline polylineWithPoints:temppoints count:planPointCounts]; 135 [_mapView addOverlay:polyLine]; // 添加路线overlay 136 delete []temppoints; 137 138 139 } 140 } 141 //步行的路线绘制 142 - (void)onGetWalkingRouteResult:(BMKRouteSearch*)searcher result:(BMKWalkingRouteResult*)result errorCode:(BMKSearchErrorCode)error 143 { 144 MapExplainViewController *exp=[[[MapExplainViewController alloc]init] 145 autorelease]; 146 exp.array=result.routes; 147 [self.navigationController pushViewController:exp animated:YES]; 148 NSArray* array = [NSArray arrayWithArray:_mapView.annotations]; 149 [_mapView removeAnnotations:array]; 150 array = [NSArray arrayWithArray:_mapView.overlays]; 151 [_mapView removeOverlays:array]; 152 if (error == BMK_SEARCH_NO_ERROR) { 153 BMKWalkingRouteLine* plan = (BMKWalkingRouteLine*)[result.routes objectAtIndex:0]; 154 int size = [plan.steps count]; 155 int planPointCounts = 0; 156 for (int i = 0; i < size; i++) { 157 BMKWalkingStep* transitStep = [plan.steps objectAtIndex:i]; 158 if(i==0){ 159 RouteAnnotation* item = [[RouteAnnotation alloc]init]; 160 item.coordinate = plan.starting.location; 161 item.title = @"起点"; 162 item.type = 0; 163 [_mapView addAnnotation:item]; // 添加起点标注 164 [item release]; 165 166 }else if(i==size-1){ 167 RouteAnnotation* item = [[RouteAnnotation alloc]init]; 168 item.coordinate = plan.terminal.location; 169 item.title = @"终点"; 170 item.type = 1; 171 [_mapView addAnnotation:item]; // 添加起点标注 172 [item release]; 173 } 174 //添加annotation节点 175 RouteAnnotation* item = [[RouteAnnotation alloc]init]; 176 item.coordinate = transitStep.entrace.location; 177 item.title = transitStep.entraceInstruction; 178 item.degree = transitStep.direction * 30; 179 item.type = 4; 180 [_mapView addAnnotation:item]; 181 [item release]; 182 //轨迹点总数累计 183 planPointCounts += transitStep.pointsCount; 184 } 185 186 //轨迹点 187 BMKMapPoint * temppoints = new BMKMapPoint[planPointCounts]; 188 int i = 0; 189 for (int j = 0; j < size; j++) { 190 BMKWalkingStep* transitStep = [plan.steps objectAtIndex:j]; 191 int k=0; 192 for(k=0;k<transitStep.pointsCount;k++) { 193 temppoints[i].x = transitStep.points[k].x; 194 temppoints[i].y = transitStep.points[k].y; 195 i++; 196 } 197 198 } 199 // 通过points构建BMKPolyline 200 BMKPolyline* polyLine = [BMKPolyline polylineWithPoints:temppoints count:planPointCounts]; 201 [_mapView addOverlay:polyLine]; // 添加路线overlay 202 delete []temppoints; 203 } 204 }
搜索的回调代理成功后,需要在mapview 上绘制线条:
1 ///<0:起点 1:终点 2:公交 3:地铁 4:驾乘 5:途经点 2 - (BMKAnnotationView*)getRouteAnnotationView:(BMKMapView *)mapview viewForAnnotation:(RouteAnnotation*)routeAnnotation 3 { 4 BMKAnnotationView* view = nil; 5 switch (routeAnnotation.type) { 6 case 0: 7 { 8 view = [mapview dequeueReusableAnnotationViewWithIdentifier:@"start_node"]; 9 if (view == nil) { 10 view = [[[BMKAnnotationView alloc]initWithAnnotation:routeAnnotation reuseIdentifier:@"start_node"] autorelease]; 11 view.image = [UIImage imageWithContentsOfFile:[self getMyBundlePath1:@"images/icon_nav_start.png"]]; 12 view.centerOffset = CGPointMake(0, -(view.frame.size.height * 0.5)); 13 view.canShowCallout = TRUE; 14 } 15 view.annotation = routeAnnotation; 16 } 17 break; 18 case 1: 19 { 20 view = [mapview dequeueReusableAnnotationViewWithIdentifier:@"end_node"]; 21 if (view == nil) { 22 view = [[[BMKAnnotationView alloc]initWithAnnotation:routeAnnotation reuseIdentifier:@"end_node"] autorelease]; 23 view.image = [UIImage imageWithContentsOfFile:[self getMyBundlePath1:@"images/icon_nav_end.png"]]; 24 view.centerOffset = CGPointMake(0, -(view.frame.size.height * 0.5)); 25 view.canShowCallout = TRUE; 26 } 27 view.annotation = routeAnnotation; 28 } 29 break; 30 case 2: 31 { 32 view = [mapview dequeueReusableAnnotationViewWithIdentifier:@"bus_node"]; 33 if (view == nil) { 34 view = [[[BMKAnnotationView alloc]initWithAnnotation:routeAnnotation reuseIdentifier:@"bus_node"] autorelease]; 35 view.image = [UIImage imageWithContentsOfFile:[self getMyBundlePath1:@"images/icon_nav_bus.png"]]; 36 view.canShowCallout = TRUE; 37 } 38 view.annotation = routeAnnotation; 39 } 40 break; 41 case 3: 42 { 43 view = [mapview dequeueReusableAnnotationViewWithIdentifier:@"rail_node"]; 44 if (view == nil) { 45 view = [[[BMKAnnotationView alloc]initWithAnnotation:routeAnnotation reuseIdentifier:@"rail_node"] autorelease]; 46 view.image = [UIImage imageWithContentsOfFile:[self getMyBundlePath1:@"images/icon_nav_rail.png"]]; 47 view.canShowCallout = TRUE; 48 } 49 view.annotation = routeAnnotation; 50 } 51 break; 52 case 4: 53 { 54 view = [mapview dequeueReusableAnnotationViewWithIdentifier:@"route_node"]; 55 if (view == nil) { 56 view = [[[BMKAnnotationView alloc]initWithAnnotation:routeAnnotation reuseIdentifier:@"route_node"] autorelease]; 57 view.canShowCallout = TRUE; 58 } else { 59 [view setNeedsDisplay]; 60 } 61 62 UIImage* image = [UIImage imageWithContentsOfFile:[self getMyBundlePath1:@"images/icon_direction.png"]]; 63 view.image = [image imageRotatedByDegrees:routeAnnotation.degree]; 64 view.annotation = routeAnnotation; 65 66 } 67 break; 68 case 5: 69 { 70 view = [mapview dequeueReusableAnnotationViewWithIdentifier:@"waypoint_node"]; 71 if (view == nil) { 72 view = [[[BMKAnnotationView alloc]initWithAnnotation:routeAnnotation reuseIdentifier:@"waypoint_node"] autorelease]; 73 view.canShowCallout = TRUE; 74 } else { 75 [view setNeedsDisplay]; 76 } 77 78 UIImage* image = [UIImage imageWithContentsOfFile:[self getMyBundlePath1:@"images/icon_nav_waypoint.png"]]; 79 view.image = [image imageRotatedByDegrees:routeAnnotation.degree]; 80 view.annotation = routeAnnotation; 81 } 82 break; 83 default: 84 break; 85 } 86 87 return view; 88 } 89 90 - (BMKAnnotationView *)mapView:(BMKMapView *)view viewForAnnotation:(id <BMKAnnotation>)annotation 91 { 92 if ([annotation isKindOfClass:[RouteAnnotation class]]) { 93 return [self getRouteAnnotationView:view viewForAnnotation:(RouteAnnotation*)annotation]; 94 } 95 return nil; 96 } 97 //加载线路 98 - (BMKOverlayView*)mapView:(BMKMapView *)map viewForOverlay:(id<BMKOverlay>)overlay 99 { 100 if ([overlay isKindOfClass:[BMKPolyline class]]) { 101 BMKPolylineView* polylineView = [[[BMKPolylineView alloc] initWithOverlay:overlay] autorelease]; 102 polylineView.fillColor = [[UIColor cyanColor] colorWithAlphaComponent:1]; 103 polylineView.strokeColor = [[UIColor blueColor] colorWithAlphaComponent:0.7]; 104 polylineView.lineWidth = 3.0; 105 return polylineView; 106 } 107 return nil; 108 }
经过以上方法,基本上在mapview上就可以看到线路导航图,如果不行,可以那些错误枚举去查看原因,毕竟百度是中国的,所以SDK里的注释都是中文,对于英文渣渣的我来说,简直太幸福了。
还有一个地方使用了百度定位来获取天气信息,比系统的地理反编码快很多,很快就获取到了所在位置的城市。
同样的天气api使用的是百度天气,返回的数据里直接就有对应天气的图片的url,虽然图片很丑,但是总比没有的强,下面是百度天气的api地址:
1 -(void)WeatherRequest 2 { 3 [self ShowWaitView:@"正在加载中。。。"]; 4 //[GlobalData GetInstance].GB_CityString 是自己获取到城市 5 NSString *str = [[GlobalData GetInstance].GB_CityString URLEncoding]; 6 //用定位的城市请求 ak是自己申请的百度密钥 7 NSString *strurl=[NSString stringWithFormat:@"http://api.map.baidu.com/telematics/v3/weather?location=%@&output=json&ak=C3d2845360d091a5e8f42f605b7472ea",str]; 8 ASIFormDataRequest *weatherRequest = [[ASIFormDataRequest alloc] initWithURL:[NSURL URLWithString:strurl]]; 9 10 weatherRequest.delegate = self; 11 [weatherRequest startAsynchronous]; 12 }
标签:
原文地址:http://www.cnblogs.com/jw-blog/p/5001017.html