更新坐标时,GMS Mapview会闪烁,重新加载并崩溃

时间:2019-02-11 04:03:22

标签: ios swift firebase google-maps

在过去的三个星期中,我一直在解决这个问题,似乎无法克服,这使我发疯。我相信我拥有所有正确的代码,但是只是顺序不正确。我目前正在设计一个类似于Uber的应用程序,但概念却完全不同。

我想做的是从Firebase下拉坐标,然后在GMSMapview上放置两个“别针”或标记。我有一个归类为GMSMapview的UIView,并通过IBOutlet进行了连接。因此,当ViewController加载时,Google Maps MapView位于UIView中。我要完成的工作是在当前“驱动程序”所在的位置有一个“ pin”,在“ my”位置所在的位置有第二个pin。我要完成的是让地图“缩放并跟随”驾驶员,直到他到达我的位置为止,类似于Uber。

我尝试了数百种不同类型的代码组合,并在此处跟随我的StackOverflow找到了文章,但这些文章无效。我能够使针脚出现(绿色是驾驶员,红色是他要去的地方),当我进入Firebase并更改坐标之一时,屏幕将“跳动”或闪烁,这真的很糟糕。做更多的研究,我读到为了完成这个概念(并在另一个ViewController中像Uber一样,在GMS MapView上显示所有“汽车”作为标记),我需要将坐标放入数组中,然后遍历包含带有变量的结构的模型。一旦这样做,“闪烁”就停止了,但是每当我更新坐标时,整个视图控制器便会继续从头开始重新加载(就好像ViewController是第一次打开一样)。有时我做纬度,有时做经度,但这没什么区别。显然,由于我是手动更新Firebase,因此无法同时执行两个值。

我在StackOverflow上找到的文章似乎非常有前途,我相信我的方向是正确的,除了在实现了Google的一些推荐代码之后,在这里,我现在崩溃了(在下面的代码中标识)。当我进入Firebase并手动更新坐标(经纬度或经度)后,就会发生此崩溃。

经过将近一个月的尝试来调整并使该代码正常工作之后,我正在寻找一些有关我的代码哪里出错的指导。我正在为GoogleMaps和Firebase使用最新的PODS。最重要的是,我正在寻找在Firebase中实时更新坐标时移动GMS标记的方法,以及在地图靠近“我的位置”时放大地图。

以下是我研究并关注的文章: GMS Map View Maker Flicker issue

How do I move marker along with moving of Google Map in iOS?

这是我的代码:

import UIKit
import CoreLocation
import CoreData
import Firebase
import FirebaseDatabase
import FirebaseAuth
import GoogleMaps
import GooglePlaces
import GooglePlacesPicker
import Alamofire
import SwiftyJSON

class SOPOST: UIViewController, CLLocationManagerDelegate, GMSMapViewDelegate, Alertable {

    @IBOutlet weak var connectedMapView: GMSMapView!
    @IBOutlet weak var driverInfoView: UIView!

    let currentUserId = Auth.auth().currentUser?.uid

    var markers = [] as NSArray

    var locationManager = CLLocationManager()
    var placesClient: GMSPlacesClient!
    var zoomLevel: Float = 12.0
    var likelyPlaces: [GMSPlace] = []
    var selectedPlace: GMSPlace?

    var custlat: CLLocationDegrees?
    var custlong: CLLocationDegrees?
    var driverlat: CLLocationDegrees?
    var driverlong: CLLocationDegrees?
    var destlat: CLLocationDegrees?
    var destlong: CLLocationDegrees?

    var location: CLLocation?

    var destinationMarker = GMSMarker()


    var currentCoordAddress: CLLocationCoordinate2D?
    var destinationCoordAddress: CLLocationCoordinate2D?
    var driverCoordAddress: CLLocationCoordinate2D?


    override func viewDidLoad() {
        super.viewDidLoad()

        connectedMapView.delegate = self
        DispatchQueue.main.async {
            DataService.instance.REF_TRIPS.observe(.value, with: { (snapshot) in
                if let findDriverSnapshot = snapshot.children.allObjects as? [DataSnapshot] {
                    for driver in findDriverSnapshot {
                        if driver.childSnapshot(forPath: "passengerKey").value as? String == self.currentUserId! {
                            let acceptanceStatus = driver.childSnapshot(forPath: "tripIsAccepted").value as! Bool
                            if acceptanceStatus == true {
                                if let observeAcceptDict = driver.value as? Dictionary<String, AnyObject> {
                                    let pickupCoordinateArray = observeAcceptDict["pickupCoordinate"] as! NSArray
                                    self.custlat = pickupCoordinateArray[0] as? CLLocationDegrees
                                    self.custlong = pickupCoordinateArray[1] as? CLLocationDegrees

                                    let driverCoordinateArray = observeAcceptDict["driverCoordinate"] as! NSArray
                                    self.markers = observeAcceptDict["driverCoordinate"] as! NSArray
                                    self.driverlat = driverCoordinateArray[0] as? CLLocationDegrees
                                    self.driverlong = driverCoordinateArray[1] as? CLLocationDegrees
                                    let prepareLocation = CLLocation(latitude: self.driverlat!, longitude: self.driverlong!)
                                    self.location = prepareLocation
                                    let destCoordinateArray = observeAcceptDict["destinationCoordinate"] as! NSArray
                                    self.destlat = destCoordinateArray[0] as? CLLocationDegrees
                                    self.destlong = destCoordinateArray[1] as? CLLocationDegrees
                                    self.currentCoordAddress = CLLocationCoordinate2DMake(self.custlat!, self.custlong!)
                                    self.destinationCoordAddress = CLLocationCoordinate2DMake(self.destlat!, self.destlong!)
                                    self.driverCoordAddress = CLLocationCoordinate2DMake(self.driverlat!, self.driverlong!)
                                    CATransaction.begin()
                                    CATransaction.setAnimationDuration(1.0)
                                    self.destinationMarker.position = CLLocationCoordinate2D(latitude: (self.markers[0] as? CLLocationDegrees)!, longitude: (self.markers[1] as? CLLocationDegrees)!)
                                    self.connectedMapView.camera = GMSCameraPosition.camera(withTarget: self.destinationMarker.position, zoom: 12.0)

                                    self.destinationMarker.icon = GMSMarker.markerImage(with: UIColor.green)
                                    self.destinationMarker.map = self.connectedMapView
                                    self.destinationMarker.tracksViewChanges = false
                                    CATransaction.commit()

                                    let customerdestmarker = GMSMarker()
                                    customerdestmarker.position = CLLocationCoordinate2D(latitude: self.custlat!, longitude: self.custlong!)
                                    customerdestmarker.icon = GMSMarker.markerImage(with: UIColor.red)
                                    customerdestmarker.map = self.connectedMapView
                                    customerdestmarker.tracksViewChanges = false

                                }
                            }
                        }
                    }
                }
            })

        }

    }

    func updateLocationoordinates(coordinates:CLLocationCoordinate2D) {
        if destinationMarker == nil
        {
            destinationMarker = GMSMarker()
            destinationMarker.position = coordinates
            let image = UIImage(named:"destinationmarker")
            destinationMarker.icon = image
            destinationMarker.map =  self.connectedMapView
            destinationMarker.appearAnimation = GMSMarkerAnimation.pop
        }
        else
        {
            CATransaction.begin()
            CATransaction.setAnimationDuration(1.0)
            destinationMarker.position =  coordinates
            CATransaction.commit()
        }
    }

    func mapView(_ mapView: GMSMapView, didChange position: GMSCameraPosition) {
        self.destinationMarker = GMSMarker(position: self.location!.coordinate)  // <----CRASHES HERE: Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value
        destinationMarker.position = position.target
        let destinationLocation = CLLocation(latitude: destinationMarker.position.latitude, longitude: destinationMarker.position.longitude)
        let destinationCoordinate = destinationLocation.coordinate
        updateLocationoordinates(coordinates: destinationCoordinate)
    }

}

0 个答案:

没有答案