如何存储放置图钉目标坐标 swift

How to store drop pin destination coordinates swift

我正在尝试存储放置图钉目标坐标并通过委托将它们传回?我有下面的 drop pin 功能。

func dropPinFor(placemark: MKPlacemark) {
        selectedItemPlacemark = placemark

        for annotation in mapView.annotations {
            if annotation.isKind(of: MKPointAnnotation.self) {
               // mapView.removeAnnotation(annotation) // removing the pins from the map
            }
        }

        let annotation = MKPointAnnotation()
        annotation.coordinate = placemark.coordinate
        mapView.addAnnotation(annotation)
        let (destLat, destLong) = (placemark.coordinate.latitude, placemark.coordinate.longitude)

        print("This is the pins destinations coord \(destLat, destLong)")
   }

但是当我在通过委托发送回数据之前尝试打印时,它打印出 0.0 lat 0.0 long

 @IBAction func addBtnWasPressed(_ sender: Any) {
         if delegate != nil {
        if firstLineAddressTextField.text != "" && cityLineAddressTextField.text != "" && postcodeLineAddressTextField.text != "" {

                //Create Model object DeliveryDestinations
            let addressObj = DeliveryDestinations(NameOrBusiness: nameOrBusinessTextField.text, FirstLineAddress: firstLineAddressTextField.text, SecondLineAddress: countryLineAddressTextField.text, CityLineAddress: cityLineAddressTextField.text, PostCodeLineAddress: postcodeLineAddressTextField.text, DistanceToDestination: distance, Lat: destlat, Long: destlong)

                print(distance)
                print("This is the latitude to use with protocol \(destlat)")
                print("This is the latitude to use with protocol \(destlong)")

                //add that object to previous view with delegate
                delegate?.userDidEnterData(addressObj: addressObj)

                //Dismising VC
                //navigationController?.popViewController(animated: true)

                clearTextFields()
            }
        }

    }

您在 dropPinFor 方法中声明了 (destLat, destLong),因此您的元组被重新声明,您只需在 dropPinFor

中分配值

声明

var coordinate : (Double, Double) = (0,0)

代码

func dropPinFor(placemark: MKPlacemark) {
        selectedItemPlacemark = placemark

        for annotation in mapView.annotations {
            if annotation.isKind(of: MKPointAnnotation.self) {
               // mapView.removeAnnotation(annotation) // removing the pins from the map
            }
        }

        let annotation = MKPointAnnotation()
        annotation.coordinate = placemark.coordinate
        mapView.addAnnotation(annotation)
        self.coordinate = (placemark.coordinate.latitude, placemark.coordinate.longitude)

        print("This is the pins destinations coord \(destLat, destLong)")
   }