ios - 带有 Swift 的 Google map SDK,myLocation 可以为零

标签 ios swift google-maps

我正在使用适用于 iOS 的 Google Maps SDK,并且想知道用户的当前位置。根据文档,myLocation属性(property) GMSMapView如果没有位置可用,将返回 nil 值:

If My Location is enabled, reveals where the user location dot is being drawn. If it is disabled, or it is enabled but no location data is available, this will be nil.

但是,当使用 Swift 时,如果字段被声明为非 nil,则无法检查 nil。当然,如果该字段在运行时为 nil,这意味着应用程序将崩溃。

Google Maps SDK in XCode

有谁知道如何解决这个问题?我想知道该位置是否不为零,如果不是,则使用我的代码中的值。

最佳答案

it's not possible to check for nil

这是不正确的。 myLocation 是一个隐式展开的可选,它们可以与 nil 进行比较,例如

let location = self.mapView.myLocation
if location != nil {
    // Use location ...
}

关于ios - 带有 Swift 的 Google map SDK,myLocation 可以为零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31921991/

相关文章:

java - 将图钉放置在 fragment 中的 map fragment 上

ios - Swift 4 URL 下载不起作用

javascript - 标记.setMap(null);不起作用

ios - 滚动 UITableView 以关闭 UIView Above

ios - uitableview 页脚中的超链接

ios - Parse,iOS,includeKey 查询不检索指针对象的属性

ios - 如何在加载数据后安全地解包 TableView 单元格

javascript - Angular 2 Google Maps Javascript API,路由服务功能不存在

ios - UITableView - 在编辑模式下重复使用单元格

ios - 如何在 tvOS 上深度链接到 Netflix?