遇到一個需求,給出起點和終點的位置,需要在地圖中展示出來,并有導航功能。
首先需要把位置轉化成經緯度,這個可以使用系統提供的方法,需要引入框架#import <MapKit/MapKit.h>
,
// address為具體地址名稱
NSString *address;
[myGeocoder geocodeAddressString:oreillyAddress completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
if ([placemarks count] > 0 && error == nil) {
CLPlacemark *firstPlacemark = [placemarks objectAtIndex:0];
// coord即為得到到坐標
CLLocationCoordinate2D coord;
coord.latitude = firstPlacemark.location.coordinate.latitude;
coord.longitude = firstPlacemark.location.coordinate.longitude;
// 由于項目中使用到是百度地圖,需要把坐標轉化為百度地圖到坐標
CLLocationCoordinate2D baiduCoor;
baiduCoor = [self changeBaiDuGPS:coord];
} else if ([placemarks count] == 0 && error == nil) {
NSLog(@"Found no placemarks.");
} else if (error != nil) {
NSLog(@"An error occurred = %@", error);
}
}];
// 坐標轉化方法
- (CLLocationCoordinate2D)changeBaiDuGPS:(CLLocationCoordinate2D)coor {
//轉換國測局坐標(google地圖、soso地圖、aliyun地圖、mapabc地圖和amap地圖所用坐標)至百度坐標
NSDictionary* testdic = BMKConvertBaiduCoorFrom(coor,BMK_COORDTYPE_COMMON);
//轉換后的百度坐標
CLLocationCoordinate2D baiduCoor = BMKCoorDictionaryDecode(testdic);
return baiduCoor;
}
同樣通過上面的方法可以得到起始點的坐標和終點的坐標,把坐標展示到地圖上:
startPoint = [[BMKPointAnnotation alloc] init];
startPoint.coordinate = startCoor;
[_mapView addAnnotation:startPoint];
endPoint = [[BMKPointAnnotation alloc] init];
endPoint.coordinate = endCoor;
[_mapView addAnnotation:endPoint];
大頭針已經添加到地圖上了,下面需要自定義大頭針的樣式,需要添加百度地圖代理_mapView.delegate = self;
:
// BMKMapViewDelegate
- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id<BMKAnnotation>)annotation {
if (annotation == startPoint) {
NSString *AnnotationViewID = @"renameMark";
BMKAnnotationView *annotationView = (BMKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationViewID];
annotationView.frame = CGRectMake(0, 0, 30, 30);
if (annotationView == nil) {
annotationView = [[BMKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID];
annotationView.image = [UIImage imageNamed:@"map_start"];
}
return annotationView;
} else if (annotation == endPoint) {
NSString *AnnotationViewID = @"renameMark";
BMKAnnotationView *annotationView = (BMKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationViewID];
annotationView.frame = CGRectMake(0, 0, 30, 30);
if (annotationView == nil) {
annotationView = [[BMKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID];
annotationView.image = [UIImage imageNamed:@"map_end"];
}
return annotationView;
}
return nil;
}
自定義大頭針樣式
頁面展示基本上完成了,導航功能需要跳轉到相應app就可以,目前我只做了跳轉到百度地圖和系統自帶地圖,跳轉相應app需要在plist文件中設置
plist文件設置相應選項
跳轉選項使用
UIAlertController
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
[alertController addAction:[UIAlertAction actionWithTitle:@"使用蘋果地圖" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
[self jumpAppleMap];
}]];
[alertController addAction:[UIAlertAction actionWithTitle:@"使用百度地圖" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
[self jumpBaiduMap];
}]];
[alertController addAction: [UIAlertAction actionWithTitle: @"取消" style: UIAlertActionStyleCancel handler:nil]];
[self presentViewController: alertController animated: YES completion: nil];
跳轉到蘋果地圖,此時使用的坐標為系統根據文字轉換的坐標,不必使用百度地圖的坐標:
- (void)jumpAppleMap {
MKMapItem *startLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:iosStartCoor addressDictionary:nil]];
MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:iosEndCoor addressDictionary:nil]];
[MKMapItem openMapsWithItems:@[startLocation, toLocation] launchOptions:@{MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving, MKLaunchOptionsShowsTrafficKey: [NSNumber numberWithBool:YES]}];
}
跳轉到百度地圖,此時需要使用轉化后的百度地圖坐標:
- (void)jumpBaiduMap {
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"baidumap://map/"]]) {
NSString *urlString = [NSString stringWithFormat:@"baidumap://map/direction?origin=%f,%f&destination=%f,%f&&mode=driving",startCoor.latitude,startCoor.longitude,endCoor.latitude,endCoor.longitude];
NSURL *url = [NSURL URLWithString:urlString];
[[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
} else {
// 沒有安裝百度地圖,無法打開
}
}
這樣程序便會跳轉到相應APP,并進入到導航功能。