如何存储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长

 @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)")
   }

最新更新