简体   繁体   English

iOS Swift获取当前位置更新

[英]IOS Swift get current location update

I have a question about periodic updating my location,with NSTimer i call periodically : 我有关于定期更新我的位置的问题,我会定期致电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()

}

this work fine after here come the viewdidload with 这项工作很好,这里是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
            }

and so on , 等等 ,

  • but i just become the location from when i start the app 但是我只是从启动应用程序开始就成为该位置

  • startUpdateLocation run but why a receive just an coordinate.timestamp one for locManager.location!.coordinate.latitude i taught the location will be updated if location is changed startUpdateLocation运行,但是为什么我只教了locManager.location!.coordinate.latitude我收到的位置的coordinate.timestamp ,如果位置改变了

I Found the simple Solution just initiate a new CLLocationManager() 我发现简单的解决方案只是启动一个新的CLLocationManager()

func timerUpdate() { 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()

} ` }`

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM