Googlemaps不允许我找到用户位置?

时间:2018-06-11 21:39:01

标签: swift firebase firebase-authentication gmsmapview

我正在尝试在swift上创建一个显示用户所在位置的视图控制器。我已经实现了谷歌地图,所以现在我所要做的就是插入正确的代码。这样做,我不断得到这两个错误消息然后应用程序崩溃。有人可以帮我找出解决方案>任何和所有的帮助表示赞赏。

import UIKit
import Foundation
import Firebase
import MapKit
import GoogleMaps
import CoreLocation

class mainViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {

    let defaults = UserDefaults.standard
    let locationManager = CLLocationManager()
    var mapView = GMSMapView()
    var camera = GMSCameraPosition()


    override func viewDidLoad() {
        super.viewDidLoad()

        self.locationManager.delegate = self
        self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
        self.locationManager.requestWhenInUseAuthorization()
        self.locationManager.startUpdatingLocation()

        GMSServices.provideAPIKey("AIzaSyBDOLisA3c-wDTbkbSssAxEb3iLw7Y5vHo")

        let camera = GMSCameraPosition.camera(withLatitude: (self.locationManager.location?.coordinate.latitude)!, longitude: (self.locationManager.location?.coordinate.latitude)!, zoom: 17)
        let mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
        view = mapView

        let marker = GMSMarker()
        marker.position = CLLocationCoordinate2D(latitude: (self.locationManager.location?.coordinate.latitude)!, longitude: (self.locationManager.location?.coordinate.latitude)!)
        marker.snippet = "Current Location"
        marker.map = mapView
        self.mapView.addSubview(mapView)

        view.backgroundColor = GREEN_Theme
        navigationController?.navigationBar.prefersLargeTitles = true
        navigationItem.title = "Welcome"
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Logout", style: .plain, target: self, action: #selector(Logout))
        }
    @objc func Logout() {
        print("Logged Out")
        do {

// I am receiving this error message on the auth.auth().signOut() "Use of unresolved identifier 'Auth'"
            try  Auth.auth().signOut()
            defaults.set(false, forKey: "user is logged in")
            let loginController = UINavigationController(rootViewController: LoginController())
            present(loginController, animated: true, completion: nil)
        } catch let err {
            print(err.localizedDescription)
        }

    }
}

2 个答案:

答案 0 :(得分:0)

您的问题是CLLocationManager没有足够的时间来获取信息,同时其他功能要求的信息仍为零。

下面会注意这个问题,它也会一直停止更新可能耗尽电量的位置,特别是考虑到你已经设置了AccuracyBest。

func getLocation(){
    locationManager=CLLocationManager()
    locationManager.delegate = self
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.requestWhenInUseAuthorization()
    locationManager.startUpdatingLocation()
}

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]){
    let lastLocation=locations[locations.count-1]
    if lastLocation.horizontalAccuracy>0{
        locationManager.stopUpdatingLocation()
        let latitude = lastLocation.coordinate.latitude
        let longitude = lastLocation.coordinate.longitude

        GMSServices.provideAPIKey("AIzaSyBDOLisA3c-wDTbkbSssAxEb3iLw7Y5vHo")
// everything that is going to require the latitude and longitude from the location manager goes here
        let camera = GMSCameraPosition.camera(withLatitude: (self.locationManager.location?.coordinate.latitude)!, longitude: (self.locationManager.location?.coordinate.latitude)!, zoom: 17)
        let mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
        self.view = mapView

        let marker = GMSMarker()
         marker.position = CLLocationCoordinate2D(latitude: (self.locationManager.location?.coordinate.latitude)!, longitude: (self.locationManager.location?.coordinate.latitude)!)
         marker.snippet = "Current Location"
         marker.map = mapView
         self.mapView.addSubview(mapView)
      }
 }

您的viewDidLoad应该:

 override func viewDidLoad() {
    super.viewDidLoad()

    getLocation()

    view.backgroundColor = GREEN_Theme
    navigationController?.navigationBar.prefersLargeTitles = true
    navigationItem.title = "Welcome"
    navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Logout", style: .plain, target: self, action: #selector(Logout))
    } 

答案 1 :(得分:0)

您正在使用Google地图作为地图视图,这意味着您要创建GMSMapView类的实例。这是一个对象。你有一个。而且我认为它是IBOutlet-wired。它带有几种委托方法。所以你可能想设置它的委托。并且您希望视图控制器从GMSMapView接收数据。因此,您将该类的委托设置为self(您的视图控制器)。

import UIKit
import Foundation
import Firebase
import GoogleMaps
import CoreLocation

class mainViewController: UIViewController, CLLocationManagerDelegate, GMSMapViewDelegate {
    // MARK: - Instance variables
    private let locationManager = CLLocationManager()

    // MARK: - IBOutlets
    @IBOutlet weak var mapView: GMSMapView!

    // MARK: - IBActions

    // MARK: - Life cycle
    override func viewDidLoad() {
        super.viewDidLoad()

        mapView.delegate = self
        mapView.isMyLocationEnabled = true
    }
    // MARK: - Life cycle


    // MARK: - GMSMapView delegate methods
    func mapView(_ mapView: GMSMapView, idleAt position: GMSCameraPosition) {
        reverseGeocodeCoordinate(position.target) // sending data when the mapView is not moved or pinched by the finger //
    }

    func reverseGeocodeCoordinate(_ coordinate: CLLocationCoordinate2D) {
        let geocoder = GMSGeocoder()
        geocoder.reverseGeocodeCoordinate(coordinate) { response, error in
            guard let address = response?.firstResult(), let lines = address.lines else {
                return
            }

            ...
            ...
        }
    }
}