Autorefresh MapPolyline when route data grows

Map(position: $position, selection: $selectedResult) { MapPolyline(coordinates: track).stroke(.blue, lineWidth: 5) } When the @State track: [CLLocationCoordinates] is growing with new data (new coordinates appended) how can we reach that Map and MapPolyline is automatically refreshed?

Post not yet marked as solved Up vote post of hlmSft Down vote post of hlmSft
1.2k views

Replies

Same Q. Need SwiftUI implementation of MKPolylineRenderer to be added to iOS 17 sdk.

In ContentView, use an array of MKPolylines, manage them in some function that adds/removes polylines based on user interaction, and then place them in the Map() in a ForEach loop. It may not be as efficient as they claim MKMultiPolyline to be, but it gets n-number of polylines on the map.

Top of the ContentView struct:

    @State private var multiPolylines = [MKPolyline]()

Using them in the Map:

            Map() {
                ForEach(multiPolylines, id:\.self) { polyline in
                    MapPolyline(polyline)
                        .stroke(.pink, lineWidth: 3)
                }
            }
  • I had a similar problem and what I did was creating an array of "Identifiable Structs" with only 1 item with the coordinates. Every time I had new data, I replaced the struct in the array and because it has a new ID, it's updated accordingly.

    It's almost the same that you are saying, but with your example, if you have too many lines, you start getting an error after approximately 600 lines.

    With my solution, I did a test with almost 5000 lines, all in the same polyline without any problem.

Add a Comment

Hi @CaneApp @hlmSft @sochpdx,

Did you manage to run the code on a device? Seems like MapPolyline has a mismatch with some imports. Just doesn't seem to work on a real device, simulator all fine..dyld[727]: Symbol not found: _$s15_MapKit_SwiftUI0A8PolylineV11coordinatesACSaySo22CLLocationCoordinate2DVG_tcfC Referenced from: <E9A5FA65-D271-398C-B746-E6F3B8951F53> /private/var/containers/Bundle/Application/89A67FF0-1A6B-4362-B5EA-3BF51DE57BF0/MyApp.app/MyApp Expected in: <CE0BFEFF-34DF-3B0D-A41D-DABB2142FB99> /System/Library/Frameworks/_MapKit_SwiftUI.framework/_MapKit_SwiftUI