Mapkit - Obtaining multiple places at once?

Is there a way to obtain the information associated to multiple places at once? For example, instead of passing a single placeID, passing an array of placeIDs, and getting the basic info such as the name, the coordinates, etc.

Answered by DTS Engineer in 809795022

There isn't an API that takes in an array to do so. The simplest thing to do here is use Swift's async-await features so the system manages your task execution and suspension as you iterate over some requests, rather than trying to build your own system that tries to request many place details in parallel at one time. That looks like this:

Task {
    var mapItems: [MKMapItem] = []
    for place in visitedPlaces {
        if let mapItem = await place.convertToMapItem() {
            mapItems.append(mapItem)
        }
    }
}
@MainActor
    func convertToMapItem() async -> MKMapItem? {
        guard let identifier = MKMapItem.Identifier(rawValue: id) else { return nil }
        let request = MKMapItemRequest(mapItemIdentifier: identifier)
        var mapItem: MKMapItem? = nil
        do {
            mapItem = try await request.mapItem
        } catch let error {
             // Handle the error
        }
        return mapItem
    }

— Ed Ford,  DTS Engineer

Accepted Answer

There isn't an API that takes in an array to do so. The simplest thing to do here is use Swift's async-await features so the system manages your task execution and suspension as you iterate over some requests, rather than trying to build your own system that tries to request many place details in parallel at one time. That looks like this:

Task {
    var mapItems: [MKMapItem] = []
    for place in visitedPlaces {
        if let mapItem = await place.convertToMapItem() {
            mapItems.append(mapItem)
        }
    }
}
@MainActor
    func convertToMapItem() async -> MKMapItem? {
        guard let identifier = MKMapItem.Identifier(rawValue: id) else { return nil }
        let request = MKMapItemRequest(mapItemIdentifier: identifier)
        var mapItem: MKMapItem? = nil
        do {
            mapItem = try await request.mapItem
        } catch let error {
             // Handle the error
        }
        return mapItem
    }

— Ed Ford,  DTS Engineer

Mapkit - Obtaining multiple places at once?
 
 
Q