2017-12-27 116 views
1

我试图存储放置引脚目标坐标并将它们通过委托传回?我有下面的引脚功能。如何存储放置引脚目标坐标swift

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

    } 
+1

您声明'(destLat,destLong)''的方法dropPinFor'内部 –

+0

[在夫特代表的实施例](的可能的复制https://stackoverflow.com/问题/ 40501780 /示例代码在swift) –

+0

@ReinierMelian感谢您的帮助我搞砸了方法之外声明,然后在方法内部声明。工作感谢。 – QuickSilver

回答

1

您在声明(destLat, destLong)dropPinFor方法,让你的元组被重新声明,你只需要在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)") 
    }