【发布时间】:2016-01-21 21:18:54
【问题描述】:
我有一个关于定期更新我的位置的问题,NSTimeri 定期致电
:
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 出现后,这项工作正常
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我教过如果位置发生变化,位置将会更新
【问题讨论】:
-
我建议您使用 RTFM 并重新开始。您不需要计时器,也不应该将方法 didUpdateLocations 放在该计时器选择器方法中
-
因为我需要一个计时器来处理我的定期请求! ,但我还需要我在请求字符串中的当前位置,但感谢您的回答
-
也许我的问题做错了:我需要一个自动、定期的位置更新,.. NSUrl 请求工作正常,现在我只需要间隔时间的当前位置
-
再次摆脱那个计时器。位置自然会不断更新。
标签: ios swift background location