我试图将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.My 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()
}
}发布于 2017-07-24 02:17:22
您需要将其命名为
defaultCity(defLat : myLat, defLon: myLon)定义未调用的函数时使用Double。
https://stackoverflow.com/questions/45268402
复制相似问题