我正在使用适用于 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,这意味着应用程序将崩溃。
有谁知道如何解决这个问题?我想知道该位置是否不为零,如果不是,则使用我的代码中的值。
最佳答案
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/