Reputation: 7558
I am using the Google Maps SDK for iOS and would like to know the user's current location. According to the documentation, the myLocation property on GMSMapView will return a nil value if no location is available:
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.
However, when using Swift if it's not possible to check for nil as the field is declared as non-nil. Of course, if the field is nil at run time this means the app will crash.
Does anyone know how to get around this? I would like to know if the location is not nil and, if not, use the value in my code.
Upvotes: 0
Views: 933
Reputation: 540105
it's not possible to check for nil
This is not correct. myLocation
is an implicitly unwrapped optional, and those can be compared with nil
, e.g.
let location = self.mapView.myLocation
if location != nil {
// Use location ...
}
Upvotes: 1
Reputation: 7558
It looks like you can do this using Key Value Coding.
// Will *not* work as 'myLocation' declared as non-nil
if let location = self.mapView.myLocation {
...
}
// Works using KVO.
if let location = self.MapView.valueForKeyPath("myLocation") as? CLLocation {
...
}
Upvotes: 0