(八十二)利用苹果服务器获取导航信息和绘制路径
要通過蘋果的服務器獲得導航數據,利用系統自帶的類即可實現,先創建MKDirectionsRequest,然后利用request創建MKDirections,最后調用MKDirection對象的calculateDirectionsWithCompletionHandler:方法,該方法通過一個結構體回調,來獲取導航信息。
結構體傳入的參數是MKDirectionsResponse對象,其中routes包含的是路徑,一般只有一條,就是從起點通往終點的路線,在route里還包含step,每個step是路徑中的一小部分,利用for-in進行遍歷即可。
需要注意的是,request在請求之前需要傳入起點和終點,屬性名為source和destination,接收MKMapItem對象,要得到MKMapItem,應該先得到MKPlacemark,要得到MKPlacemark,首先應當利用地理編碼得到CLPlacemark,然后利用CLPlacemark初始化MKPlacemark。這個在上一節有講,這里不再贅述。
- (void)startNavigation{MKPlacemark *startMrk = [[MKPlacemark alloc] initWithPlacemark:_startMrk];MKPlacemark *endMrk = [[MKPlacemark alloc] initWithPlacemark:_endMrk];MKMapItem *startItem = [[MKMapItem alloc] initWithPlacemark:startMrk];MKMapItem *endItem = [[MKMapItem alloc] initWithPlacemark:endMrk];MKDirectionsRequest *request = [[MKDirectionsRequest alloc] init];request.source = startItem;request.destination = endItem;MKDirections *directions = [[MKDirections alloc] initWithRequest:request];[directions calculateDirectionsWithCompletionHandler:^(MKDirectionsResponse *response, NSError *error) {if(error){return;}for (MKRoute *route in response.routes) {NSLog(@"%@ %fkm %fh",route.name,route.distance / 1000,route.expectedTravelTime / 3600);// 繪制路線,通過向地圖上添加遮蓋(蒙板)// 系統開始繪制路徑時,會詢問路徑的屬性,寬度、顏色等。[_mapView addOverlay:route.polyline];for (MKRouteStep *step in route.steps) {NSLog(@"%@ %f",step.instructions,step.distance);}}}];} 觀察上面的代碼,對于路徑,可以通過mapView的addOverlay:方法傳入一個MKPolyline對象即可實現向地圖上添加路徑。
需要注意的是,繪制路徑之前系統會調用mapView的代理方法,詢問路徑繪制的細節,如果不實現這個方法,無法繪制路徑,首先讓控制器成為mapView的代理,然后實現下面的方法,設置路徑樣式。
- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay{MKPolylineRenderer *pathRender = [[MKPolylineRenderer alloc] initWithOverlay:overlay];pathRender.lineWidth = 6;pathRender.lineJoin = kCGLineJoinRound;pathRender.strokeColor = [UIColor redColor];return pathRender;}
一般要在起點和終點放置大頭針,只需要定義模型遵循MKAnnotation協議,然后創建模型,調用mapView的addAnnotation:方法即可放置大頭針,樣式為默認。這個方法應該在地理編碼獲取到終點回調時添加,下面是完整的代碼。
// // ViewController.m // 通過蘋果服務器獲取導航信息 // // Created by 11 on 7/22/15. // Copyright (c) 2015 soulghost. All rights reserved. //#import "ViewController.h" #import <MapKit/MapKit.h> #import "MyAnnotation.h"@interface ViewController () <MKMapViewDelegate> @property (weak, nonatomic) IBOutlet UITextField *startView; @property (weak, nonatomic) IBOutlet UITextField *endView; @property (strong, nonatomic) CLGeocoder *gcoder; @property (strong, nonatomic) CLPlacemark *startMrk,*endMrk; @property (weak, nonatomic) IBOutlet MKMapView *mapView;@end@implementation ViewController- (void)viewDidLoad {[super viewDidLoad];_gcoder = [[CLGeocoder alloc] init];_mapView.delegate = self;}- (IBAction)startBtnClick:(id)sender {NSString *start = _startView.text;NSString *end = _endView.text;[_gcoder geocodeAddressString:start completionHandler:^(NSArray *placemarks, NSError *error) {if(placemarks.count) _startMrk = [placemarks firstObject];else return;[_gcoder geocodeAddressString:end completionHandler:^(NSArray *placemarks, NSError *error) {if(placemarks.count) _endMrk = [placemarks firstObject];else return;[self startNavigation];// 添加起點和終點的大頭針MyAnnotation *startAnnotation = [[MyAnnotation alloc] init];startAnnotation.title = _startMrk.locality;startAnnotation.subtitle = _startMrk.name;startAnnotation.coordinate = _startMrk.location.coordinate;[_mapView addAnnotation:startAnnotation];MyAnnotation *endAnnotation = [[MyAnnotation alloc] init];endAnnotation.title = _endMrk.locality;endAnnotation.subtitle = _endMrk.name;endAnnotation.coordinate = _endMrk.location.coordinate;[_mapView addAnnotation:endAnnotation];}];}];}- (void)startNavigation{MKPlacemark *startMrk = [[MKPlacemark alloc] initWithPlacemark:_startMrk];MKPlacemark *endMrk = [[MKPlacemark alloc] initWithPlacemark:_endMrk];MKMapItem *startItem = [[MKMapItem alloc] initWithPlacemark:startMrk];MKMapItem *endItem = [[MKMapItem alloc] initWithPlacemark:endMrk];MKDirectionsRequest *request = [[MKDirectionsRequest alloc] init];request.source = startItem;request.destination = endItem;MKDirections *directions = [[MKDirections alloc] initWithRequest:request];[directions calculateDirectionsWithCompletionHandler:^(MKDirectionsResponse *response, NSError *error) {if(error){return;}for (MKRoute *route in response.routes) {NSLog(@"%@ %fkm %fh",route.name,route.distance / 1000,route.expectedTravelTime / 3600);// 繪制路線,通過向地圖上添加遮蓋(蒙板)// 系統開始繪制路徑時,會詢問路徑的屬性,寬度、顏色等。[_mapView addOverlay:route.polyline];for (MKRouteStep *step in route.steps) {NSLog(@"%@ %f",step.instructions,step.distance);}}}];}// 繪制路徑的樣式 - (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay{MKPolylineRenderer *pathRender = [[MKPolylineRenderer alloc] initWithOverlay:overlay];pathRender.lineWidth = 6;pathRender.lineJoin = kCGLineJoinRound;pathRender.strokeColor = [UIColor redColor];return pathRender;}@end
轉載于:https://www.cnblogs.com/aiwz/p/6154117.html
總結
以上是生活随笔為你收集整理的(八十二)利用苹果服务器获取导航信息和绘制路径的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: jquery判断页面、图片是否加载完成
- 下一篇: 分组加密的工作模式