#import#import#import "FKViewController.h"@interface FKViewController ()@property (strong, nonatomic) IBOutlet UITextField *destField;
@property (strong, nonatomic) IBOutlet MKMapView *mapView;
// 定義一個(gè)CLGeocoder對(duì)象示姿,該對(duì)象負(fù)責(zé)對(duì)用戶(hù)輸入的地址進(jìn)行解析
@property (nonatomic, strong) CLGeocoder* geocoder;
// 定義一個(gè)變量來(lái)保存導(dǎo)航路徑
@property (nonatomic, strong) MKPolyline* naviPath;
- (IBAction)navTapped:(id)sender;
- (IBAction)finishEdit:(id)sender;
@end
@implementation FKViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.geocoder = [[CLGeocoder alloc] init];
self.mapView.mapType = MKMapTypeStandard;
self.mapView.delegate = self;
self.mapView.zoomEnabled = YES;
// 設(shè)置地圖可滾動(dòng)
self.mapView.scrollEnabled = YES;
// 設(shè)置地圖不可旋轉(zhuǎn)
self.mapView.rotateEnabled = NO;
self.mapView.showsUserLocation = YES;
[self locateToLatitude:40.0431 longitude:116.3];
}
- (IBAction)navTapped:(id)sender
{
// 獲取用戶(hù)輸入的目標(biāo)地址
NSString* destStr = self.destField.text;
if (destStr != nil && destStr.length > 0)
{
// 解析目標(biāo)地址叶骨,獲取實(shí)際的經(jīng)度涛目、維度信息
[self.geocoder geocodeAddressString:destStr completionHandler:
^void (NSArray *placemarks, NSError *error)
{
/* 實(shí)際應(yīng)用中,此處如果發(fā)現(xiàn)placemarks集合包含多個(gè)元素,即表明根據(jù)該地址字符串
檢索到多個(gè)地址值,那么應(yīng)該顯示一個(gè)列表框讓用戶(hù)選擇目標(biāo)地址。
此處為了簡(jiǎn)化該示例秕重,直接使用第一個(gè)地址值作為目標(biāo)地址。
*/
if(placemarks.count > 0)
{
// 刪除上一次的導(dǎo)航路徑
[self.mapView removeOverlay:self.naviPath];
// 創(chuàng)建MKDirectionsRequest對(duì)象厉膀,作為查詢(xún)導(dǎo)航線路的請(qǐng)求
MKDirectionsRequest* request = [[MKDirectionsRequest alloc] init];
// 將當(dāng)前位置設(shè)置為導(dǎo)航的起始點(diǎn)
request.source = [MKMapItem mapItemForCurrentLocation];
// 獲取地址解析得到的第一個(gè)地址值
CLPlacemark* clPlacemark = placemarks[0];
MKPlacemark* mkPlacemark = [[MKPlacemark alloc]
initWithPlacemark:clPlacemark];
// 將解析得到的目標(biāo)地址設(shè)置為導(dǎo)航的結(jié)束點(diǎn)
request.destination = [[MKMapItem alloc]
initWithPlacemark:mkPlacemark];
// 以MKDirectionsRequest作為參數(shù)溶耘,創(chuàng)建MKDirections對(duì)象
MKDirections* directions = [[MKDirections alloc]
initWithRequest:request];
[directions calculateDirectionsWithCompletionHandler:
^void (MKDirectionsResponse *response, NSError *error)
{
// 獲取查詢(xún)返回的第一條線路
MKRoute* route = response.routes[0];
// 保存檢索得到的導(dǎo)航信息
self.naviPath = route.polyline;
// 將self.naviPath(MKPolyline對(duì)象)添加成覆蓋層
[self.mapView addOverlay:self.naviPath
level:MKOverlayLevelAboveLabels];
}];
}
else
{
// 使用提示框提醒用戶(hù)輸入的地址無(wú)效
[[[UIAlertView alloc] initWithTitle:@"提醒"
message:@"無(wú)法定位您輸入的地址,請(qǐng)重新輸入服鹅!" delegate:nil
cancelButtonTitle:@"確定" otherButtonTitles: nil]
show];
}
}];
}
}
- (IBAction)finishEdit:(id)sender
{
// 關(guān)閉文本框關(guān)聯(lián)的虛擬鍵盤(pán)
[sender resignFirstResponder];
}
- (void)locateToLatitude:(CGFloat)latitude longitude:(CGFloat)longitude
{
// 設(shè)置地圖中心的經(jīng)凳兵、緯度
CLLocationCoordinate2D center = {latitude , longitude};
// 設(shè)置地圖顯示的范圍,
MKCoordinateSpan span;
// 地圖顯示范圍越小菱魔,細(xì)節(jié)越清楚
span.latitudeDelta = 0.01;
span.longitudeDelta = 0.01;
// 創(chuàng)建MKCoordinateRegion對(duì)象留荔,該對(duì)象代表了地圖的顯示中心和顯示范圍。
MKCoordinateRegion region = {center,span};
// 設(shè)置當(dāng)前地圖的顯示中心和顯示范圍
[self.mapView setRegion:region animated:YES];
}
- (MKOverlayRenderer *)mapView:(MKMapView *)mapViewrendererForOverlay:(id)overlay
{
// 創(chuàng)建MKPolylineRenderer對(duì)象澜倦,該對(duì)象負(fù)責(zé)繪制MKPolyline覆蓋層控件
MKPolylineRenderer* renderer = [[MKPolylineRenderer alloc]
initWithPolyline:overlay];
// 設(shè)置MKPolylineRenderer對(duì)象的線條顏色
renderer.strokeColor = [UIColor blueColor];
// 設(shè)置MKPolylineRenderer的線寬
renderer.lineWidth = 2;
return renderer;
}
@end