Mcrich23 / SwiftUIMap

A better map for SwiftUI
MIT License
25 stars 1 forks source link

Map Drag, Zoom, Rotate Freezes Map #5

Open mkaulfers opened 1 year ago

mkaulfers commented 1 year ago

Describe the bug It seems that updating the map with any gesture causes the map to simply freeze, rather than panning and zooming. The same thing happens whenever I tap on an Annotation

To Reproduce Steps to reproduce the behavior:

  1. Launch on Previews or Simulator at Map
  2. Attempt to interact

Expected behavior It should allow gestures

Versions (please complete the following information):

Screenshots Shows loading the map, using the code below, and that I can't interact with it. Simulator Screen Recording - iPhone 14 Pro Max - 2023-06-23 at 14 27 39

Additional context

struct MapView: View {
    @EnvironmentObject var appConfig: AppConfig

    @State var zoom: Double = 1.0
    @State var coordinates: LocationCoordinate
    @State var points: [Annotations]
    @State var isUserLocationVisible = false

    var body: some View {
        AnnotationMapView(
            zoom: $zoom, // Starting Zoom of Map (Range: 0-1, Lower is closer in)
            coordinates: $coordinates, // Starting address in the Center of the Map (use this or coordinates)
            points: $points,
            isUserLocationVisible: $isUserLocationVisible, // Binding for if isUserLocationVisible
            selected: { Title, Subtitle, Address, isCluster in // Action When Marker is Selected (Returns title, subtitle, and address in annotation along with if it's in a cluster)
                print("tapped \(Address)")
            }, deselected: { // Action When Marker is Deselceted
                print("deselected annotation")
            }
        )
        .showCompass(true)
        .pointsOfInterest(.excludingAll)
        .mapType(.standard)
        .isMultipleTouchEnabled(true)
        .isUserInteractionEnabled(true)
        .isScrollEnabled(true)
        .isRotateEnabled(true)
        .isZoomEnabled(true)
        .camera(MKMapCamera(lookingAtCenter: coordinates, fromDistance: zoom*999999, pitch: 0, heading: 0))
        .cameraBoundary(MKMapView.CameraBoundary(coordinateRegion: MKCoordinateRegion(center: coordinates, span: MKCoordinateSpan(latitudeDelta: 4, longitudeDelta: 4))))
        .cameraZoomRange(MKMapView.CameraZoomRange(minCenterCoordinateDistance: CLLocationDistance(600)))
    }

    init(pois: [LocationDetail]) {
        self.coordinates = RegionCalculator.calculateRegion(from: pois).center
        self.points = pois.map {
            Annotations(title: $0.locationName,
                        subtitle: $0.locationDescription,
                        location: .coordinates($0.coordinate),
                        glyphImage: .defaultIcon,
                        markerTintColor: .blue,
                        glyphTintColor: .red,
                        displayPriority: .defaultHigh)
        }
    }
}

struct MapView_Previews: PreviewProvider {
    static var previews: some View {
        MapView(pois: appConfig.poiLocations ?? [])
            .environmentObject(appConfig)
    }
}

class RegionCalculator {
    static func calculateRegion(from pois: [LocationDetail]) -> MKCoordinateRegion {
        guard !pois.isEmpty else {
            return MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: 0, longitude: 0), span: MKCoordinateSpan(latitudeDelta: 0.5, longitudeDelta: 0.5))
        }

        var totalLat = 0.0
        var totalLong = 0.0

        for poi in pois {
            totalLat += poi.coordinate.latitude
            totalLong += poi.coordinate.longitude
        }

        let avgLat = totalLat / Double(pois.count)
        let avgLong = totalLong / Double(pois.count)

        return MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: avgLat, longitude: avgLong), span: MKCoordinateSpan(latitudeDelta: 0.5, longitudeDelta: 0.5))
    }
}

Request

It's entirely possible that I have miffed the implementation completely, if so please let me know and I'll attempt to correct it. Also, is it possible to utilize a custom SwiftUI view in place of the Annotation?

Mcrich23 commented 1 year ago

Can you please provide a small sample project for me to debug and play around with?

Mcrich23 commented 1 year ago

@mkaulfers ?

mkaulfers commented 1 year ago

I'm trying to see if I can come up with something. It was a blocker for me so I ended up pulling the package from my project. In theory you should be able to reproduce by just passing in your own locations that have a coordinate property.

Mcrich23 commented 1 year ago

In theory you should be able to reproduce by just passing in your own locations that have a coordinate property.

I think that it may have to do with the overlays

mkaulfers commented 1 year ago

Can you explain a bit more what you think it might be? I'll fork and take a gander. I've been trying to implement my own maps and yeh, it's a heavy lift, so having a working package would be better xD. Does this package also support routes and such.

Mcrich23 commented 6 months ago

@mkaulfers Sorry for not responding until now. I believe I found the issue and fixed it a couple months ago, but did you find resolution?