- BOOL hasBaiduMap = NO;
- BOOL hasGaodeMap = NO;
- if ([[UIApplication sharedApplication]canOpenURL:[NSURL URLWithString:@"baidumap://map/"]]){
- hasBaiduMap = YES;
- }
- if ([[UIApplication sharedApplication]canOpenURL:[NSURL URLWithString:@"iosamap://"]]){
- hasGaodeMap = YES;
- }
- if ([@"使用百度地圖導航" isEqualToString:title])
- {
- NSString *urlString = [[NSString stringWithFormat:@"baidumap://map/direction?origin=latlng:%f,%f|name:我的位置&destination=latlng:%f,%f|name:終點&mode=driving",currentLat, currentLon,_shopLat,_shopLon] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] ;
- [[UIApplication sharedApplication]openURL:[NSURL URLWithString:urlString]];
- }
- else if ([@"使用高德地圖導航" isEqualToString:title])
- {
- NSString *urlString = [[NSString stringWithFormat:@"iosamap://navi?sourceApplication=%@&backScheme=%@&poiname=%@&lat=%f&lon=%f&dev=1&style=2",@"app name", yourscheme, @"終點", _shopLat, _shopLon] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
- [[UIApplication sharedApplication]openURL:[NSURL URLWithString:urlString]];
- }
#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];
}