Sunday, November 8, 2026
HomeiOS Developmentswift - How can I make iOS MapKit knowledge Serializable / Codable?

swift – How can I make iOS MapKit knowledge Serializable / Codable?

[ad_1]

Suppose that I instantiate a MKDirections.Request().

This offers me a request object, to which I set a supply, vacation spot, transportType, and requestsAlternateRoutes.

Subsequent, I instantiate an MKDirections object utilizing that Request object. I then calculate the routes and carry out some app-side logic on them, render them to a MKMapView, and so on.

I need the person to have the ability to save a selected route to allow them to shortly reference it later (i.e. they save this specific route as per the UI, shut the app, and a while later open the app and need to shortly go to this route and see it on a map).

I’ve appeared on the related Apple Developer docs to see how I can go about instantiating even simply an MKRoute from scratch (i.e. supply, vacation spot, and so on.) however the docs all say:

You don’t create situations of this class straight. Once you use an MKDirections object to request instructions from Apple, the returned MKDirections.Response object accommodates the doable routes.

(supply: MKRoute dev doc)

I have been in a position to reconstruct related supply and vacation spot MKPlacemarks utilizing the latitude and longitude coordinates I saved in a separate object that conforms to Codable. Nonetheless, there would not appear to be any API from Apple to do the identical with an MKRoute.

Does there exist any manner I am someway lacking to serialize an MKRoute in order that once I need to show it to the person as a reference, I need not calculate a totally new MKDirections.Request first, which then returns all doable routes, which then requires the person to but once more decide the one they initially picked the primary time? For the app I am engaged on, that is undesirable UX.

My subsequent thought could be to possibly simply prolong or subclass an MKPolyline / MKOverlay from the route and set that as much as conform to Codable so it may be saved, after which simply forego re-creating an MKRoute. This might “work” as the principle concept right here is to render the route onto the MapView however appears a bit unnecessarily sophisticated as a result of it additionally means writing extra code to avoid wasting the Route properties. I additionally do not know if that is doable since I have never tried it but.

It appears Apple could also be limiting its API purposely such that offline performance cannot be carried out, which I do not thoughts since my app is not meant for use for mapping/GPS (the precise Apple Maps app ought to nonetheless be used; my app is mainly only a reference). However that is most likely going too far into the weeds.

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments