我正在尝试将GPS坐标从一个功能传递到另一个功能但它无效。
func defaultCity(defLat: Double, defLon: Double){
let url = URL(string: "api.openweathermap.org/data/2.5/weather?lat=\(defLat)&lon=\(defLon)&appid=626a124ef0844d2e021329c38a5dfafd")
let task = URLSession.shared.dataTask(with: url!) { (data, response, error) in
if error != nil{
print("Problem extracting data")
} else {
if let urlContent = data {
do {
let jsonResult = try JSONSerialization.jsonObject(with: urlContent, options: JSONSerialization.ReadingOptions.mutableContainers) as AnyObject
print("Geoloc coords: \(jsonResult)")
}catch{
print("Json Processing has failed or city name not recognized.")
}
}
}
}
task.resume()
}
我正在尝试将defLon
和defLat
从currentLocation
转换为defaultCity
。如何传递这些变量?
func currentlocation(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]){
if let location = locations.first{
//print(location.coordinate)
let myLocation:CLLocationCoordinate2D = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude)
print("My location is \(myLocation)")
let myLon = location.coordinate.longitude
let myLat = location.coordinate.latitude
defaultCity(defLat: myLat, defLon: myLon)
print(location.altitude)
print(location.speed)
}
}
当我使用以下行调用viewDidLoad()中的函数时:
defaultCity(defLat : Double, defLon: Double)
这就是我得到的错误,我无法弄明白。奇怪的是,红色箭头仅在第一个Double
下方。我的viewDidLoad()
方法:
override func viewDidLoad() {
super.viewDidLoad()
openCityAlert()
getAndDisplayWeather()
//for use when app is open and in background
locationManager.requestAlwaysAuthorization()
if CLLocationManager.locationServicesEnabled(){
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.startUpdatingLocation()
}
}
答案 0 :(得分:0)
您需要将其称为
defaultCity(defLat : myLat, defLon: myLon)
定义不调用的函数时使用 Double
。