2023年7月4日发(作者:)
随记(九)--确定两个地点的经纬度,⾃制驾车路线并计算其距离使⽤地图,使⽤最多的也是百度地图API,以前使⽤百度地图只是表⾯地引⽤⼀下⽅法,很少点进⽅法或者对应属性的⽂件中,去查看其对应的说明与含义。1.百度地图两点之间的距离 1 /** 2
3 *计算指定两点之间的距离 4
5 *@param a 第⼀个坐标点 6
7 *@param b 第⼆个坐标点 8
9 *@return 两点之间的距离,单位:⽶10
11 */12
13 UIKIT_EXTERN CLLocationDistance BMKMetersBetweenMapPoints(BMKMapPoint a, BMKMapPoint b);BMKMetersBetweenMapPoints⽅法只是两个经纬度之间的直线距离
2.路线实际距离 1 ///此类表⽰⼀条驾车路线 2 @interface BMKDrivingRouteLine : BMKRouteLine{ 3 bool _isSupportTraffic;//从2.7.0开始,废弃 4 NSArray* _wayPoints; 5 } 6 ///该路线所在区域是否含有交通流量信息,从2.7.0开始,废弃 7 @property (nonatomic) bool isSupportTraffic; 8 ///路线途经点列表,成员类型为BMKPlanNode 9 @property (nonatomic, strong) NSArray* wayPoints;10 @end 1 ///此类表⽰路线数据结构的基类,表⽰⼀条路线,路线可能包括:路线规划中的换乘/驾车/步⾏路线 2 ///此类为路线数据结构的基类,⼀般关注其⼦类对象即可,⽆需直接⽣成该类对象 3 @interface BMKRouteLine : NSObject{ 4 int _distance; 5 BMKTime* _duration; 6 BMKRouteNode* _starting; 7 BMKRouteNode* _terminal; 8 NSString* _title; 9 NSArray* _steps;10 }11 ///路线长度 单位: ⽶12 @property (nonatomic) int distance;13 ///路线耗时 单位: 秒14 @property (nonatomic, strong) BMKTime* duration;15 ///路线起点信息16 @property (nonatomic, strong) BMKRouteNode* starting;17 ///路线终点信息18 @property (nonatomic, strong) BMKRouteNode* terminal;19 ///路线名称(预留字段,现为空)20 @property (nonatomic, strong) NSString* title;21 ///路线中的所有路段,成员类型为BMKWalkingStep,BMKDrivingStep,BMKTransitStep22 @property (nonatomic, strong) NSArray* steps;23 @end将MBKDrivingRouteLine实例化,通过distance属性获取实际驾车路线距离
长话段说,直接贴上我的代码在XXX.h⽂件中 1 #import
4 @interface XXXViewController : UIViewController 5
6 //@property (weak, nonatomic) IBOutlet BMKMapView *mapView; 7
8
9 @property (nonatomic, assign) CLLocationCoordinate2D startCoor;10 @property (nonatomic, assign) CLLocationCoordinate2D endCoor;11 @property (nonatomic, copy) NSString *addrName;12
13 @end
在XXX.m⽂件中 1 #import "XXX.h" 2 #import "UIImage+Rotate.h" 3
4 #import
6 #define MYBUNDLE_NAME @ "" 7 #define MYBUNDLE_PATH [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: MYBUNDLE_NAME] 8 #define MYBUNDLE [NSBundle bundleWithPath: MYBUNDLE_PATH] 9
10 @interface RouteAnnotation : BMKPointAnnotation 11 { 12 int _type; ///<0:起点 1:终点 2:公交 3:地铁 4:驾乘 5:途经点 13 int _degree; 14 } 14 } 15
16 @property (nonatomic) int type; 17 @property (nonatomic) int degree; 18 @end 19
20 @implementation RouteAnnotation 21
22 @synthesize type = _type; 23 @synthesize degree = _degree; 24 @end 25
26 @interface ServerRepairViewController ()
33 @end 34
35 @implementation ServerRepairViewController 36
37 - (void)viewDidLoad { 38 [super viewDidLoad]; 39
40 = @"外出服务"; 41 // arButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"驾车" style:UIBarButtonItemStylePlain target:self
action:@selector(driveSearchAction)]; 42
43 _mapView = [[BMKMapView alloc] initWithFrame:CGRectMake(0, 0, K_ScreenWidth, K_ScreenHeight - 64)]; 44 [ addSubview:_mapView]; 45 _routesearch = [[BMKRouteSearch alloc] init]; 46
47 _te = self; 48 _te = self; 49
50 _distanceLab = [[UILabel alloc] initWithFrame:CGRectMake(0, K_ScreenHeight - 64 - 40, K_ScreenWidth, 40)]; 51 _oundColor = [UIColor whiteColor]; 52 _lor = [UIColor blueColor]; 53 [ addSubview:_distanceLab]; 54
55
56 // 计算两点之间直线距离 57 // CLLocationDistance distance = BMKMetersBetweenMapPoints(BMKMapPointMake(de, ude),
BMKMapPointMake(de, ude)); 58
59 } 60
61
62
63 - (void)viewWillAppear:(BOOL)animated 64 { 65 [_mapView viewWillAppear]; 66
67 _te = self; 68 _te = self; 69
70 [self driveSearchAction]; 71 } 72
73 - (void)viewWillDisappear:(BOOL)animated 74 { 75
76 [_mapView viewWillDisappear]; 77 _te = nil; 77 _te = nil; 78 _te = nil; 79 } 80
81 -(void)dealloc 82 { 83 if (_mapView != nil) { 84 _mapView = nil; 85 } 86 if (_routesearch != nil) { 87 _routesearch = nil; 88 } 89 } 90
91
92 - (void)driveSearchAction 93 { 94 // NSLog(@"1 = %lf, %lf, %lf, %lf",de, ude, de, ude); 95
96 BMKPlanNode *start = [[BMKPlanNode alloc] init]; 97 = oor; 98
99 BMKPlanNode *end = [[BMKPlanNode alloc] init];100 = r;101 = me;102
103 BMKDrivingRoutePlanOption *drivingRoutePlanOption = [[BMKDrivingRoutePlanOption alloc] init];104 = start;105 = end;106
107 BOOL flag = [_routesearch drivingSearch:drivingRoutePlanOption];108 if (flag) {109 NSLog(@"car检索发送成功");110 } else {111 NSLog(@"car检索发送失败");112 }113 }114
115
116 #pragma mark - BMKMapViewDelegate117
118 - (BMKAnnotationView *)mapView:(BMKMapView *)view viewForAnnotation:(id
126 - (BMKOverlayView*)mapView:(BMKMapView *)map viewForOverlay:(id
138
139 #pragma mark - BMKRouteSearchDelegate140
141 - (void)onGetDrivingRouteResult:(BMKRouteSearch*)searcher result:(BMKDrivingRouteResult*)result errorCode:(BMKSearchErrorCode)error142 {142 {143 NSArray* array = [NSArray arrayWithArray:_tions];144 [_mapView removeAnnotations:array];145 array = [NSArray arrayWithArray:_ys];146 [_mapView removeOverlays:array];147
148 if (error == BMK_SEARCH_NO_ERROR) {149 BMKDrivingRouteLine* plan = (BMKDrivingRouteLine*)[ objectAtIndex:0];150 _ = [NSString stringWithFormat:@"总路线:%.2lf公⾥", ce * 2 / 1000.0];151 // 计算路线⽅案中的路段数⽬152 NSInteger size = [ count];153 int planPointCounts = 0;154 for (int i = 0; i < size; i++) {155 BMKDrivingStep* transitStep = [ objectAtIndex:i];156 if(i==0){157 RouteAnnotation* item = [[RouteAnnotation alloc]init];158 nate = on;159 = @"起点";160 = 0;161 [_mapView addAnnotation:item]; // 添加起点标注162
163 }else if(i==size-1){164 RouteAnnotation* item = [[RouteAnnotation alloc]init];165 nate = on;166 = @"终点";167 = 1;168 [_mapView addAnnotation:item]; // 添加起点标注169 }170 //添加annotation节点171 RouteAnnotation* item = [[RouteAnnotation alloc]init];172 nate = on;173 = eInstruction;174 = ion * 30;175 = 4;176 [_mapView addAnnotation:item];177
178 //轨迹点总数累计179 planPointCounts += Count;180 }181 // 添加途经点182 if (nts) {183 for (BMKPlanNode* tempNode in nts) {184 RouteAnnotation* item = [[RouteAnnotation alloc]init];185 item = [[RouteAnnotation alloc]init];186 nate = ;187 = 5;188 = ;189 [_mapView addAnnotation:item];190 }191 }192 //轨迹点193 BMKMapPoint * temppoints = new BMKMapPoint[planPointCounts];194 int i = 0;195 for (int j = 0; j < size; j++) {196 BMKDrivingStep* transitStep = [ objectAtIndex:j];197 int k=0;198 for(k=0;k 204 }205 // 通过points构建BMKPolyline206 BMKPolyline* polyLine = [BMKPolyline polylineWithPoints:temppoints count:planPointCounts];207 [_mapView addOverlay:polyLine]; // 添加路线overlay207 [_mapView addOverlay:polyLine]; // 添加路线overlay208 delete []temppoints;209 [self mapViewFitPolyLine:polyLine];210 }211 }212 213 214 #pragma mark - 私有215 - (NSString*)getMyBundlePath1:(NSString *)filename216 {217 NSBundle * libBundle = MYBUNDLE ;218 if ( libBundle && filename ){219 NSString * s=[[libBundle resourcePath ] stringByAppendingPathComponent : filename];220 return s;221 }222 return nil ;223 }224 225 - (BMKAnnotationView*)getRouteAnnotationView:(BMKMapView *)mapview viewForAnnotation:(RouteAnnotation*)routeAnnotation226 {227 BMKAnnotationView* view = nil;228 switch () {229 case 0:230 {231 view = [mapview dequeueReusableAnnotationViewWithIdentifier:@"start_node"];232 if (view == nil) {233 view = [[BMKAnnotationView alloc]initWithAnnotation:routeAnnotation reuseIdentifier:@"start_node"];234 = [UIImage imageWithContentsOfFile:[self getMyBundlePath1:@"images/icon_nav_"]];235 Offset = CGPointMake(0, -( * 0.5));236 wCallout = TRUE;237 }238 tion = routeAnnotation;239 }240 break;241 case 1:242 {243 view = [mapview dequeueReusableAnnotationViewWithIdentifier:@"end_node"];244 if (view == nil) {245 view = [[BMKAnnotationView alloc]initWithAnnotation:routeAnnotation reuseIdentifier:@"end_node"];246 = [UIImage imageWithContentsOfFile:[self getMyBundlePath1:@"images/icon_nav_"]];247 Offset = CGPointMake(0, -( * 0.5));248 wCallout = TRUE;249 }250 tion = routeAnnotation;251 }252 break;253 case 2:254 {255 view = [mapview dequeueReusableAnnotationViewWithIdentifier:@"bus_node"];256 if (view == nil) {257 view = [[BMKAnnotationView alloc]initWithAnnotation:routeAnnotation reuseIdentifier:@"bus_node"];258 = [UIImage imageWithContentsOfFile:[self getMyBundlePath1:@"images/icon_nav_"]];259 wCallout = TRUE;260 }261 tion = routeAnnotation;262 }263 break;264 case 3:265 {266 view = [mapview dequeueReusableAnnotationViewWithIdentifier:@"rail_node"];267 if (view == nil) {268 view = [[BMKAnnotationView alloc]initWithAnnotation:routeAnnotation reuseIdentifier:@"rail_node"];269 = [UIImage imageWithContentsOfFile:[self getMyBundlePath1:@"images/icon_nav_"]];270 wCallout = TRUE;271 }272 tion = routeAnnotation;272 tion = routeAnnotation;273 }274 break;275 case 4:276 {277 view = [mapview dequeueReusableAnnotationViewWithIdentifier:@"route_node"];278 if (view == nil) {279 view = [[BMKAnnotationView alloc]initWithAnnotation:routeAnnotation reuseIdentifier:@"route_node"];280 wCallout = TRUE;281 } else {282 [view setNeedsDisplay];283 }284 285 UIImage* image = [UIImage imageWithContentsOfFile:[self getMyBundlePath1:@"images/icon_"]];286 = [image imageRotatedByDegrees:];287 tion = routeAnnotation;288 289 }290 break;291 case 5:292 {293 view = [mapview dequeueReusableAnnotationViewWithIdentifier:@"waypoint_node"];294 if (view == nil) {295 view = [[BMKAnnotationView alloc]initWithAnnotation:routeAnnotation reuseIdentifier:@"waypoint_node"];296 wCallout = TRUE;297 } else {298 [view setNeedsDisplay];299 }300 301 UIImage* image = [UIImage imageWithContentsOfFile:[self getMyBundlePath1:@"images/icon_nav_"]];302 = [image imageRotatedByDegrees:];303 tion = routeAnnotation;304 }305 break;306 default:307 break;308 }309 310 return view;311 }312 313 314 //根据polyline设置地图范围315 - (void)mapViewFitPolyLine:(BMKPolyline *) polyLine {316 CGFloat ltX, ltY, rbX, rbY;317 if (ount < 1) {318 return;319 }320 BMKMapPoint pt = [0];321 ltX = pt.x, ltY = pt.y;322 rbX = pt.x, rbY = pt.y;323 for (int i = 1; i < ount; i++) {324 BMKMapPoint pt = [i];325 if (pt.x < ltX) {326 ltX = pt.x;327 }328 if (pt.x > rbX) {329 rbX = pt.x;330 }331 if (pt.y > ltY) {332 ltY = pt.y;333 }334 if (pt.y < rbY) {335 rbY = pt.y;336 }337 }337 }338 BMKMapRect rect;339 = BMKMapPointMake(ltX , ltY);340 = BMKMapSizeMake(rbX - ltX, rbY - ltY);341 [_mapView setVisibleMapRect:rect];342 _vel = _vel - 0.3;343 }344 @end
发布者:admin,转转请注明出处:http://www.yc00.com/xiaochengxu/1688421816a136014.html
评论列表(0条)