IOS Swift获取当前位置更新

时间:2016-01-21 21:18:55

标签: ios swift background location

我有一个关于定期更新我的位置的问题,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

 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 我教过如果位置改变,位置将会更新

1 个答案:

答案 0 :(得分:0)

  

我找到了简单的解决方案,只需启动一个新的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()

} `