1

I have the following code:

    struct MyLocationMap: View {

    @EnvironmentObject var userData: UserData
    @State var annotationArray: [MyAnnotation] = [MyAnnotation(coordinates: CLLocationCoordinate2D(latitude: CurrentLocation().coordinates?.latitude ?? CLLocationCoordinate2D().latitude, longitude: CurrentLocation().coordinates?.longitude ?? CLLocationCoordinate2D().longitude), type: .waypoint)]

    var body: some View {

        MakeMapView(annotationArray: annotationArray)
        .gesture(
        LongPressGesture(minimumDuration: 1)
        .onEnded { _ in
            print("MapView pressed!\n--------------------------------------\n")
            //Handle press here

        })
    }
}

import SwiftUI
import CoreLocation
import MapKit

struct MakeMapView : UIViewRepresentable {
    typealias UIViewType = MKMapView

    let annotationArray: [MyAnnotation]?



    func makeUIView(context: UIViewRepresentableContext<MakeMapView>) -> MKMapView{
        MKMapView()
    }




    func updateUIView(_ mapView: MKMapView, context: Context) {



        mapView.showsUserLocation = true


        if let coordinates = CurrentLocation().coordinates {

            //            updateAnnotations(from: mapView)

            DispatchQueue.main.asyncAfter(deadline: .now() + 1.0, execute: {

                mapView.showsUserLocation = true
                mapView.showsCompass = true
                mapView.showsScale = true
                mapView.mapType = .satellite
                let span = MKCoordinateSpan(latitudeDelta: 0.002, longitudeDelta: 0.002)
                let region = MKCoordinateRegion(center: coordinates, span: span)
                mapView.setRegion(region, animated: true)
            })

        }
    }

What I am having trouble with is implementing func convert(_ point: CGPoint, toCoordinateFrom view: UIView?) -> CLLocationCoordinate2D to obtain the lat/lon of the gesture solely using SwiftUI/Combine. Is it possible? If not, can I implement it with what I have?

I have reviewed the post at How to handle touch gestures in SwiftUI in Swift UIKit Map component?

and

Add single pin to Mapkit with SwiftUI

Once I have the coordinates, dropping the pin is straightforward, but I can't wrap my head around getting the coordinates.

Thanks.

Yrb
  • 8,103
  • 2
  • 14
  • 44

1 Answers1

2

A DragGesture with no minimumDistance activates immediately and has location and startLocation in its onChanged listener, you can use that to grab location like so:

struct GesturesView: View {
    @State private var location: CGPoint = .zero

    var body: some View {
        let drag = DragGesture(minimumDistance: 0).onChanged { 
            self.location = $0.startLocation 
        }

        let longPress = LongPressGesture().onEnded { _ in
            self.doSomething(with: self.location)
        }

        let gesture = longPress.simultaneously(with: drag)

        return Rectangle()
            .frame(width: 300, height: 300)
            .padding()
            .gesture(gesture)
    }

    func doSomething(with location: CGPoint) {
        print("Pressed at", location)
    }
}
Casper Zandbergen
  • 3,419
  • 2
  • 25
  • 49