【问题标题】:Swift Google Maps GMS Marker not sticking on user locationSwift Google Maps GMS Marker 没有粘在用户位置上
【发布时间】:2018-12-22 11:02:55
【问题描述】:

我的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()
})

看起来每当我拖动我的地图时,它似乎会跟随我的谷歌地图中心的图标,而不是当我拖动谷歌地图时它只会停留在用户位置上。您可以在我上传的video 上查看。

【问题讨论】:

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


    【解决方案1】:

    我的错,好像我把所有东西都放在了谷歌地图上,

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

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

    进入这个代码块。

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

    【讨论】:

      猜你喜欢
      • 2015-11-03
      • 2013-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-02
      • 2013-10-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多