如何存储放置引脚目标坐标swift
我试图存储放置引脚目标坐标并将它们通过委托传回?我有下面的引脚功能。如何存储放置引脚目标坐标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()
}
}
}
回答:
您在声明(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)")
}
以上是 如何存储放置引脚目标坐标swift 的全部内容, 来源链接: utcz.com/qa/264909.html