swift 通过经纬度打开三方地图软件

打开苹果自带地图

OC版

CLLocationDegrees latitude = 37.7749; // 目标位置的纬度
CLLocationDegrees longitude = -122.4194; // 目标位置的经度

// 打开苹果地图
CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(latitude, longitude);
MKPlacemark *placemark = [[MKPlacemark alloc] initWithCoordinate:coordinate];
MKMapItem *mapItem = [[MKMapItem alloc] initWithPlacemark:placemark];
mapItem.name = @"目标位置";

[mapItem openInMapsWithLaunchOptions:nil];

Swift版本

let latitude: CLLocationDegrees = 37.7749 // 目标位置的纬度
let longitude: CLLocationDegrees = -122.4194 // 目标位置的经度

// 打开苹果地图
let coordinate = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
let placemark = MKPlacemark(coordinate: coordinate)
let mapItem = MKMapItem(placemark: placemark)
mapItem.name = "目标位置"
mapItem.openInMaps(launchOptions: nil)

打开百度地图

OC版

// 打开百度地图
NSString *baiduMapURLString = [NSString stringWithFormat:@"baidumap://map/marker?location=%f,%f", latitude, longitude];
NSURL *baiduMapURL = [NSURL URLWithString:baiduMapURLString];

if ([[UIApplication sharedApplication] canOpenURL:baiduMapURL]) {
    // 百度地图已安装
    [[UIApplication sharedApplication] openURL:baiduMapURL options:@{} completionHandler:nil];
} else {
    // 百度地图未安装,打开网页版地图
    NSString *webURLString = [NSString stringWithFormat:@"http://api.map.baidu.com/marker?location=%f,%f", latitude, longitude];
    NSURL *webURL = [NSURL URLWithString:webURLString];
    [[UIApplication sharedApplication] openURL:webURL options:@{} completionHandler:nil];
}

Swift版本

// 打开百度地图
let baiduMapURLString = "baidumap://map/marker?location=\(latitude),\(longitude)"
if let baiduMapURL = URL(string: baiduMapURLString), UIApplication.shared.canOpenURL(baiduMapURL) {
    UIApplication.shared.open(baiduMapURL)
} else {
    let webURLString = "http://api.map.baidu.com/marker?location=\(latitude),\(longitude)"
    if let webURL = URL(string: webURLString) {
        UIApplication.shared.open(webURL)
    }
}

打开高德地图

OC版

// 打开高德地图
NSString *gaodeMapURLString = [NSString stringWithFormat:@"iosamap://viewMap?sourceApplication=yourAppName&poiname=&lat=%f&lon=%f&dev=0", latitude, longitude];
NSURL *gaodeMapURL = [NSURL URLWithString:gaodeMapURLString];

if ([[UIApplication sharedApplication] canOpenURL:gaodeMapURL]) {
    // 高德地图已安装
    [[UIApplication sharedApplication] openURL:gaodeMapURL options:@{} completionHandler:nil];
} else {
    // 高德地图未安装,打开网页版地图
    NSString *webURLString = [NSString stringWithFormat:@"http://uri.amap.com/marker?position=%f,%f", latitude, longitude];
    NSURL *webURL = [NSURL URLWithString:webURLString];
    [[UIApplication sharedApplication] openURL:webURL options:@{} completionHandler:nil];
}

Swift版本

// 打开高德地图
let gaodeMapURLString = "iosamap://viewMap?sourceApplication=yourAppName&poiname=&lat=\(latitude)&lon=\(longitude)&dev=0"
if let gaodeMapURL = URL(string: gaodeMapURLString), UIApplication.shared.canOpenURL(gaodeMapURL) {
    UIApplication.shared.open(gaodeMapURL)
} else {
    let webURLString = "http://uri.amap.com/marker?position=\(latitude),\(longitude)"
    if let webURL = URL(string: webURLString) {
        UIApplication.shared.open(webURL)
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值