Search code examples
swift4google-maps-sdk-iosxcode10

Google Maps strange behavior swift 4 ios


Update: Dose it have to do anything with language? as my app is sporting English and Arabic.

Hi everyone I am struggling with this since days and I have tried everything but can not find any solution, the problem is when I run the app through xCode it all works fine like in the following screenshot. working maps

Then when i unplug the device and reopen the app it stops working, the app is running fine but i dont see any map but just the markers like the following screenshot. not working map

Any help will be appreciated.

AppDelegate

 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    GMSServices.provideAPIKey(googleApiKey)
    GMSPlacesClient.provideAPIKey(googleApiKey)       
    return true

}

ViewController

     //Step 1
     @IBOutlet weak var mapview: GMSMapView!

      //Step 2
     override func viewDidAppear(_ animated: Bool) {
            super.viewDidAppear(animated)
            self.mapview.settings.scrollGestures = true
            self.mapview.settings.zoomGestures = true
            self.mapview.isMyLocationEnabled = true
            self.mapview.settings.myLocationButton = true
            self.mapview.delegate = self
            self.mapview.layoutIfNeeded()

        }

    }

    //Step 3 After we have the current location items are loaded from server by user location and the markers are added in GetBusinesses function
    extension ItemsList: CLLocationManagerDelegate {
        func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
            guard status == .authorizedWhenInUse else {
                return
            }
                self.locationManager.startUpdatingLocation()

                self.mapview.camera = GMSCameraPosition(target:  (self.locationManager.location?.coordinate)!, zoom: 14, bearing: 0, viewingAngle: 0)
                self.GetBusinesses(data: "&isOpen=2&cat=\(Prefrences.getSelectedCategoryId())")
        }
    }

Solution

  • Okay so after struggling many days and trying every stupid possible way to fix it, I finally found a solution to it. The main problem was if your app is supporting multiple languages then google map will show this kind of behaviour. Following are the steps to fix it.

    Step 1: If you have the GMSMapView view in your viewController, break the outlets of your GMSMapView since it will be created dynamically.

    Step 2: Set the application language as per the user settings.

    Step 3: Create custom GMSMapView object set delegate methods and other necessary settings and add it to its container.

    That's it, you are good to go. What I understood from this is that you have to set your app language first before creating the GMSMapView (I can be wrong as well). If anyone knows why this method is working kindly explain it.

        //Our Custom MapView Var
        var mapview: GMSMapView!
    
    
        //View viewDidLoad() function
        override func viewDidLoad() {
                super.viewDidLoad()
                /*
                  TODO: 
                  Get selected language from preferences and set accordingly. 
                */
                UserDefaults.standard.set(["en"], forKey: "AppleLanguages")
                UserDefaults.standard.synchronize()
    
                let camera = GMSCameraPosition.camera(withLatitude: 55.277397,
                                                      longitude: 25.199514,
                                                      zoom:12)
                self.mapview = GMSMapView.map(withFrame: self.mMapContainer.bounds, camera: camera)
                self.mapview.delegate = self
                self.mapview.isMyLocationEnabled = true
                self.mapview.settings.myLocationButton = true
                self.mapview.isMyLocationEnabled = true
                self.mapview.autoresizingMask = [.flexibleWidth, .flexibleHeight]
                mMapContainer.addSubview(self.mapview)
    }