美文网首页
iOS内部调用手机地图导航目的地或标记指定位置

iOS内部调用手机地图导航目的地或标记指定位置

作者: 最强的小强 | 来源:发表于2023-04-18 11:39 被阅读0次

需求背景: 查看一个公司数据后,需要看公司的注册地址在地图的位置,并导航过去或查看相关地图位置

Tips 👉:这种方式的好处就是直接调用手机自带的地图,不用锲入SDK进行操作,轻量化;

1、配置相对应地图appLSApplicationQueriesSchemes白名单

· 1.1系统地图不用配置
· 1.2 高德地图 iosamap
· 1.3 百度地图 baidumap

20230411102431.jpg
2、判断是否能够打开URL,然后跳转链接即可.
#pragma mark - 导航目的地
- (void)openMaps {
    NSURL *gaode_App = [NSURL URLWithString:@"iosamap://"];
    NSURL *apple_App = [NSURL URLWithString:@"http://maps.apple.com/"];
    if ([[UIApplication sharedApplication] canOpenURL:gaode_App]) { // 使用高德地图导航
        CLGeocoder *geoCoder = [[CLGeocoder alloc]init];
        NSString *address = @"北京市昌平区";
        [geoCoder geocodeAddressString:address completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
            if (placemarks.count > 0 && error == nil) {
                CLPlacemark *placeMark = placemarks[0];
                CLLocation *location = placeMark.location;
                CLLocationCoordinate2D coordinate = location.coordinate;
                double latitude = coordinate.latitude;
                double longitude = coordinate.longitude;
                NSString *urlString = [[NSString stringWithFormat:@"iosamap://path?sourceApplication=wiseweb.FightEagle&dlat=%f&dlon=%f&dname=%@&style=2&t=0&dev=0",latitude,longitude,address] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
                [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
            }
        }];
    } else if ([[UIApplication sharedApplication] canOpenURL:apple_App]) { // 使用苹果自带地图导航
        NSString *urlString=[[NSString stringWithFormat:@"http://maps.apple.com/?daddr=%@",@"北京市昌平区"] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
    }
}
#pragma mark - 标记指定位置
- (void)openCustomMapsWithAddress:(NSString *)address {
    if (!([address isKindOfClass:[NSString class]] && [address isNoEmpty])) {
        return;
    }
    NSURL *gaode_App = [NSURL URLWithString:@"iosamap://"];
    NSURL *baidu_App = [NSURL URLWithString:@"baidumap://"];
    NSURL *apple_App = [NSURL URLWithString:@"http://maps.apple.com/"];
    UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"请选择您已经安装的导航工具" message:@"" preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    [alertC addAction:cancel];
    if ([[UIApplication sharedApplication] canOpenURL:gaode_App]) {
        UIAlertAction *picker = [UIAlertAction actionWithTitle:@"高德地图(推荐)" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            NSString *urlString = [[NSString stringWithFormat:@"iosamap://poi?sourceApplication=wiseweb.FightEagle&name=%@&dev=0",address] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
        }];
        [alertC addAction:picker];
    }
    if ([[UIApplication sharedApplication] canOpenURL:baidu_App]) {
        UIAlertAction *picker = [UIAlertAction actionWithTitle:@"百度地图" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            CLGeocoder *geoCoder = [[CLGeocoder alloc]init];
            [geoCoder geocodeAddressString:address completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
               if (placemarks.count > 0 && error == nil) {
                   CLPlacemark *placeMark = placemarks[0];
                   CLLocation *location = placeMark.location;
                   CLLocationCoordinate2D coordinate = location.coordinate;
                   double latitude = coordinate.latitude;
                   double longitude = coordinate.longitude;
                   NSString *urlString=[[NSString stringWithFormat:@"baidumap://map/marker?location=%lf,%lf&title=%@&content=%@&src=wiseweb.FightEagle",latitude,longitude,address,address] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
                   [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
               }
           }];
        }];
        [alertC addAction:picker];
    }
    if ([[UIApplication sharedApplication] canOpenURL:apple_App]) {
        UIAlertAction *picker = [UIAlertAction actionWithTitle:@"苹果地图" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            CLGeocoder *geoCoder = [[CLGeocoder alloc]init];
            [geoCoder geocodeAddressString:address completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
               if (placemarks.count > 0 && error == nil) {
                   CLPlacemark *placeMark = placemarks[0];
                   CLLocation *location = placeMark.location;
                   CLLocationCoordinate2D coordinate = location.coordinate;
                   double latitude = coordinate.latitude;
                   double longitude = coordinate.longitude;
                   NSString *urlString=[[NSString stringWithFormat:@"http://maps.apple.com/?q=%@&sll=%lf,%lf",address,latitude,longitude]  stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
                   [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
               }
           }];
        }];
        [alertC addAction:picker];
    }
    [self presentViewController:alertC animated:YES completion:nil];
}


相关文章

网友评论

      本文标题:iOS内部调用手机地图导航目的地或标记指定位置

      本文链接:https://www.haomeiwen.com/subject/gbgiddtx.html