#pragma mark--导航按钮
- (void)navButtonClick:(UIButton *)sender {
//检测装了哪些地图应用
NSURL *baiDuAppUrl = [NSURL URLWithString:@"baidumap://location?id=1"];
BOOL hasBaiDuMap = [[UIApplication sharedApplication]canOpenURL:baiDuAppUrl];
NSURL *gaoDeMapUrl = [NSURL URLWithString:@"iosamap://location?id=1"];
BOOL hasgaoDeMap = [[UIApplication sharedApplication]canOpenURL:gaoDeMapUrl];
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
[alertController addAction:action2];
if (hasBaiDuMap) {
UIAlertAction *action3 = [UIAlertAction actionWithTitle:@"使用百度地图导航" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
NSString *urlString = [[NSString stringWithFormat:@"baidumap://map/direction?origin=latlng:%f,%f|name:我的位置&destination=latlng:%f,%f|name:终点&mode=driving",self.mapView.userLocation.coordinate.latitude, self.mapView.userLocation.coordinate.longitude, self.poiList.poiLat.floatValue, self.poiList.poiLng.floatValue] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] ;
[[UIApplication sharedApplication]openURL:[NSURL URLWithString:urlString]];
}];
[alertController addAction:action3];
} if (hasgaoDeMap) {
UIAlertAction *action4 = [UIAlertAction actionWithTitle:@"使用高德地图导航" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
NSString *urlString = [[NSString stringWithFormat:@"iosamap://navi?sourceApplication=%@&backScheme=%@&lat=%f&lon=%f&dev=0&style=2",@"ChengMi",@"iosChengmi",self.poiList.poiLat.floatValue, self.poiList.poiLng.floatValue] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
}];
[alertController addAction:action4];
}
UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"使用苹果自带地图导航" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
MKMapItem *currentLocation = [MKMapItem mapItemForCurrentLocation];
//获取到目标位置的坐标
CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(self.poiList.poiLat.floatValue, self.poiList.poiLng.floatValue);
//MKMapItem的特点如下:是OC API 可以通过一个或者多个pins来打开地图,直接转至某个地方,定制地图的显示。
MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:coordinate addressDictionary:nil]];
//打开系统地图
[MKMapItem openMapsWithItems:@[currentLocation, toLocation]launchOptions:@{MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving,MKLaunchOptionsShowsTrafficKey: [NSNumber numberWithBool:YES]}];
}];
[alertController addAction:action1];
[self presentViewController:alertController animated:YES completion:nil];
}