Swift Google 地图 GMS 标记未贴在用户位置上
Swift Google Maps GMS Marker not sticking on user location
我的 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上查看
我的错,好像我把所有东西都放在了google地图上,
我从 didChange position
函数中移动了下面的代码行。
UpdateService.instance.updateUserLocation(withCoordinate: position.target)
UpdateService.instance.updateContractorLocation(withCoordinate: position.target)
进入此代码块。
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
我的 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上查看
我的错,好像我把所有东西都放在了google地图上,
我从 didChange position
函数中移动了下面的代码行。
UpdateService.instance.updateUserLocation(withCoordinate: position.target)
UpdateService.instance.updateContractorLocation(withCoordinate: position.target)
进入此代码块。
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])