CLLocationManager获取位置很慢,Swift

时间:2017-02-15 09:36:57

标签: swift mapkit cllocationmanager location-services

我正在创建这个应用程序,它需要获取用户位置 - 一切正常,事实是,从接受使用位置服务到获取实际位置的时间需要5秒 - 这是正常的? 我已经使用过其他应用程序了,它的速度要快得多......

以下是我的代码:

override func viewDidLoad() {
    super.viewDidLoad()

    // Ask for Location-Authorisation from the User.
    self.locationManager.requestWhenInUseAuthorization()

    if CLLocationManager.locationServicesEnabled() {
        locationManager.delegate = self
        locationManager.requestLocation()
    }

    mapView.delegate = self

}



func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    let locValue: CLLocationCoordinate2D = manager.location!.coordinate

    let initialLocation = CLLocation(latitude: locValue.latitude, longitude: locValue.longitude)

    self.centerMapOnLocation(initialLocation)
}

func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
    print("could not get location")
}

但是从应用程序获取放入centerMapOnLocation函数的位置的时间似乎只是很长。获取用户位置时会出现什么情况?我正在测试一个wifi连接,所以我知道这不是因为互联网很慢,或者连接不好......

有人有想法吗? :)

祝你好运!

4 个答案:

答案 0 :(得分:7)

尝试设置准确度并使用locationManager.startUpdatingLocation()。我这样做,并在一秒钟内(在设备上)得到答案。

答案 1 :(得分:3)

来自 requestLocation()

的文档
  

此方法立即返回。调用它会导致位置管理器获取位置修复(可能需要几秒钟)并使用结果调用委托的locationManager(_:didUpdateLocations :)方法。

Source

基本上,一切都很好用你的代码,它只是框架的构建方式。

答案 2 :(得分:1)

初始化位置管理器时,添加startUpdatingLocation():

let manager = CLLocationManager()
 manager.delegate = self
 manager.requestWhenInUseAuthorization()
 manager.requestLocation()
 manager.startUpdatingLocation()

如果没有 startUpdatingLocation() 地理定位需要大约 5 秒,有了它,请求几乎立即执行。

答案 3 :(得分:0)

如果您不想延迟位置管理器的应用程序启动,请考虑部署两个位置管理器(在应用程序委托中),其中一个任务是快速生成位置,另一个任务是准确生成位置:

// Jest Snapshot v1

exports[`ResultsList.vue should match snapshot 1`] = `
<div class="container container--fluid" title="Foobar">
  <div class="v-card v-sheet theme--light">
    <div class="v-card__title">
      Results
      <div class="spacer"></div>
      <div class="v-input v-input--hide-details theme--light v-text-field v-text-field--single-line">
        <div class="v-input__control">
          <div class="v-input__slot">
            <div class="v-text-field__slot"><label for="input-4" class="v-label theme--light" style="left: 0px; position: absolute;">Search</label><input id="input-4" type="text"></div>
            <div class="v-input__append-inner">
              <div class="v-input__icon v-input__icon--append"><i aria-hidden="true" class="v-icon notranslate mdi mdi-magnify theme--light"></i></div>
            </div>
          </div>
        </div>
      </div>
    </div>
    <div class="v-data-table theme--light">
      <div class="v-data-table__wrapper">
        <table>
          <colgroup>
            <col class="">
            <col class="">
            <col class="">
            <col class="">
            <col class="">
          </colgroup>
          <thead class="v-data-table-header">
            <tr>
              <th role="columnheader" scope="col" aria-label="Task" class="text-start"><span>Task</span></th>
              <th role="columnheader" scope="col" aria-label="State: Not sorted. Activate to sort ascending." aria-sort="none" class="text-start sortable"><span>State</span><i aria-hidden="true" class="v-icon notranslate v-data-table-header__icon mdi mdi-arrow-up theme--light" style="font-size: 18px;"></i></th>
              <th role="columnheader" scope="col" aria-label="Start Time: Not sorted. Activate to sort ascending." aria-sort="none" class="text-start sortable"><span>Start Time</span><i aria-hidden="true" class="v-icon notranslate v-data-table-header__icon mdi mdi-arrow-up theme--light" style="font-size: 18px;"></i></th>
              <th role="columnheader" scope="col" aria-label="End Time: Not sorted. Activate to sort ascending." aria-sort="none" class="text-start sortable"><span>End Time</span><i aria-hidden="true" class="v-icon notranslate v-data-table-header__icon mdi mdi-arrow-up theme--light" style="font-size: 18px;"></i></th>
              <th role="columnheader" scope="col" aria-label="Result: Not sorted. Activate to sort ascending." aria-sort="none" class="text-start sortable"><span>Result</span><i aria-hidden="true" class="v-icon notranslate v-data-table-header__icon mdi mdi-arrow-up theme--light" style="font-size: 18px;"></i></th>
            </tr>
          </thead>
          <tbody>
            <tr class="v-data-table__empty-wrapper">
              <td colspan="5">No data available</td>
            </tr>
          </tbody>
        </table>
      </div>
      <div class="v-data-footer">
        <div class="v-data-footer__select">Rows per page:<div class="v-input v-input--hide-details v-input--is-label-active v-input--is-dirty theme--light v-text-field v-select">
            <div class="v-input__control">
              <div role="button" aria-haspopup="listbox" aria-expanded="false" aria-owns="list-17" class="v-input__slot">
                <div class="v-select__slot">
                  <div class="v-select__selections">
                    <div class="v-select__selection v-select__selection--comma">10</div><input aria-label="$vuetify.dataTable.itemsPerPageText" id="input-17" readonly="readonly" type="text" aria-readonly="false" autocomplete="off">
                  </div>
                  <div class="v-input__append-inner">
                    <div class="v-input__icon v-input__icon--append"><i aria-hidden="true" class="v-icon notranslate mdi mdi-menu-down theme--light"></i></div>
                  </div><input type="hidden" value="10">
                </div>
                <div class="v-menu">
                  <!---->
                </div>
              </div>
            </div>
          </div>
        </div>
        <div class="v-data-footer__pagination">–</div>
        <div class="v-data-footer__icons-before"><button type="button" disabled="disabled" class="v-btn v-btn--disabled v-btn--flat v-btn--icon v-btn--round v-btn--text theme--light v-size--default" aria-label="Previous page"><span class="v-btn__content"><i aria-hidden="true" class="v-icon notranslate mdi mdi-chevron-left theme--light"></i></span></button></div>
        <div class="v-data-footer__icons-after"><button type="button" disabled="disabled" class="v-btn v-btn--disabled v-btn--flat v-btn--icon v-btn--round v-btn--text theme--light v-size--default" aria-label="Next page"><span class="v-btn__content"><i aria-hidden="true" class="v-icon notranslate mdi mdi-chevron-right theme--light"></i></span></button></div>
      </div>
    </div>
  </div>
</div>
`;

locA.delegate = self locA.desiredAccuracy = kCLLocationAccuracyThreeKilometers locA.requestWhenInUseAuthorization() locA.startUpdatingLocation() // quick and multiple locB.delegate = self locB.desiredAccuracy = kCLLocationAccuracyBest locB.requestWhenInUseAuthorization() locB.requestLocation() // slow and single 精度与3 km的组合应该返回一个位置,直到根视图控制器准备就绪为止。 startUpdatingLocation()经理很可能会在用户启动应用后很好地返回位置。

B

作为最后的保护层,您可以在客户端上保留位置信息:

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

    switch manager {

    case locA:

        locA.stopUpdatingLocation()
        deviceLocation = locations.last! // set property with estimated loc

    case locB:
        deviceLocation = locations.last! // overwrite property with accurate loc

    default:
        break

    }

}