Rama Krish
Rama Krish

Reputation: 373

SwifUI Map iOS 14 handle drag and Magnification Gesture

I am placing a small view on top of Map if user drags or zoom the map, I use below code to do that but the Zoom event not triggering . kindly help me what I am missing

    let drag = DragGesture()
        .onChanged({ _ in
            self.onDragEnded()
        })
    let pinch = MagnificationGesture()
        .onChanged({ _ in
            self.onDragEnded()
        })
    @State private var show = false

    let combinedGesture = pinch.simultaneously(with: drag)

   var body: some View {

    ZStack {
        Map(coordinateRegion: $region, showsUserLocation: true, annotationItems: results) { dest in
            
        }.gesture(combinedGesture)
        if show {
            Color.green
                .frame(width: 100, height: 100, alignment: .center)
        }
      }
    }
    private func onDragEnded() {
        show = true
        
    }

Upvotes: 3

Views: 1587

Answers (1)

Adrien
Adrien

Reputation: 1937

When the user drag and/or zoom the map, the region changes. You just have to observe the MKCoordinateRegion, or one of its two properties (span or center) :

.onChange(of: region.span) {_ in
     print("regionDidChange")
}

The property you observe with onChange has to be Equatable :

extension MKCoordinateSpan: Equatable {
    public static func == (lhs: MKCoordinateSpan, rhs: MKCoordinateSpan) -> Bool {
        lhs.latitudeDelta == rhs.latitudeDelta && lhs.longitudeDelta == rhs.longitudeDelta
    }
}

EDIT :

But if you want to perform different actions when the position changes and when the zoom changes, it's more complicated. Because 1- the zoom level depends on both the position and the span. 2- when the user zooms in or out, the center changes (very slightly) 3- when the center changes the span changes (a little)

You can start this way:

struct MapView: View {
    @State private var region = MKCoordinateRegion(center: .init(latitude: 42, longitude: 1.2), span: .init(latitudeDelta: 10, longitudeDelta: 10))
    @State private var drag: Int = 0
    @State private var pinch: Int = 0
    @State private var oldZoomLevel: Double?
    
    var body: some View {
        ZStack(alignment: Alignment(horizontal: .center, vertical: .top)) {
            
            GeometryReader { proxy in
                Map(coordinateRegion: $region)
                    .onChange(of: region) { newRegion in
                        let zlevel = getZoomLevel(mapWidth: Double(proxy.size.width))
                        if zlevel != oldZoomLevel {
                            pinch += 1
                        } else {
                            drag += 1
                        }
                        oldZoomLevel = zlevel
                    }
            }
   
            HStack {
                Text(drag.description)
                    .padding()
                    .background(Color.pink)
                Spacer()
                Text(pinch.description)
                    .padding()
                    .background(Color.yellow)
            }
        }
    }
    
    func getZoomLevel(mapWidth: Double) -> Double {
        let MERCATOR_RADIUS = 85445659.44705395
        let level = 20.00 - log2(region.span.longitudeDelta * MERCATOR_RADIUS * Double.pi / (180.0 * mapWidth))
        return round(level * 100000)/100000
    }
}

And the extensions :

extension CLLocationCoordinate2D: Equatable {
    public static func == (lhs: CLLocationCoordinate2D, rhs: CLLocationCoordinate2D) -> Bool {
        lhs.latitude == rhs.latitude && lhs.longitude == rhs.longitude
    }
}

extension MKCoordinateSpan: Equatable {
    public static func == (lhs: MKCoordinateSpan, rhs: MKCoordinateSpan) -> Bool {
        lhs.latitudeDelta == rhs.latitudeDelta && lhs.longitudeDelta == rhs.longitudeDelta
    }
}

extension MKCoordinateRegion: Equatable {
    public static func == (lhs: MKCoordinateRegion, rhs: MKCoordinateRegion) -> Bool {
        lhs.center == rhs.center && lhs.span == rhs.span
    }
}

Upvotes: 2

Related Questions