简体   繁体   中英

Get current location in ios 9 using Google maps sdk

I am using Google maps SDK with swift 3.I want to get the current location on a map but I am not able to get current location in the real device but in the simulator, it shows the location of apple.But on the real device, it shows the current location of set in the app scheme otherwise it shows the error " Error: Error Domain=kCLErrorDomain Code=0 "(null)" ". I am using below code to get the current location.Please help me.This code works in ios 10 but not work in ios 9

 @IBOutlet weak var view_map: GMSMapView!
    let locationManager = CLLocationManager()
    var didFindMyLocation = false
    override func viewDidLoad() {
        super.viewDidLoad()
        view_map.isMyLocationEnabled = true
//        //Location Manager code to fetch current location
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.requestWhenInUseAuthorization()
        if #available(iOS 9.0, *) {
            locationManager.allowsBackgroundLocationUpdates = true
            locationManager.requestLocation()

        }
        locationManager.startUpdatingLocation()
    }
    //Location Manager delegates
       func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        let location = locations.last

        let camera = GMSCameraPosition.camera(withLatitude: (location?.coordinate.latitude)!, longitude: (location?.coordinate.longitude)!, zoom: 17.0)

        self.view_map.animate(to: camera)

        //Finally stop updating location otherwise it will come again and again in this delegate
        self.locationManager.stopUpdatingLocation()

    }
    // 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.
            view_map.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)")
    }
//Add these in your info.plist
<key>NSLocationAlwaysUsageDescription</key>
    <string>Reason to access location background usage description</string>
    <key>NSLocationWhenInUseUsageDescription</key>
    <string>Reason to access location usage description</string>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM