2

下面是我用来获取当前用户位置的代码:

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)")
    }


}

位置状态正常。

错误:错误域=kCLErrorDomain 代码=0“(空)”

上面的代码适用于模拟器中的前 5 个测试,之后开始抛出上述错误并且位置永远不会更新。

有人可以帮忙吗?

在此处输入图像描述

4

0 回答 0