2

Below is the code that I used for getting the current user location:

import UIKit
import GooglePlaces
import GoogleMaps
import CoreLocation

class ViewController: UIViewController, CLLocationManagerDelegate {

    @IBOutlet weak var googleMapView: GMSMapView!
    var placesClient: GMSPlacesClient!
    var zoomLevel: Float = 15.0

    var locationManager = CLLocationManager()
    var addressString : String = ""
    var lng : Double = 0.0
    var lat : Double = 0.0

    @IBOutlet weak var latitudeLabel: UILabel!
    @IBOutlet weak var longitudeLabel: UILabel!
    @IBOutlet weak var locationAddressLabel: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()

        locationManager.delegate = self
        locationManager.requestWhenInUseAuthorization()
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.startUpdatingLocation()
        placesClient = GMSPlacesClient.shared()

        // Do any additional setup after loading the view.
    }


    @IBAction func getcurrentLocationButton(_ sender: Any) {

        self.latitudeLabel.text = "\(self.lat)"
        self.longitudeLabel.text = "\(self.lng)"
        self.locationAddressLabel.text = "\(self.addressString)"

    }


    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        let location: CLLocation = locations.last!
        print("Location: \(location)")

        let camera = GMSCameraPosition.camera(withLatitude: location.coordinate.latitude,
                                              longitude: location.coordinate.longitude,
                                              zoom: zoomLevel)

        if googleMapView.isHidden {
            googleMapView.isHidden = false
            googleMapView.camera = camera
        } else {
            googleMapView.animate(to: camera)
        }

        lng = location.coordinate.longitude
        lat = location.coordinate.latitude



        print("Latitude :- \(location.coordinate.latitude)")
        print("Longitude :-\(location.coordinate.longitude)")

        let  placelocation = CLLocationCoordinate2D(latitude : location.coordinate.latitude, longitude:  location.coordinate.longitude)


        let marker = GMSMarker()
        marker.position = placelocation
        marker.map = self.googleMapView

        marker.title = "Current Location"

        let ceo: CLGeocoder = CLGeocoder()
        ceo.reverseGeocodeLocation(location, completionHandler:
            {(placemarks, error) in
                if (error != nil)
                {
                    print("reverse geodcode fail: \(error!.localizedDescription)")
                }
                let pm = placemarks! as [CLPlacemark]

                if pm.count > 0 {
                    let pm = placemarks![0]


                    if pm.subLocality != nil {
                        self.addressString = self.addressString + pm.subLocality! + ", "
                    }
                    if pm.thoroughfare != nil {
                        self.addressString = self.addressString + pm.thoroughfare! + ", "
                    }
                    if pm.locality != nil {
                        self.addressString = self.addressString + pm.locality! + ", "
                    }
                    if pm.country != nil {
                        self.addressString = self.addressString + pm.country! + ", "
                    }
                    if pm.postalCode != nil {
                        self.addressString = self.addressString + pm.postalCode! + " "
                    }

                    print(self.addressString)

                }
        })


    }

    // Handle authorization for the location manager.
    func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
        switch status {
        case .restricted:
            print("Location access was restricted.")
        case .denied:
            print("User denied access to location.")
            // Display the map using the default location.
            googleMapView.isHidden = false
        case .notDetermined:
            print("Location status not determined.")
        case .authorizedAlways: fallthrough
        case .authorizedWhenInUse:
            print("Location status is OK.")
        }
    }

    // Handle location manager errors.
    func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
        locationManager.stopUpdatingLocation()
        print("Error: \(error)")
    }


}

Location status is OK.

Error: Error Domain=kCLErrorDomain Code=0 "(null)"

The above code works for the first 5 tests in simulator after which starts to throw the above error and the location never gets updated.

Can somebody help ?

enter image description here

  • 1
    Can you please try the solutions in this thread and let us know if they work for you? https://stackoverflow.com/questions/32543754/ios-9-error-domain-kclerrordomain-code-0-null – evan Sep 20 '19 at 21:52
  • Thank you, I did some changes in the info.plist and it worked fine. Now even if set custom location, the correct customised location is showing. –  Sep 24 '19 at 07:25
  • 1
    Great thx for your update, I recommend you post your own answer for the community's sake. :) – evan Sep 24 '19 at 09:11

0 Answers0