Search code examples
swifttabbarcontroller

Google API Maps don't show up on TabBar Application


I am learning about Google Maps and it works perfectly in a SingleView Application. However I wanted to improve my app and have used a Tab Bar menu. I connected my ViewController with MapView to TabBarController and then map fails to load, there is just blank square instead of a map.

What did I do wrong?

EDIT:

class ViewController: UIViewController, CLLocationManagerDelegate {

@IBOutlet weak var mapView: GMSMapView!

let locationManager = CLLocationManager()

override func viewDidLoad() {

    super.viewDidLoad()
    self.locationManager.requestWhenInUseAuthorization()
    self.locationManager.delegate = self
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
    self.locationManager.startUpdatingLocation()
    self.locationManager.startMonitoringSignificantLocationChanges()    
}

//MARK: Shows Google Map
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

    self.locationManager.stopUpdatingLocation()
    let locationArray = locations as NSArray
    let locationObj = locationArray.lastObject as! CLLocation
    let coord = locationObj.coordinate

    mapView.camera = GMSCameraPosition(target: coord, zoom: 15, bearing: 0, viewingAngle: 0)

    locationManager.stopUpdatingLocation()
}

func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {

    if status == .AuthorizedWhenInUse {

        locationManager.startUpdatingLocation()

        mapView.myLocationEnabled = true
        mapView.settings.myLocationButton = true
    }
}

Solution

  • I think I found the problem, at least now everything works. So I moved

    mapView.myLocationEnabled = true
    mapView.settings.myLocationButton = true
    

    to func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) before locationManager.stopUpdatingLocation() and now it works.

    However I do have a feeling that my code is bad, it uses stopUpdatingLocation() twice. In my case does it mean, that app will get user location only once when the app laods?