iOS。 CLLocationManager在didUpdateLocations中仅接收一次位置更新 [英] iOS. CLLocationManager receives location update only once in didUpdateLocations

查看:293
本文介绍了iOS。 CLLocationManager在didUpdateLocations中仅接收一次位置更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码来获取位置更新(iOS 7):

I have the following code to get location updates (iOS 7):

import UIKit
import CoreLocation

class FirstViewController: UIViewController, CLLocationManagerDelegate {

    var locationManager: CLLocationManager!

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    @IBAction func startTracking(sender : AnyObject) {
        NSLog("Start tracking")
        if (locationManager == nil) {
            locationManager = CLLocationManager()
            locationManager.delegate = self
            locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation
            locationManager.distanceFilter = kCLDistanceFilterNone
            locationManager.pausesLocationUpdatesAutomatically = false
        }

        locationManager.startUpdatingLocation()
    }

    @IBAction func stopTracking(sender : AnyObject) {
        NSLog("Stop tracking")
        stopUpdatingLocation()
    }

    func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!) {
        NSLog("Error" + error.description)
    }

    func locationManager(manager:CLLocationManager, didUpdateLocations locations:AnyObject[]) {
        println("locations = \(locations)")
    }

    func locationManager(manager: CLLocationManager!,
        didChangeAuthorizationStatus status: CLAuthorizationStatus) {

        switch status {
            case CLAuthorizationStatus.Restricted:
                locationStatus = "Access: Restricted"
                break
            case CLAuthorizationStatus.Denied:
                locationStatus = "Access: Denied"
                break
            case CLAuthorizationStatus.NotDetermined:
                locationStatus = "Access: NotDetermined"
                shouldIAllow = true
                break
            default:
                locationStatus = "Access: Allowed"
                shouldIAllow = true
        }
        NSLog(locationStatus)
    }
}

我在 didUpdateLocations 中只有一个更新:在调用 startTracking 之后didUpdateLocations 将仅被调用一次,然后在5秒钟内GPS指示消失。

I get only one update in the didUpdateLocations: after calling startTracking the didUpdateLocations will be called only once and in 5 seconds GPS indicator disappears.

一些详细信息:


  • 应用程序被授权使用位置服务

  • 应用程序处于前台

  • 足够有趣:如果我在 didUpdateLocations 中放置断点,它将被命中4-5次。

  • application is authorized to use location services
  • application is in foreground
  • interestingly enough: if I put breakpoint in the didUpdateLocations it will be hit 4 - 5 time.

我在这里看到了类似问题的答案(例如在Swift中实施CLLocationManagerDelegate方法),但对我来说仍然无效。

I have seen answers to the similar questions here (like Implement CLLocationManagerDelegate methods in Swift), but it still doesn't work for me.

我在做什么错?

谢谢!

推荐答案

Xcode 6为Location添加了很多内容服务。

Xcode 6 made lots of additions to Location Services.

1)您需要更改info.plist文件,使其包含字符串 NSLocationWhenInUseUsageDescription键。此值将是您的应用程序打开定位服务的理由:使用定位服务跟踪您的跑步。

1) You need to change your info.plist file to include the String "NSLocationWhenInUseUsageDescription" key. The value of this will be your applications reasoning for turning on location services: "Using location services to track your run".

2)使用 [self.locationManager requestWhenInUseAuthorization] ;

2) use "[self.locationManager requestWhenInUseAuthorization];" in your code to cause the pop-up to appear with your above string.

更多信息在WWDC 2014视频上。

More info is on the WWDC 2014 videos.

这篇关于iOS。 CLLocationManager在didUpdateLocations中仅接收一次位置更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆