CLLocationManager 不改变坐标?
CLLocationManager doesn't change coordinates?
我有两个不同的 classes,其中之一 LocationService 仅用于确定位置。问题是我不知道为什么它不更新坐标变量。
ViewController:
import UIKit
class ViewController: UIViewController {
var coords: (lat: Double, long: Double) = (0,0)
var tracking = LocationServices()
override func viewDidLoad() {
super.viewDidLoad()
tracking.startTracking()
tracking.getLocations()
coordinat = tracking.coordinates
retreiveWeatherForecast()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// retrieveWeatherForecast functions
和简单的定位服务class:
import UIKit
import CoreLocation
class LocationServices: NSObject, CLLocationManagerDelegate {
var locationManager = CLLocationManager()
var coordinates: (lat: Double, long: Double) = (0,0)
func startTracking() {
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestAlwaysAuthorization()
locationManager.startUpdatingLocation()
}
func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
locationManager.stopUpdatingLocation()
print(error)
}
func getLocations() {
#if os (ios)
func locationManager(manager:CLLocationManager, didUpdateLocations locations:[AnyObject]) {
//println("locations = \(locationManager)")
var latValue = locationManager.location.coordinate.latitude
var lonValue = locationManager.location.coordinate.longitude
coordinates = (latValue,lonValue)
println(latValue)
println(lonValue)
}
#endif
}
}
当我构建它时,它 returns 天气默认坐标 = (0,0)。
好吧,我不小心让它起作用了。据我了解,问题出在这里:func locationManager(manager:CLLocationManager, didUpdateLocations locations:[AnyObject])
位置必须是 CLLocation 类型,而不是 AnyObject。
我正在使用 Swift 2.0。
我有两个不同的 classes,其中之一 LocationService 仅用于确定位置。问题是我不知道为什么它不更新坐标变量。
ViewController:
import UIKit
class ViewController: UIViewController {
var coords: (lat: Double, long: Double) = (0,0)
var tracking = LocationServices()
override func viewDidLoad() {
super.viewDidLoad()
tracking.startTracking()
tracking.getLocations()
coordinat = tracking.coordinates
retreiveWeatherForecast()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// retrieveWeatherForecast functions
和简单的定位服务class:
import UIKit
import CoreLocation
class LocationServices: NSObject, CLLocationManagerDelegate {
var locationManager = CLLocationManager()
var coordinates: (lat: Double, long: Double) = (0,0)
func startTracking() {
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestAlwaysAuthorization()
locationManager.startUpdatingLocation()
}
func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
locationManager.stopUpdatingLocation()
print(error)
}
func getLocations() {
#if os (ios)
func locationManager(manager:CLLocationManager, didUpdateLocations locations:[AnyObject]) {
//println("locations = \(locationManager)")
var latValue = locationManager.location.coordinate.latitude
var lonValue = locationManager.location.coordinate.longitude
coordinates = (latValue,lonValue)
println(latValue)
println(lonValue)
}
#endif
}
}
当我构建它时,它 returns 天气默认坐标 = (0,0)。
好吧,我不小心让它起作用了。据我了解,问题出在这里:func locationManager(manager:CLLocationManager, didUpdateLocations locations:[AnyObject])
位置必须是 CLLocation 类型,而不是 AnyObject。
我正在使用 Swift 2.0。