2、進行線路導航
- (void)viewDidLoad {
[super viewDidLoad];
//地圖初始化設定
mapView =[[MKMapView alloc]initWithFrame:self.view.frame];
mapView.region=MKCoordinateRegionMake(CLLocationCoordinate2DMake(39.26, 116.3), MKCoordinateSpanMake(5, 5));
mapView.mapType=MKMapTypeStandard;
mapView.delegate=self;
[self.view addSubview:mapView];
//導航設定
CLLocationCoordinate2D fromcoor=CLLocationCoordinate2DMake(39.26, 116.3);
CLLocationCoordinate2D tocoor = CLLocationCoordinate2DMake(33.33, 113.33);
//建立出發點和目的點資訊
MKPlacemark *fromPlace = [[MKPlacemark alloc] initWithCoordinate:fromcoor
addressDictionary:nil];
MKPlacemark *toPlace = [[MKPlacemark alloc]initWithCoordinate:tocoor addressDictionary:nil];
//建立出發節點和目的地節點
MKMapItem * fromItem = [[MKMapItem alloc]initWithPlacemark:fromPlace];
MKMapItem * toItem = [[MKMapItem alloc]initWithPlacemark:toPlace];
//初始化導航搜尋請求
MKDirectionsRequest *request = [[MKDirectionsRequest alloc]init];
request.source=fromItem;
request.destination=toItem;
request.requestsAlternateRoutes=YES;
//初始化請求檢索
MKDirections *directions = [[MKDirections alloc]initWithRequest:request];
//開始檢索,結果會傳回在block中
[directions calculateDirectionsWithCompletionHandler:^(MKDirectionsResponse *response, NSError *error) {
if (error) {
NSLog(@"error:%@",error);
}else{
//提取導航線路結果中的一條線路
MKRoute *route =response.routes[0];
//将線路中的每一步詳情提取出來
NSArray * stepArray = [NSArray arrayWithArray:route.steps];
//進行周遊
for (int i=0; i<stepArray.count; i++) {
//線路的詳情節點
MKRouteStep * step = stepArray[i];
//在此節點處添加一個大頭針
MKPointAnnotation * point = [[MKPointAnnotation alloc]init];
point.coordinate=step.polyline.coordinate;
point.title=step.instructions;
point.subtitle=step.notice;
[mapView addAnnotation:point];
//将此段線路添加到地圖上
[mapView addOverlay:step.polyline];
}
}
}];
}
//地圖覆寫物的代理方法
-(MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay{
MKPolylineRenderer *renderer = [[MKPolylineRenderer alloc] initWithPolyline:overlay];
renderer.strokeColor = [UIColor redColor];
renderer.lineWidth = 4.0;
return renderer;
}
//标注的代理方法
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{
MKPinAnnotationView * view= [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@"anno"];
view.canShowCallout=YES;
return view;
}
效果如下:
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLicmbw5SOihjYiZmZmNWNyIGM2MDOwUTZ0UjZjdDMlJWMjZWOx8CX5d2bs92Yl1iclB3bsVmdlR2LcNWaw9CXt92Yu4GZjlGbh5yYjV3Lc9CX6MHc0RHaiojIsJye.png)
二、附近興趣點檢索
興趣點檢索的邏輯和導航線路檢索的邏輯相似,直接通過代碼來示範:
//建立一個位置資訊對象,第一個參數為經緯度,第二個為緯度檢索範圍,機關為米,第三個為經度檢索範圍,機關為米
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(tocoor, 5000, 5000);
//初始化一個檢索請求對象
MKLocalSearchRequest * req = [[MKLocalSearchRequest alloc]init];
//設定檢索參數
req.region=region;
//興趣點關鍵字
req.naturalLanguageQuery=@"hotal";
//初始化檢索
MKLocalSearch * ser = [[MKLocalSearch alloc]initWithRequest:req];
//開始檢索,結果傳回在block中
[ser startWithCompletionHandler:^(MKLocalSearchResponse *response, NSError *error) {
//興趣點節點數組
NSArray * array = [NSArray arrayWithArray:response.mapItems];
for (int i=0; i<array.count; i++) {
MKMapItem * item=array[i];
MKPointAnnotation * point = [[MKPointAnnotation alloc]init];
point.title=item.name;
point.subtitle=item.phoneNumber;
point.coordinate=item.placemark.coordinate;
[mapView addAnnotation:point];
}
}];