mapView:GMSMapView! nil导致应用崩溃(迅速)

问题描述 投票:0回答:1

关于使用iOS版Google Maps SKD,我是一个全新的人,我遇到以下问题:我的应用程序使用Firebase,如果用户登录,则MapVC是第一个VC。我在情节提要中设置了UIVIew我想要的大小并使它的类为GMSMapView。我还使用了CLLocation Manager,位置正常,将其传递给camera变量就可以了。但是,如果添加此代码:mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)或此代码:self.mapView.camera = camera,则由于mapView为nil,应用程序将崩溃。如果我不添加这些代码行,则显示的地图只是默认地图(欧洲)。我需要的是地图摄像头,以显示我从CLLocation获取的位置,并随着位置的变化在地图上对其进行更新。

我的MapVC代码如下:

import UIKit
import GoogleMaps
import GooglePlaces
import CoreLocation
import Alamofire
import Firebase
import FirebaseFirestore

class AlertaVC: UIViewController, CLLocationManagerDelegate {

    @IBOutlet weak var alertaBtn: RoundButton!
    var latitude: CLLocationSpeed?
    var longitude: CLLocationDegrees?
    var mapView: GoogleMaps.GMSMapView!
    var db: Firestore!
    var userID: String?
    var nameUser: Any?


    let locationManager = CLLocationManager()

    @IBAction func dispararAlertaBtn(_ sender: UIButton) {
        //changing the title and color after the alert is sent
        alertaBtn.isSelected = !alertaBtn.isSelected

        if alertaBtn.isSelected {

            //setting up twilio to send the alert SMS
            let headers: HTTPHeaders = [
                "Content-Type": "application/x-www-form-urlencoded"
            ]
            //add the phone numbers from the firebase document
            let parameters: Parameters = [
                "To": "phone-number",
                "Body": "Ola, to enviando SMS!"
            ]
            //change the SMS body to include the user's name and the link with live location
            AF.request("https://url.twil.io/smsAlerta", method: .post, parameters: parameters, headers: headers).responseJSON { response in
                print(response.response as Any, "response alamofire")
            }

            alertaBtn.setTitle("Encerrar alerta!", for: .normal)
            alertaBtn.backgroundColor = UIColor(red: 0.83529, green: 0.4, blue: 0.5725490196, alpha: 1.0)
        } else {
            alertaBtn.setTitle("Disparar alerta!", for: .normal)
            alertaBtn.backgroundColor = UIColor(red: 0.3254, green: 0.1921, blue: 0.2627, alpha: 1.0)
        }

    }


    override func viewDidLoad() {
        super.viewDidLoad()

        locationManager.requestAlwaysAuthorization()
        locationManager.requestWhenInUseAuthorization()
        if CLLocationManager.locationServicesEnabled() {
            locationManager.delegate = self
            locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
            locationManager.startUpdatingLocation()
        }
        db = Firestore.firestore()

        let user = Auth.auth().currentUser
        if let user = user {
          // The user's ID, unique to the Firebase project.
            self.userID = user.uid
            print(self.userID!, "user ID firebase")
        }
    }

    override func viewWillAppear(_ animated: Bool) {
        //showCurrentLocation() Tried calling the function @ViewWillAppear but it didn't work either
    }

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        guard let locValue: CLLocationCoordinate2D = manager.location?.coordinate else { return }
        latitude = locValue.latitude
        longitude = locValue.longitude

        let camera: GMSCameraPosition = GMSCameraPosition.camera(withLatitude: latitude ?? 19.741755, longitude: longitude ?? -155.844437, zoom: 7.0) //if doesnt load will show Hawaii
        print(camera as Any, "camera") <-- WORKS FINE: GMSCameraPosition 0x600000f389f0: target:(37.332, -122.031) bearing:0.000 zoomLevel:7.000 viewingAngle:0.000 camera
        self.mapView.camera = camera <<--- CRASHES HERE: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value

        self.mapView?.animate(to: camera) <<-- WITHOUT THE LINE ABOVE THIS ANIMATION DOESN`T HAPPEN, BECAUSE MAPVIEW IS NIL

        print("locations = \(latitude ?? 56.56), \(longitude ?? 45.45)")
    }


    func showCurrentLocation() { <<- NOT BEING CALLED, BUT IF IT IS, CRASHES
        //mapView.settings.myLocationButton = true
        let locationObj = locationManager.location!
        let coord = locationObj.coordinate
        let lattitude = coord.latitude
        let longitude = coord.longitude
        print(" lat in  updating \(lattitude) ")
        print(" long in  updating \(longitude)")

        let center = CLLocationCoordinate2D(latitude: locationObj.coordinate.latitude, longitude: locationObj.coordinate.longitude)
        let marker = GMSMarker()
        marker.position = center
        marker.title = "current location"
        marker.map = mapView
        let camera: GMSCameraPosition = GMSCameraPosition.camera(withLatitude: lattitude, longitude: longitude, zoom: 7.0)
        mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera) <-- CRASHES HERE
        self.mapView.animate(to: camera)
    }

    func fetchUserName(userId: String) {
        db.collection("Usuarias").document(self.userID!).getDocument() {
            (document, err) in
           if let document = document, document.exists {
            let dataDescription = document.data()!["Nome"] ?? "nil"
                print("Document data: \(dataDescription)")
            self.nameUser = dataDescription
            print(self.nameUser as Any, "dentro do fetch")
            } else {
                print("Document does not exist")
            }
        }
    }

}

非常感谢您的帮助。谢谢=)

ios swift cllocationmanager google-maps-sdk-ios gmsmapview
1个回答
0
投票

[如果您在情节提要中创建了'View'并将其分配为'GMSMapView',则应在ViewController中将其添加为'IBOutlet',您可以通过单击键盘上的'Option',然后像您一样从地图上拖动到ViewController来完成此操作'请添加“ alertaBtn”,如果要以编程方式创建地图,则需要以“ viewDidLoad”方法启动它,然后使用它来设置相机或进行其他更改。

© www.soinside.com 2019 - 2024. All rights reserved.