我正在尝试将 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()
}
}
最佳答案
你需要这样调用它
defaultCity(defLat : myLat, defLon: myLon)
Double
用于定义不调用的函数。
关于swift - 无法将类型 'Double.Type' 的值转换为预期的参数类型 'Double',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45268402/