Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ViewMapAnnotation not updating location #23

Open
dvdmrtnz opened this issue Aug 17, 2022 · 1 comment
Open

ViewMapAnnotation not updating location #23

dvdmrtnz opened this issue Aug 17, 2022 · 1 comment

Comments

@dvdmrtnz
Copy link

Using MapAnnotation I can change the coordinate of an item and it changes on the map. However if I use ViewMapAnnotation, it doesn't change the location.

Here is a code example. Using MapAnnotation you can see the point move around. If you change it to ViewMapAnnotation, the point doesn't move at all.

import SwiftUI
import MapKit
import Map

struct ContentView: View {
    
    @State private var map = MKCoordinateRegion(
        center: CLLocationCoordinate2D(latitude: 40.4, longitude: -3.7),
        span: MKCoordinateSpan(latitudeDelta: 2, longitudeDelta: 2)
    )
    
    @State var locations = [
        MyLocation(coordinate:CLLocationCoordinate2D(latitude:40.4, longitude: -3.7))]
    
    var body: some View {
        Map(
            coordinateRegion: $map,
            annotationItems: locations,
            annotationContent: { location in
                MapAnnotation(coordinate: location.coordinate) { // Change MapAnnotation to ViewMapAnnotation here
                    Circle()
                    .frame(width: 25, height: 25)
                    .foregroundColor(.red)
                }
            }
        )
        .task({
            while(true)
            {
                try? await Task.sleep(nanoseconds: 1_000_000_000)
                locations[0].coordinate = CLLocationCoordinate2D(
                    latitude: 40.4 + Double.random(in: -0.5...0.5),
                    longitude: -3.7 + Double.random(in: -0.5...0.5))
            }
        })
    }
        
}

struct MyLocation : Identifiable {
    let id = UUID()
    var coordinate: CLLocationCoordinate2D
}
@pauljohanneskraft
Copy link
Owner

Hey - sorry for the long wait. I'm not sure how Apple's Map implementation makes sure that that is the case exactly (since it isn't open-source).

In this implementation so far, I was trying to keep the protocols for creating custom annotations and overlays as clean as possible, so I'm not sure, if this would still easily be possible here (since I'm assuming that they simply let MapAnnotation inherit from Equatable or something like that).

In your example, you could easily circumvent the issue you are facing by creating a MyLocation object on each change of the coordinate. I'm not sure, if this plays well with animations and so on, but I unfortunately didn't have time to check whether the Apple approach has animations either.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants