1

I'm trying to store the drop pin destination coordinates and pass them back through the delegate? I have the below drop pin function.

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

But when I try to print before sending data back through delegate it print's 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()
            }
        }

    }
QuickSilver
  • 730
  • 5
  • 28

1 Answers1

1

You are declaring the (destLat, destLong) inside the dropPinFor method, so your tuple is redeclared you need only assign the value in the dropPinFor

Declaration

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

Code

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)")
   }
Reinier Melian
  • 20,519
  • 3
  • 38
  • 55