You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多
文档控制台
免费开始使用

iOS应用调用Apple Maps设置目的地及相关功能技术咨询

嘿,我来帮你捋捋关于Apple Maps导航相关的API问题,刚好之前做过类似需求,给你详细拆解下:

关于Apple Maps导航API的三个问题解答

1. 苹果是否提供API用于在Apple Maps中设置目的地?

当然有!你可以借助MapKit框架里的MKMapItem类实现这个需求,核心是调用openMapsWithLaunchOptions:(Objective-C)或openInMaps(launchOptions:)(Swift)方法。这里分两种场景给你举实际代码例子:

用GPS坐标设置目的地

Objective-C版本:

// 1. 创建目标坐标
CLLocationCoordinate2D destinationCoord = CLLocationCoordinate2DMake(37.33182, -122.03118);
// 2. 创建地标
MKPlacemark *placemark = [[MKPlacemark alloc] initWithCoordinate:destinationCoord addressDictionary:nil];
// 3. 创建地图项
MKMapItem *destinationItem = [[MKMapItem alloc] initWithPlacemark:placemark];
// 4. 设置导航模式(可选值:驾车/步行/公共交通)
NSDictionary *launchOptions = @{MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving};
// 5. 打开Apple Maps并设置目的地
[destinationItem openInMapsWithLaunchOptions:launchOptions];

Swift版本:

// 1. 创建目标坐标
let destinationCoord = CLLocationCoordinate2D(latitude: 37.33182, longitude: -122.03118)
// 2. 创建地标
let placemark = MKPlacemark(coordinate: destinationCoord)
// 3. 创建地图项
let destinationItem = MKMapItem(placemark: placemark)
// 4. 设置导航模式(可选值:驾车/步行/公共交通)
let launchOptions = [MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving]
// 5. 打开Apple Maps并设置目的地
destinationItem.openInMaps(launchOptions: launchOptions)

用地址字符串设置目的地

如果只有地址文本,需要先通过CLGeocoder把地址解析成坐标,再按上面的流程处理:

Objective-C版本:

CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder geocodeAddressString:@"1 Infinite Loop, Cupertino, CA" completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
    if (!error && placemarks.count > 0) {
        CLPlacemark *placemark = placemarks.firstObject;
        MKMapItem *destinationItem = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithPlacemark:placemark]];
        NSDictionary *launchOptions = @{MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeWalking};
        [destinationItem openInMapsWithLaunchOptions:launchOptions];
    }
}];

Swift版本:

let geocoder = CLGeocoder()
geocoder.geocodeAddressString("1 Infinite Loop, Cupertino, CA") { placemarks, error in
    guard let placemark = placemarks?.first, error == nil else {
        return
    }
    let destinationItem = MKMapItem(placemark: MKPlacemark(placemark: placemark))
    let launchOptions = [MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeWalking]
    destinationItem.openInMaps(launchOptions: launchOptions)
}

2. 能否获知导航是否已激活并获取当前目的地?

很遗憾,苹果没有提供公开的API来直接查询Apple Maps的导航激活状态,或者获取它当前的目的地。这主要是出于用户隐私保护和系统应用权限隔离的考虑。

不过,如果是你的APP发起的导航,你可以在跳转前自己记录下目的地信息,然后通过监听APP的前后台状态来做一些间接判断,但没法直接获取Apple Maps内部的实时导航数据。

3. 若已设置目的地,是否可添加途经点?

可以的!从iOS 10开始,MKMapItem的相关方法支持添加途经点,你只需要在启动参数里指定MKLaunchOptionsWaypointsKey即可。举个例子:

Objective-C版本:

// 起点(可选,不设置默认用用户当前位置)
MKMapItem *currentLocation = [MKMapItem mapItemForCurrentLocation];

// 途经点1
CLLocationCoordinate2D waypoint1Coord = CLLocationCoordinate2DMake(37.7749, -122.4194);
MKPlacemark *waypoint1Placemark = [[MKPlacemark alloc] initWithCoordinate:waypoint1Coord addressDictionary:nil];
MKMapItem *waypoint1 = [[MKMapItem alloc] initWithPlacemark:waypoint1Placemark];

// 目的地
CLLocationCoordinate2D destinationCoord = CLLocationCoordinate2DMake(37.33182, -122.03118);
MKPlacemark *destinationPlacemark = [[MKPlacemark alloc] initWithCoordinate:destinationCoord addressDictionary:nil];
MKMapItem *destination = [[MKMapItem alloc] initWithPlacemark:destinationPlacemark];

// 组装途经点数组
NSArray *waypoints = @[waypoint1];

// 设置启动参数
NSDictionary *launchOptions = @{
    MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving,
    MKLaunchOptionsWaypointsKey: waypoints,
    MKLaunchOptionsShowsTrafficKey: @YES
};

// 打开Apple Maps,启动带途经点的导航
[MKMapItem openMapsWithItems:@[currentLocation, destination] launchOptions:launchOptions];

Swift版本:

// 起点(可选,不设置默认用用户当前位置)
let currentLocation = MKMapItem.forCurrentLocation()

// 途经点1
let waypoint1Coord = CLLocationCoordinate2D(latitude: 37.7749, longitude: -122.4194)
let waypoint1Placemark = MKPlacemark(coordinate: waypoint1Coord)
let waypoint1 = MKMapItem(placemark: waypoint1Placemark)

// 目的地
let destinationCoord = CLLocationCoordinate2D(latitude: 37.33182, longitude: -122.03118)
let destinationPlacemark = MKPlacemark(coordinate: destinationCoord)
let destination = MKMapItem(placemark: destinationPlacemark)

// 组装途经点数组
let waypoints = [waypoint1]

// 设置启动参数
let launchOptions = [
    MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving,
    MKLaunchOptionsWaypointsKey: waypoints,
    MKLaunchOptionsShowsTrafficKey: true
]

// 打开Apple Maps,启动带途经点的导航
MKMapItem.openMaps(with: [currentLocation, destination], launchOptions: launchOptions)

注意:途经点的数量有上限,目前最多支持15个(不同iOS版本可能有微调)。


内容的提问来源于stack exchange,提问作者Coder

火山引擎 最新活动