我正在 Xcode 中使用 Google Map SDK 制作一个单一 View 应用程序。我已按照在线说明进行操作,我的应用程序可以成功加载谷歌地图 View 。我还启用了 myLocation
,以便 myLocation
按钮显示在 map View 上。
我知道单击 myLocation 按钮会自动更改相机位置,但我想知道应该如何使用 myLocation
的数据(例如添加标记或添加路径节点) )?
例如,我尝试直接访问 mapView.myLocation
let lat = mapView.myLocation?.coordinate.latitude
let long = mapView.myLocation?.coordinate.longitude
path.addCoordinate(CLLocationCoordinate2D(latitude: lat!, longitude: long!))
但是,这将使应用程序崩溃并抛出:
fatal error: unexpectedly found nil while unwrapping an Optional value
此错误消息意味着什么?我应该如何解决此问题?
最佳答案
错误表明mapView的myLocation
属性为零。您应该在访问之前检查是否有值。
if let myLocation = mapView.myLocation {
let lat = myLocation.coordinate.latitude
let long = myLocation.coordinate.longitude
path.addCoordinate(CLLocationCoordinate2D(latitude: lat, longitude: long))
}
还要验证为什么 myLocation
为零。可能是用户不允许定位服务。
关于ios - 如何使用适用于 iOS 的 Google Map SDK 将 myLocation 与 Swift 结合使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35806914/