(八十二)利用苹果服务器获取导航信息和绘制路径
要通過(guò)蘋(píng)果的服務(wù)器獲得導(dǎo)航數(shù)據(jù),利用系統(tǒng)自帶的類(lèi)即可實(shí)現(xiàn),先創(chuàng)建MKDirectionsRequest,然后利用request創(chuàng)建MKDirections,最后調(diào)用MKDirection對(duì)象的calculateDirectionsWithCompletionHandler:方法,該方法通過(guò)一個(gè)結(jié)構(gòu)體回調(diào),來(lái)獲取導(dǎo)航信息。
結(jié)構(gòu)體傳入的參數(shù)是MKDirectionsResponse對(duì)象,其中routes包含的是路徑,一般只有一條,就是從起點(diǎn)通往終點(diǎn)的路線(xiàn),在route里還包含step,每個(gè)step是路徑中的一小部分,利用for-in進(jìn)行遍歷即可。
需要注意的是,request在請(qǐng)求之前需要傳入起點(diǎn)和終點(diǎn),屬性名為source和destination,接收MKMapItem對(duì)象,要得到MKMapItem,應(yīng)該先得到MKPlacemark,要得到MKPlacemark,首先應(yīng)當(dāng)利用地理編碼得到CLPlacemark,然后利用CLPlacemark初始化MKPlacemark。這個(gè)在上一節(jié)有講,這里不再贅述。
- (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);// 繪制路線(xiàn),通過(guò)向地圖上添加遮蓋(蒙板)// 系統(tǒng)開(kāi)始繪制路徑時(shí),會(huì)詢(xún)問(wèn)路徑的屬性,寬度、顏色等。[_mapView addOverlay:route.polyline];for (MKRouteStep *step in route.steps) {NSLog(@"%@ %f",step.instructions,step.distance);}}}];} 觀察上面的代碼,對(duì)于路徑,可以通過(guò)mapView的addOverlay:方法傳入一個(gè)MKPolyline對(duì)象即可實(shí)現(xiàn)向地圖上添加路徑。
需要注意的是,繪制路徑之前系統(tǒng)會(huì)調(diào)用mapView的代理方法,詢(xún)問(wèn)路徑繪制的細(xì)節(jié),如果不實(shí)現(xiàn)這個(gè)方法,無(wú)法繪制路徑,首先讓控制器成為mapView的代理,然后實(shí)現(xiàn)下面的方法,設(shè)置路徑樣式。
- (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;}
一般要在起點(diǎn)和終點(diǎn)放置大頭針,只需要定義模型遵循MKAnnotation協(xié)議,然后創(chuàng)建模型,調(diào)用mapView的addAnnotation:方法即可放置大頭針,樣式為默認(rèn)。這個(gè)方法應(yīng)該在地理編碼獲取到終點(diǎn)回調(diào)時(shí)添加,下面是完整的代碼。
// // ViewController.m // 通過(guò)蘋(píng)果服務(wù)器獲取導(dǎo)航信息 // // 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];// 添加起點(diǎn)和終點(diǎn)的大頭針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);// 繪制路線(xiàn),通過(guò)向地圖上添加遮蓋(蒙板)// 系統(tǒng)開(kāi)始繪制路徑時(shí),會(huì)詢(xún)問(wèn)路徑的屬性,寬度、顏色等。[_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
轉(zhuǎn)載于:https://www.cnblogs.com/aiwz/p/6154117.html
總結(jié)
以上是生活随笔為你收集整理的(八十二)利用苹果服务器获取导航信息和绘制路径的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: jquery判断页面、图片是否加载完成
- 下一篇: 分组加密的工作模式