首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >LocationManager位置

LocationManager位置
EN

Stack Overflow用户
提问于 2016-08-30 02:00:20
回答 1查看 413关注 0票数 0

我有一个列表,其中填充了来自API的数据。所以基本上这个过程是这样的:

当用户打开应用程序时,我会这样做:

代码语言:javascript
复制
if CLLocationManager.locationServicesEnabled() {
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.distanceFilter = 20
        locationManager.startUpdatingLocation()
    }

然后在我的func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation])中,我调用API来获取数据。

但问题是,func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation])有时可以被调用5-6次,所以会有很多API调用,如果我只得到一个位置,那么我得到一个远离用户的位置的可能性很大。

对如何解决这个问题有什么想法吗?我基本上想要最好的位置,并尽可能少地进行API调用,最好是这样。

EN

回答 1

Stack Overflow用户

发布于 2016-09-01 03:41:55

基本上,如果你想避免在func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation])中调用API,你需要考虑一些控制:

在received locations

  • "Improve“
  • horizontalAccuracydistanceFilter values

之间创建一个时间间隔

记住,你总是需要避免:

在启动locationManager)之前,避免locations

  • Invalid locations

  • Invalid locations

  • Out-of-order location

  • Cached位置为空的

代码示例:

代码语言:javascript
复制
protocol MyLocationManagerDelegate {
    func locationControllerDidUpdateLocation(location: CLLocation)
}

final class MyLocationManager: NSObject, CLLocationManagerDelegate {

    var oldLocation: CLLocation?
    let kTimeFilter = Double(10)                  //avoid locations for each kTimeFilter seconds
    let kValidDistanceToOldLocation = Double(100)  //avoid location less than kValidDistanceToOldLocation meters
    var startDate: NSDate?
    var delegate: MyLocationManagerDelegate?


    func isValidLocation(newLocation newLocation: CLLocation?, oldLocation: CLLocation?) -> Bool {

        // avoid nil locations
        if newLocation == nil {
            return false
        }

        //avoid invalid locations
        if newLocation!.coordinate.latitude == 0 || newLocation!.coordinate.longitude == 0 {
            return false
        }

        //avoid invalid locations
        if (newLocation!.horizontalAccuracy < 0){
            return false
        }

        if oldLocation != nil {

            let distance = newLocation!.distanceFromLocation(oldLocation!)

            if fabs(distance) < kValidDistanceToOldLocation {
                return false
            }

            //avoid out-of-order location.
            let secondsSinceLastPoint = newLocation!.timestamp.timeIntervalSinceDate(oldLocation!.timestamp)
            if secondsSinceLastPoint < 0 || secondsSinceLastPoint < kTimeFilter {
                return false
            }
        }

        //avoid cached locations (before you start the locationManager)
        let secondsSinceManagerStarted = newLocation!.timestamp.timeIntervalSinceDate(startDate!)
        if secondsSinceManagerStarted < 0 {
            return false
        }

        return true
    }

    func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

        let newLocation = locations.last

        if isValidLocation(newLocation: newLocation, oldLocation:oldLocation) || oldLocation == nil {
            self.delegate?.locationControllerDidUpdateLocation(newLocation!)
            oldLocation = newLocation
        }

    }

}

class ViewController: UIViewController, MyLocationManagerDelegate {


    override func viewDidLoad() {
        super.viewDidLoad()

        let location = MyLocationManager()
        location.delegate = self

    }

    func locationControllerDidUpdateLocation(location: CLLocation) {
        //Api Call
    }

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39212373

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档