打开苹果自带地图
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)
}
}