隨記(九)--確定兩個地點的經緯度,自制駕車路線並計算其距離


使用地圖,使用最多的也是百度地圖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,BMKTransitStep
22 @property (nonatomic, strong) NSArray* steps;
23 @end

將MBKDrivingRouteLine實例化,通過distance屬性獲取實際駕車路線距離

 

 長話段說,直接貼上我的代碼

在XXX.h文件中

 1 #import <UIKit/UIKit.h>
 2 #import "BMapKit.h"
 3 
 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 <MapKit/MapKit.h>
  5 
  6 #define MYBUNDLE_NAME @ "mapapi.bundle"
  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 }
 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 () <BMKMapViewDelegate, BMKRouteSearchDelegate>
 27 {
 28     BMKRouteSearch* _routesearch;
 29     BMKMapView *_mapView;
 30     UILabel *_distanceLab;
 31 }
 32 
 33 @end
 34 
 35 @implementation ServerRepairViewController
 36 
 37 - (void)viewDidLoad {
 38     [super viewDidLoad];
 39     
 40     self.title = @"外出服務";
 41 //    self.navigationItem.rightBarButtonItem = [[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     [self.view addSubview:_mapView];
 45     _routesearch = [[BMKRouteSearch alloc] init];
 46     
 47     _mapView.delegate = self;
 48     _routesearch.delegate = self;
 49     
 50     _distanceLab = [[UILabel alloc] initWithFrame:CGRectMake(0, K_ScreenHeight - 64 - 40, K_ScreenWidth, 40)];
 51     _distanceLab.backgroundColor = [UIColor whiteColor];
 52     _distanceLab.textColor = [UIColor blueColor];
 53     [self.view addSubview:_distanceLab];
 54     
 55     
 56     // 計算兩點之間直線距離
 57 //    CLLocationDistance  distance = BMKMetersBetweenMapPoints(BMKMapPointMake(self.startCoor.latitude, self.startCoor.longitude), BMKMapPointMake(self.endCoor.latitude, self.endCoor.longitude));
 58 
 59 }
 60 
 61 
 62 
 63 - (void)viewWillAppear:(BOOL)animated
 64 {
 65     [_mapView viewWillAppear];
 66     
 67     _mapView.delegate = self;
 68     _routesearch.delegate = self;
 69 
 70     [self driveSearchAction];
 71 }
 72 
 73 - (void)viewWillDisappear:(BOOL)animated
 74 {
 75 
 76     [_mapView viewWillDisappear];
 77     _mapView.delegate = nil;
 78     _routesearch.delegate = 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",self.startCoor.latitude, self.startCoor.longitude, self.endCoor.latitude, self.endCoor.longitude);
 95     
 96     BMKPlanNode *start = [[BMKPlanNode alloc] init];
 97     start.pt = self.startCoor;
 98     
 99     BMKPlanNode *end = [[BMKPlanNode alloc] init];
100     end.pt = self.endCoor;
101     end.name = self.addrName;
102     
103     BMKDrivingRoutePlanOption *drivingRoutePlanOption = [[BMKDrivingRoutePlanOption alloc] init];
104     drivingRoutePlanOption.from = start;
105     drivingRoutePlanOption.to = 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 - BMKMapViewDelegate
117 
118 - (BMKAnnotationView *)mapView:(BMKMapView *)view viewForAnnotation:(id <BMKAnnotation>)annotation
119 {
120     if ([annotation isKindOfClass:[RouteAnnotation class]]) {
121         return [self getRouteAnnotationView:view viewForAnnotation:(RouteAnnotation*)annotation];
122     }
123     return nil;
124 }
125 
126 - (BMKOverlayView*)mapView:(BMKMapView *)map viewForOverlay:(id<BMKOverlay>)overlay
127 {
128     if ([overlay isKindOfClass:[BMKPolyline class]]) {
129         BMKPolylineView* polylineView = [[BMKPolylineView alloc] initWithOverlay:overlay];
130         polylineView.fillColor = [[UIColor alloc] initWithRed:0 green:1 blue:1 alpha:1];
131         polylineView.strokeColor = [[UIColor alloc] initWithRed:0 green:0 blue:1 alpha:0.7];
132         polylineView.lineWidth = 3.0;
133         return polylineView;
134     }
135     return nil;
136 }
137 
138 
139 #pragma mark - BMKRouteSearchDelegate
140 
141 - (void)onGetDrivingRouteResult:(BMKRouteSearch*)searcher result:(BMKDrivingRouteResult*)result errorCode:(BMKSearchErrorCode)error
142 {
143     NSArray* array = [NSArray arrayWithArray:_mapView.annotations];
144     [_mapView removeAnnotations:array];
145     array = [NSArray arrayWithArray:_mapView.overlays];
146     [_mapView removeOverlays:array];
147     
148     if (error == BMK_SEARCH_NO_ERROR) {
149         BMKDrivingRouteLine* plan = (BMKDrivingRouteLine*)[result.routes objectAtIndex:0];
150         _distanceLab.text = [NSString stringWithFormat:@"總路線:%.2lf公里", plan.distance * 2 / 1000.0];
151         // 計算路線方案中的路段數目
152         NSInteger size = [plan.steps count];
153         int planPointCounts = 0;
154         for (int i = 0; i < size; i++) {
155             BMKDrivingStep* transitStep = [plan.steps objectAtIndex:i];
156             if(i==0){
157                 RouteAnnotation* item = [[RouteAnnotation alloc]init];
158                 item.coordinate = plan.starting.location;
159                 item.title = @"起點";
160                 item.type = 0;
161                 [_mapView addAnnotation:item]; // 添加起點標注
162                 
163             }else if(i==size-1){
164                 RouteAnnotation* item = [[RouteAnnotation alloc]init];
165                 item.coordinate = plan.terminal.location;
166                 item.title = @"終點";
167                 item.type = 1;
168                 [_mapView addAnnotation:item]; // 添加起點標注
169             }
170             //添加annotation節點
171             RouteAnnotation* item = [[RouteAnnotation alloc]init];
172             item.coordinate = transitStep.entrace.location;
173             item.title = transitStep.entraceInstruction;
174             item.degree = transitStep.direction * 30;
175             item.type = 4;
176             [_mapView addAnnotation:item];
177             
178             //軌跡點總數累計
179             planPointCounts += transitStep.pointsCount;
180         }
181         // 添加途經點
182         if (plan.wayPoints) {
183             for (BMKPlanNode* tempNode in plan.wayPoints) {
184                 RouteAnnotation* item = [[RouteAnnotation alloc]init];
185                 item = [[RouteAnnotation alloc]init];
186                 item.coordinate = tempNode.pt;
187                 item.type = 5;
188                 item.title = tempNode.name;
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 = [plan.steps objectAtIndex:j];
197             int k=0;
198             for(k=0;k<transitStep.pointsCount;k++) {
199                 temppoints[i].x = transitStep.points[k].x;
200                 temppoints[i].y = transitStep.points[k].y;
201                 i++;
202             }
203             
204         }
205         // 通過points構建BMKPolyline
206         BMKPolyline* polyLine = [BMKPolyline polylineWithPoints:temppoints count:planPointCounts];
207         [_mapView addOverlay:polyLine]; // 添加路線overlay
208         delete []temppoints;
209         [self mapViewFitPolyLine:polyLine];
210     }
211 }
212 
213 
214 #pragma mark - 私有
215 - (NSString*)getMyBundlePath1:(NSString *)filename
216 {
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*)routeAnnotation
226 {
227     BMKAnnotationView* view = nil;
228     switch (routeAnnotation.type) {
229         case 0:
230         {
231             view = [mapview dequeueReusableAnnotationViewWithIdentifier:@"start_node"];
232             if (view == nil) {
233                 view = [[BMKAnnotationView alloc]initWithAnnotation:routeAnnotation reuseIdentifier:@"start_node"];
234                 view.image = [UIImage imageWithContentsOfFile:[self getMyBundlePath1:@"images/icon_nav_start.png"]];
235                 view.centerOffset = CGPointMake(0, -(view.frame.size.height * 0.5));
236                 view.canShowCallout = TRUE;
237             }
238             view.annotation = 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                 view.image = [UIImage imageWithContentsOfFile:[self getMyBundlePath1:@"images/icon_nav_end.png"]];
247                 view.centerOffset = CGPointMake(0, -(view.frame.size.height * 0.5));
248                 view.canShowCallout = TRUE;
249             }
250             view.annotation = 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                 view.image = [UIImage imageWithContentsOfFile:[self getMyBundlePath1:@"images/icon_nav_bus.png"]];
259                 view.canShowCallout = TRUE;
260             }
261             view.annotation = 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                 view.image = [UIImage imageWithContentsOfFile:[self getMyBundlePath1:@"images/icon_nav_rail.png"]];
270                 view.canShowCallout = TRUE;
271             }
272             view.annotation = 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                 view.canShowCallout = TRUE;
281             } else {
282                 [view setNeedsDisplay];
283             }
284             
285             UIImage* image = [UIImage imageWithContentsOfFile:[self getMyBundlePath1:@"images/icon_direction.png"]];
286             view.image = [image imageRotatedByDegrees:routeAnnotation.degree];
287             view.annotation = 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                 view.canShowCallout = TRUE;
297             } else {
298                 [view setNeedsDisplay];
299             }
300             
301             UIImage* image = [UIImage imageWithContentsOfFile:[self getMyBundlePath1:@"images/icon_nav_waypoint.png"]];
302             view.image = [image imageRotatedByDegrees:routeAnnotation.degree];
303             view.annotation = 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 (polyLine.pointCount < 1) {
318         return;
319     }
320     BMKMapPoint pt = polyLine.points[0];
321     ltX = pt.x, ltY = pt.y;
322     rbX = pt.x, rbY = pt.y;
323     for (int i = 1; i < polyLine.pointCount; i++) {
324         BMKMapPoint pt = polyLine.points[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     }
338     BMKMapRect rect;
339     rect.origin = BMKMapPointMake(ltX , ltY);
340     rect.size = BMKMapSizeMake(rbX - ltX, rbY - ltY);
341     [_mapView setVisibleMapRect:rect];
342     _mapView.zoomLevel = _mapView.zoomLevel - 0.3;
343 }
344 @end

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM