I have my google maps view embedded in a TabBar Controller and it is the second item in the tab. The view loads the map but it doesn't display the current location or the locate button. I have already edited the scheme to include a location for the build to use.
var locationManager = CLLocationManager()
var didFindMyLocation = false
override func viewDidLoad() {
super.viewDidLoad()
var camera = GMSCameraPosition.cameraWithLatitude(33.7,
longitude: -77.4, zoom: 8.0)
mapView.camera = camera
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
mapView.addObserver(self, forKeyPath: "myLocation", options: NSKeyValueObservingOptions.New, context: nil)
// Do any additional setup after loading the view.
}
func locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
if status == CLAuthorizationStatus.AuthorizedWhenInUse {
mapView.myLocationEnabled = true
}
}
override func observeValueForKeyPath(keyPath: String, ofObject object: AnyObject, change: [NSObject : AnyObject], context: UnsafeMutablePointer<Void>) {
if !didFindMyLocation {
let myLocation: CLLocation = change[NSKeyValueChangeNewKey] as! CLLocation
mapView.camera = GMSCameraPosition.cameraWithTarget(myLocation.coordinate, zoom: 10.0)
mapView.settings.myLocationButton = true
didFindMyLocation = true
}
}
If location permission was previously set func locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus)
probably isn't being called because the authorization status is not changing.
You can also just manually perform a check for authorization and then enable the location on the map
if CLLocationManager.locationServicesEnabled()
&& CLLocationManager.authorizationStatus() == CLAuthorizationStatus.AuthorizedWhenInUse {
mapView.myLocationEnabled = true
}