我试图存储drop pin目标坐标并通过委托传回它们?我有下面的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()
}
}
}
答案 0 :(得分: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)")
}