繁体   English   中英

使用Google Maps SDK在iOS 9中获取当前位置

[英]Get current location in ios 9 using Google maps sdk

我正在使用Swift 3的Google Maps SDK。我想获取地图上的当前位置,但无法在真实设备中获取当前位置,但是在模拟器中却显示了苹果的位置。但是在真实设备上,则会显示set在应用方案中的当前位置,否则会显示错误“ 错误:错误域= kCLErrorDomain代码= 0“(null)” “。 我正在使用下面的代码来获取当前位置。请帮助我。此代码在ios 10中有效,但在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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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