Map icon indicating copy to clipboard operation
Map copied to clipboard

Annotations aren't immobile and move with zoom

Open GuillaumeGenest opened this issue 1 year ago • 4 comments

When I use ignoreSafeArea or edgesIgnoringSafeArea(.all) in my code and if I zoom, my annotations aren't immobile and move. To fix the problem I have to add a title with a VStack above my map Some one have an idea of the problem ?

Thank you Good work with the package, it's really impressive

Carte(coordinateRegion: $region ,type: MKStandardMapConfiguration() , userTrackingMode: $userTrackingMode ,annotationItems: MockedDataMapAnnotation, annotationContent: { location in CarteAnnotation(coordinate: CLLocationCoordinate2D(latitude: location.latitude, longitude: location.longitude)) { VStack(spacing: 0) { Image(systemName: "map") .resizable() .scaledToFit() .frame(width: 15, height: 15) .font(.headline) .foregroundColor(Color.black) .padding(6) .background(Color.white) .clipShape(Circle()) Image(systemName: "triangle.fill") .resizable() .scaledToFit() .foregroundColor(.black) .frame(width: 10, height: 10) .rotationEffect(Angle(degrees: 180)) .offset(y: -1.5) .padding(.bottom, 40) } } }, overlays: [ MKPolyline(coordinates: polylineCoordinates, count: polylineCoordinates.count) ],overlayContent: { overlay in RendererCarteOverlay(overlay: overlay) { _, overlay in

                if let polyline = overlay as? MKPolyline {
                    let renderer = MKPolylineRenderer(polyline: polyline)
                    renderer.lineWidth = 2
                    renderer.lineCap = .butt
                    renderer.lineJoin = .miter
                    renderer.miterLimit = 0
                    renderer.lineDashPhase = 0
                    renderer.lineDashPattern = [10,5]
                    renderer.strokeColor = .orange
                    return renderer
                } else {
                     assertionFailure("Unknown overlay type found.")
                     print("Probleme overlay")
                     return MKOverlayRenderer(overlay: overlay)
                }
            }
        }
   ).edgesIgnoringSafeArea(.all)

GuillaumeGenest avatar May 22 '23 09:05 GuillaumeGenest

As described in the readme, please do not create your MKOverlay instances inside the body-property of your view, since this means that the overlay instances do not have fixed pointer values (i.e. the ObjectIdentifier is different for each run) and that means that the overlays will constantly be removed and added again for each call of the view's body. Please make sure to have them wrapped in @State and only set their values, when they actually need to be changed.

Having said that, you seem to be using Carte and CarteAnnotation, which are both not defined in this library - are those custom wrappers you wrote? It may also help to fix your annotations to a certain size, since resizing doesn't really work that well with MKMapView.

pauljohanneskraft avatar May 22 '23 10:05 pauljohanneskraft

I have the same issue without overlay, I just rename map and MapViewAnnotation to understand the error. I have the same issue with this code.

struct ContentView: View { @State var region = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: 48.861, longitude: 2.335833), latitudinalMeters: 8000, longitudinalMeters: 8000)

@State var MockedDataMapAnnotation : [Location] = [
Location(name: "Perou", cityName: "Perou", latitude: -12.046374, longitude: -77.0427934, description: "", category: .Logement, imageURL: ""),
   Location(name: "Le Louvre", cityName: "Paris", latitude: 48.861, longitude:2.335833, description: "", category: .Musee, imageURL: ""),
Location(name: "Tour eiffeul", cityName: "Paris", latitude: 48.858370, longitude:2.294481, description: "", category: .Musee, imageURL: ""),
   Location(name: "Berlin", cityName: "Berlin", latitude:52.520007, longitude:13.404954, description: "", category: .Logement, imageURL: ""),
   Location(name: "Rome", cityName: "Rome", latitude:41.902784, longitude:12.496366, description: "", category: .Bar, imageURL: "https://firebasestorage.googleapis.com/v0/b/sunny-road-2f946.appspot.com/o/user?alt=media&token=d959b262-7cdb-48d9-a720-da355b8167bc"),
   Location(name: "Barcelone", cityName: "Barcelone", latitude:41.3850639, longitude:2.1734035, description: "", category: .Vue, imageURL: "")
]

var body: some View {
        Map(coordinateRegion: $region , annotationItems: MockedDataMapAnnotation,
              annotationContent: { location in
            ViewMapAnnotation(coordinate: CLLocationCoordinate2D(latitude: location.latitude, longitude: location.longitude)) {
                VStack(spacing: 0) {
                    Image(systemName: "map")
                        .resizable()
                        .scaledToFit()
                        .frame(width: 15, height: 15)
                        .font(.headline)
                        .foregroundColor(Color.black)
                        .padding(6)
                        .background(Color.white)
                        .clipShape(Circle())
                    Image(systemName: "triangle.fill")
                        .resizable()
                        .scaledToFit()
                        .foregroundColor(.black)
                        .frame(width: 10, height: 10)
                        .rotationEffect(Angle(degrees: 180))
                        .offset(y: -1.5)
                        .padding(.bottom, 40)
                }
            }
        }
        ).ignoresSafeArea()
}

}

GuillaumeGenest avatar May 22 '23 11:05 GuillaumeGenest

@pauljohanneskraft I analyzed the problem, and it seems have an issue with the function private func updateRegion(on mapView: MKMapView, from previousView: Carte?, to newView: Carte, animated: Bool) in the coordinator ( for iOS 16). When the region is not set, the annotations don't move when you zoom.

Moreover when I tried to zoom in to the maximum, I have the error on my debugger screen : Wrapped around the polygon without finishing... :-( [VKDefault] Building failed to triangulate!

I don't know if you have ever had this kind of problem.

GuillaumeGenest avatar May 25 '23 12:05 GuillaumeGenest

I'm having this issue as well. Has there been a fix or workaround yet?

Bubba8291 avatar Aug 03 '23 22:08 Bubba8291