標籤:
現在很多的APP 都開始引入了地圖和定位功能,包括一些餐飲業,團購等。他們都過定位和地圖來讓使用者更加方便的根據自己的位置找到合適的目標,也就是說,現在地圖定位已經不再是導航工具類,地圖工具類所特有的了,本文將著重介紹下如何自行導航。運行如下:
<一>通過故事版添加地圖介面,以及導覽按鈕(由於操作過於簡單本文不便列出)
<二>要想完成導航必須要瞭解起始的位置,不然無法完成導航,本文採用兩個特殊的地名作為例子,實際開發中可以根據網路定位當前的位置,這裡我們引入類CLGeocoder來進行地理編碼,通過地理編碼來擷取我們的有用資訊。
// 兩地名稱,代碼如下NSString * sourceName=@"新疆";NSString * destinationName=@"煙台";[self.geocoder geocodeAddressString:sourceName completionHandler:^(NSArray *sourcemarks, NSError *error) { [self.geocoder geocodeAddressString:destinationName completionHandler:^(NSArray *destmarks, NSError *error) {CLPlacemark * sourceMark=[sourcemarks firstObject]; CLPlacemark * toMark=[destmarks firstObject]; }];}];
<三>添加起始地和目的地圖釘並將自訂圖釘代碼寫在上面地理編碼完成之後的代碼塊裡面
增加自訂圖釘
JRAnnotation * source=[[JRAnnotation alloc] init]; source.title=sourceName; source.subtitle=sourceMark.name; source.coordinate=sourceMark.location.coordinate; [self.mapView addAnnotation:source]; JRAnnotation * dest=[[JRAnnotation alloc] init]; dest.title=destinationName; dest.subtitle=toMark.name; dest.coordinate=toMark.location.coordinate;[self.mapView addAnnotation:dest];
:
<四>進行導航之前的劃線,此代碼我抽取了個方法,仍然是要在上面的地理編碼完成塊裡面調用
#pragma mark - 導航之前劃線- (void)_mapGuilderFromMark:(CLPlacemark * ) sourceMark toMark:(CLPlacemark *) destMark{ //1 定義方向請求 MKDirectionsRequest * request=[[MKDirectionsRequest alloc] init]; //2 定義開始和結束位置 //1> 開始 MKPlacemark *sourcemkpm=[[MKPlacemark alloc] initWithPlacemark:sourceMark]; MKMapItem * sourceItem=[[MKMapItem alloc] initWithPlacemark:sourcemkpm]; request.source=sourceItem; self.sourceItem=sourceItem; //2> 結束 MKPlacemark *destmkpm=[[MKPlacemark alloc] initWithPlacemark:destMark]; MKMapItem * destItem=[[MKMapItem alloc] initWithPlacemark:destmkpm]; request.destination=destItem; self.destItem=destItem; //3 根據方向請求擷取方向 MKDirections *dirction=[[MKDirections alloc] initWithRequest:request]; //4 計算路線模型 [dirction calculateDirectionsWithCompletionHandler:^(MKDirectionsResponse *response, NSError *error) { if(error) return ; NSArray * routesArray=response.routes; for (MKRoute * root in routesArray) { //添加路線遮蓋,傳遞路線遮蓋模型 [self.mapView addOverlay:root.polyline]; } }];}
:
<五>調用蘋果內建的地圖進行導航,當點擊導覽按鈕的時候調用
#pragma mark - 導航- (IBAction)beginGuiding:(id)sender { //1 設定起始item NSArray * array=@[self.sourceItem,self.destItem]; //2 設定導航模式,走路還是開車,以及是否顯示路況 NSDictionary * dic=@{MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeDriving,MKLaunchOptionsShowsTrafficKey:@YES}; //3 開啟蘋果地圖開始導航 [MKMapItem openMapsWithItems:array launchOptions:dic]; }
另外附兩個渲染器代理方法
#pragma mark - mapViewDelegate//返回遮蓋渲染器-(MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay{ MKPolylineRenderer * render=[[MKPolylineRenderer alloc]initWithPolyline:overlay]; render.lineWidth=5; render.strokeColor=[UIColor blueColor]; return render;}//返回圖釘渲染器- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{ static NSString * identy=@"big"; MKPinAnnotationView * pinView=(MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:identy]; if(pinView==nil){ pinView=[[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:identy]; pinView.canShowCallout=YES;//設定點擊出明細 } pinView.pinColor=MKPinAnnotationColorGreen;//設定圖釘顏色 return pinView;}
想要進一步瞭解的同學,可以點擊查看原始碼,親自運行體驗!
傑瑞教育
出處:http://www.cnblogs.com/jerehedu/
本文著作權歸煙台傑瑞教育科技有限公司和部落格園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文串連,否則保留追究法律責任的權利。
IOS開發之地圖導航