【问题标题】:IOS Swift get current location updateIOS Swift 获取当前位置更新
【发布时间】: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


【解决方案1】:

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

} `

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-10-19
    • 2021-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多