【问题标题】:how convert locations: [CLLocation] to Coordinate2D?如何将位置:[CLLocation] 转换为 Coordinate2D?
【发布时间】:2017-12-08 08:55:53
【问题描述】:

cat如何获取didUpdateLocation委托的坐标位置?

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

请帮帮我。谢谢

【问题讨论】:

  • CLLocation().coordinate ?
  • developer.apple.com/documentation/corelocation/cllocation/… 这是一个简单的属性。然后检查数组的第一个还是最后一个值?
  • 我想要第一个位置的拳头值
  • @user9071596,let coordinate: CLLocationCoordinate2D? = locations.first?.coordinate,可能。

标签: ios swift cllocationmanager cllocation cllocationcoordinate2d


【解决方案1】:
 func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

 location = locations.last!
}

【讨论】:

    【解决方案2】:

    这个问题的简单答案:

    (locations.first?.coordinate)!
    

    【讨论】:

    • 为什么要先使用?,然后再使用!?为什么不直接locations.first!.coordinate?或locations[0].coordinate?
    • @Sulthan 不幸的是,编译器建议在访问可选链表达式的非可选属性时使用该语法。而且很多人没有仔细检查编译器的建议?
    【解决方案3】:

    请在发帖前做一些研究。

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        let coordinate = locations.first?.coordinate
    }
    

    CLLocation Documentation

    位置管理器为您提供一组位置,只需获取第一个位置,然后访问坐标属性。

    【讨论】:

      【解决方案4】:
      func locationManager(manager: CLLocationManager!, didUpdateLocations locations: AnyObject[]!) {
              var locationArray = locations as NSArray
              var locationObj = locationArray.lastObject as CLLocation
              var coord = locationObj.coordinate
      
              println(coord.latitude)
              println(coord.longitude)
      }
      

      【讨论】:

        【解决方案5】:

        另一种方式:

        let lastLocation = locations.flatMap({ $0.coordinate }).last

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-03-07
          • 2015-04-24
          • 2023-03-31
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多