Return CLLocationCoordinate2D 结构
Return CLLocationCoordinate2D to struct
我是编程新手,我的问题是如何将变量类型 CLLocationCoordinate2D 的数据return 转换为坐标,即结构类型。尝试开发天气应用程序。
我有一个结构:
struct Coordinates {
let latitude: Double
let longitude: Double
}
我的代码如下所示:
//getting coordinates from String
func getCoordinateFrom(address: String, completion: @escaping(_ coordinate: CLLocationCoordinate2D?, _ error: Error?) -> () ) {
CLGeocoder().geocodeAddressString(address) { placemarks, error in
completion(placemarks?.first?.location?.coordinate, error)
}
}
//When the User type his city, coordinates have type of CLLocationCoordinate2D
@IBAction func changeCityButtonPressed(_ sender: UIButton) {
guard let address = textField.text else { return }
getCoordinateFrom(address: address) { coordinate, error in
guard let coordinate = coordinate, error == nil else { return }
DispatchQueue.main.async {
print(coordinate)
}
}
}
我有一个常量,我的任务是将坐标从函数转移到这个常量。
let coordinates = Coordinates(latitude: 00.617366, longitude: 37.617366)
问题是函数中的这些坐标是闭包的。所以我不能 return 他们等等。我试图找到正确的答案但没有结果。有人有一些 advice/solution?
这一行:
let coordinates = Coordinates(latitude: 00.617366, longitude: 37.617366)
将let
替换为var
在此代码中:
@IBAction func changeCityButtonPressed(_ sender: UIButton) {
guard let address = textField.text else { return }
getCoordinateFrom(address: address) { coordinate, error in
guard let coordinate = coordinate, error == nil else { return }
DispatchQueue.main.async {
print(coordinate)
}
}
}
替换为:
guard let coordinate = coordinate, error == nil else { return }
DispatchQueue.main.async {
print(coordinate)
}
通过这个:
guard let coordinate = coordinate, error == nil else { return }
coordinates.latitude = coordinate.latitude
coordinates.longitude = coordinate.longitude
DispatchQueue.main.async {
print(coordinate)
}
这就是您的坐标变量将如何更新为新的经纬度
我是编程新手,我的问题是如何将变量类型 CLLocationCoordinate2D 的数据return 转换为坐标,即结构类型。尝试开发天气应用程序。
我有一个结构:
struct Coordinates {
let latitude: Double
let longitude: Double
}
我的代码如下所示:
//getting coordinates from String
func getCoordinateFrom(address: String, completion: @escaping(_ coordinate: CLLocationCoordinate2D?, _ error: Error?) -> () ) {
CLGeocoder().geocodeAddressString(address) { placemarks, error in
completion(placemarks?.first?.location?.coordinate, error)
}
}
//When the User type his city, coordinates have type of CLLocationCoordinate2D
@IBAction func changeCityButtonPressed(_ sender: UIButton) {
guard let address = textField.text else { return }
getCoordinateFrom(address: address) { coordinate, error in
guard let coordinate = coordinate, error == nil else { return }
DispatchQueue.main.async {
print(coordinate)
}
}
}
我有一个常量,我的任务是将坐标从函数转移到这个常量。
let coordinates = Coordinates(latitude: 00.617366, longitude: 37.617366)
问题是函数中的这些坐标是闭包的。所以我不能 return 他们等等。我试图找到正确的答案但没有结果。有人有一些 advice/solution?
这一行:
let coordinates = Coordinates(latitude: 00.617366, longitude: 37.617366)
将let
替换为var
在此代码中:
@IBAction func changeCityButtonPressed(_ sender: UIButton) {
guard let address = textField.text else { return }
getCoordinateFrom(address: address) { coordinate, error in
guard let coordinate = coordinate, error == nil else { return }
DispatchQueue.main.async {
print(coordinate)
}
}
}
替换为:
guard let coordinate = coordinate, error == nil else { return }
DispatchQueue.main.async {
print(coordinate)
}
通过这个:
guard let coordinate = coordinate, error == nil else { return }
coordinates.latitude = coordinate.latitude
coordinates.longitude = coordinate.longitude
DispatchQueue.main.async {
print(coordinate)
}
这就是您的坐标变量将如何更新为新的经纬度