我有一个关于定期更新我的位置的问题,我会定期调用 NSTimer
:
func timerUpdate() {
func locationManager1 (manager: CLLocationManager , didUpdateLocations locations: [CLLocation]) {
let locValue:CLLocationCoordinate2D = (manager.location?.coordinate)!
_ = locManager.location!
print(locValue)
}
//self.locManager.startUpdatingLocation()
self.loctimestamp.text = "\(locManager.location!.timestamp)"
// Check if the user allowed authorization
//let loc = CLLocation()
let kur = 999
let lat = locManager.location!.coordinate.latitude
let lon = locManager.location!.coordinate.longitude
let acc = locManager.desiredAccuracy
let age = 3
let urlComponents = NSURLComponents(string: "http://example.com/track/gps.php?kur=\(kur)&lon=\(lon)&lat=\(lat)&acc=\(acc)&age=\(age)")
let urler = urlComponents!.URL
let url = urler
let request = NSURLRequest(URL: url!)
//let html = NSData(contentsOfURL: url!)
let session = NSURLSession.sharedSession()
let task = session.dataTaskWithRequest(request, completionHandler: {data, response, error -> Void in
let html = NSString(data: (data)!, encoding: NSUTF8StringEncoding)
print(html)
print("Response: \(response)")
self.debug.text = ("Response: \(response)")
self.debug1.text = String(html)
}
)
task.resume()
Status.text = "an"
//locManager.stopUpdatingLocation()
}
在 viewdidload
with
self.locManager.startUpdatingLocation()
if #available(iOS 8.0, *) {
if (CLLocationManager.authorizationStatus() == CLAuthorizationStatus.AuthorizedWhenInUse ||
CLLocationManager.authorizationStatus() == CLAuthorizationStatus.Authorized)
{
//locManager.distanceFilter.hashValue:100
locManager.pausesLocationUpdatesAutomatically = false
self.locManager.delegate = self
self.locManager.desiredAccuracy = kCLLocationAccuracyBest
self.locManager.requestAlwaysAuthorization()
// self.mapView.showUserLocation = true
if #available(iOS 9.0, *) {
locManager.allowsBackgroundLocationUpdates = true
} else {
// Fallback on earlier versions
}
等等,
但我只是成为我启动应用程序时的位置
startUpdateLocation 运行,但为什么只收到
coordinate.timestamp
一个用于locManager.location!.coordinate.latitude
我教过如果位置更改,位置将更新
最佳答案
I Found the simple Solution just initiate a new
CLLocationManager()
函数 timerUpdate() {
->>> let loco = CLLocationManager() !!!
// Check if the user allowed authorization
loctimestamp.text = "\(loco.location!.timestamp)"
//let loc = CLLocation()
let kur = 999
let lat = loco.location!.coordinate.latitude
let lon = loco.location!.coordinate.longitude
let acc = loco.desiredAccuracy
let age = 3
let urlComponents = NSURLComponents(string: "http://example.com/track/gps.php?kur=\(kur)&lon=\(lon)&lat=\(lat)&acc=\(acc)&age=\(age)")
let urler = urlComponents!.URL
let url = urler
let request = NSURLRequest(URL: url!)
//let html = NSData(contentsOfURL: url!)
let session = NSURLSession.sharedSession()
let task = session.dataTaskWithRequest(request, completionHandler: {data, response, error -> Void in
let html = NSString(data: (data)!, encoding: NSUTF8StringEncoding)
print(html)
print("Response: \(response)")
self.debug.text = ("Response: \(response)")
self.debug1.text = String(html)
}
)
task.resume()
Status.text = "an"
//locManager.stopUpdatingLocation()
} `
关于IOS Swift 获取当前位置更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34934560/