获取用户位置坐标(OneShotLocationManager)
Getting coordinates of user's location (OneShotLocationManager)
我目前正在尝试制作一个天气应用程序。它使用 http://developer.forecast.io API,它接受经度和纬度坐标来查找天气。我已经实现了 OneShotLocationManager,但我无法完全从中获取坐标,然后将它们与 Forecast API.
一起使用
这是我的代码:
var manager: OneShotLocationManager?
let coordinate: (lat: Double, long: Double) = (0,0)
override func viewDidLoad() {
super.viewDidLoad()
manager = OneShotLocationManager()
manager!.fetchWithCompletion { location, error in
// fetch location or an error
if let loc = location {
print(location?.coordinate.latitude)
print(location?.coordinate.longitude)
let coordinate: (lat: Double, long: Double) = ((location?.coordinate.latitude)!,location!.coordinate.longitude)
} else if let err = error {
print(err.localizedDescription)
}
self.manager = nil
} }
我可以让 OneShotLocationManager 将坐标打印到控制台,但我不知道如何在 Forecast.io 中实际使用它们。如有任何帮助,我们将不胜感激。
您不再需要使用位置变量,因此您进行 if let
检查。
试试这个:
if let loc = location {
let coordinate = (lat: loc.coordinate.latitude, long: loc.coordinate.longitude)
// ...your web API request stuff using coordinate....
}
我目前正在尝试制作一个天气应用程序。它使用 http://developer.forecast.io API,它接受经度和纬度坐标来查找天气。我已经实现了 OneShotLocationManager,但我无法完全从中获取坐标,然后将它们与 Forecast API.
一起使用这是我的代码:
var manager: OneShotLocationManager?
let coordinate: (lat: Double, long: Double) = (0,0)
override func viewDidLoad() {
super.viewDidLoad()
manager = OneShotLocationManager()
manager!.fetchWithCompletion { location, error in
// fetch location or an error
if let loc = location {
print(location?.coordinate.latitude)
print(location?.coordinate.longitude)
let coordinate: (lat: Double, long: Double) = ((location?.coordinate.latitude)!,location!.coordinate.longitude)
} else if let err = error {
print(err.localizedDescription)
}
self.manager = nil
} }
我可以让 OneShotLocationManager 将坐标打印到控制台,但我不知道如何在 Forecast.io 中实际使用它们。如有任何帮助,我们将不胜感激。
您不再需要使用位置变量,因此您进行 if let
检查。
试试这个:
if let loc = location {
let coordinate = (lat: loc.coordinate.latitude, long: loc.coordinate.longitude)
// ...your web API request stuff using coordinate....
}