2016-01-21 42 views
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運行成爲了位置,但爲何我教,如果位置有所改變

+0

我建議你RTFM,並開始過來。您不需要計時器,並且您不會將方法didUpdateLocations放入該定時器選擇器方法 –

+0

中,因爲我需要定時器來定期處理請求! ,但我也需要我的請求字符串中的當前位置,但感謝您的回答 – gungott

+0

也許我做了我的問題錯了:我需要一個自動,定期的位置更新,.. NSUrl請求工作正常,現在我只需要當前位置在間隔時間 – gungott

回答

0

我發現的位置將被更新,接收只是一個coordinate.timestamp一個locManager.location!.coordinate.latitude 簡單的解決方案只是開始新的CLLocationManager()

FUNC 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() 

} `