Swift Google Maps GMS标记未停留在用户位置

时间:2018-07-14 05:57:58

标签: swift google-maps google-maps-markers

我的GMSMarker出现问题,似乎它不停留在用户位置上。我想将标记粘贴到用户位置本身,请检查下面的代码。

func mapView(_ mapView: GMSMapView, didChange position: GMSCameraPosition) {
    UpdateService.instance.updateUserLocation(withCoordinate: position.target)
    UpdateService.instance.updateContractorLocation(withCoordinate: position.target)
}

func loadContractorAnnotation() {
    DispatchQueue.main.async {
        DataService.instance.REF_CONTRACTOR.observeSingleEvent(of: .value, with: { (snapshot) in
            if let contractorSnapshot = snapshot.children.allObjects as? [DataSnapshot] {
                for contractor in contractorSnapshot {
                    if contractor.hasChild("coordinate") {
                        if contractor.childSnapshot(forPath: "contractorIsAvailable").value as? Bool == true {
                            if let contractorCoordinateDict = contractor.value as? Dictionary<String, Any> {
                                let coordinateArray  = contractorCoordinateDict["coordinate"] as! NSArray
                                let contractorCoordinate = CLLocationCoordinate2D(latitude: coordinateArray[0] as! CLLocationDegrees , longitude: coordinateArray[1] as! CLLocationDegrees)
                                CATransaction.begin()
                                CAAnimation.init().duration = 0.5
                                self.contractorMarker.position = contractorCoordinate
                                self.contractorMarker.iconView = self.markerImageView(image: UIImage(named: "contractor-marker")!, size: CGSize(width: 30, height: 30))
                                self.contractorMarker.groundAnchor = CGPoint(x: 0.5, y: 0.5)
                                CATransaction.commit()
                                self.contractorMarker.map = self.mapView
                            }
                        }
                    }
                }
            }
        }) 
    }
}
// ViewDidLoad()
DataService.instance.REF_CONTRACTOR.observe(.value, with: {(snapshot) in
        self.loadContractorAnnotation()
})

看起来每当我拖动地图时,它似乎都将跟随Google地图中心的图标,而当我拖动Google地图时,它将仅停留在用户位置。您可以在我上传的video上进行检查。

1 个答案:

答案 0 :(得分:0)

我不好,看来我把所有东西都放在了谷歌地图上,

我从didChange position函数中移出了下面的代码行。

UpdateService.instance.updateUserLocation(withCoordinate: position.target)
UpdateService.instance.updateContractorLocation(withCoordinate: position.target)

进入此代码块。

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])