1
我有定期更新我的位置的問題,與NSTimer
我定期 撥打:IOS斯威夫特獲得當前位置更新
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運行成爲了位置,但爲何我教,如果位置有所改變
我建議你RTFM,並開始過來。您不需要計時器,並且您不會將方法didUpdateLocations放入該定時器選擇器方法 –
中,因爲我需要定時器來定期處理請求! ,但我也需要我的請求字符串中的當前位置,但感謝您的回答 – gungott
也許我做了我的問題錯了:我需要一個自動,定期的位置更新,.. NSUrl請求工作正常,現在我只需要當前位置在間隔時間 – gungott