IOS Swift 获取当前位置更新

标签 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 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/

相关文章:

ios - 无信息的崩溃报告

Iphone 应用程序升级检查

ios - 更改表格标题部分的字体

ios - TableView 中出现意外的非空返回值

iOS 进入深度 sleep 需要多长时间?

ios - swift 。正确初始化 UITableViewCell 层次结构

CSS背景图片定位

html - 一行中可以并排放置 2 张背景图片吗?

ios - UITextField - 当应用程序来自后台时键盘不隐藏

ios - react 性 cocoa : Response from HTTPRequest using RACCommand