我正在与Firebase共享和检索坐标,但是当我在我的控制台中打印它们时......我得到相同的坐标3-4次。 这会对我的自定义标记图像文件产生奇怪的影响。 如何只从Firebase获取一次坐标?
这是我的代码:
var posts=[postStruct]()
var mapView : GMSMapView? = nil
var friendLocator : [Locator] = [Locator]()
struct Locator {
let name: String
let long: CLLocationDegrees
let lat: CLLocationDegrees
}
var latPass: Double!
var longPass: Double!
var fetchLat: Double!
var fetchLong: Double!
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
{
var location=locations[0]
let span:MKCoordinateSpan=MKCoordinateSpanMake(0.01, 0.01)
var myLocation:CLLocationCoordinate2D=CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude)
let region:MKCoordinateRegion=MKCoordinateRegionMake(myLocation, span)
latPass=28.3217378
longPass=75.6895935
post()
self.configureMapView()
let dataBaseRef=FIRDatabase.database().reference()
dataBaseRef.child("Raunak Trikha").queryOrderedByKey().observeSingleEvent(of: .childAdded, with: {(snapshot) in
let postDict = snapshot.value as? [String : AnyObject] ?? [:]
var fetchLat = postDict["lat"] as! Double
var fetchLong = postDict["long"] as! Double
let locator = Locator(name: "Raunak Trikha", long: fetchLong, lat: fetchLat)
self.friendLocator.append(locator)
self.locateFriend()
print(fetchLat)
print(fetchLong)
})
manager.stopUpdatingLocation()
self.view = mapView
}
func locateFriend() {
for friend in friendLocator{
let friendMarker = GMSMarker()
friendMarker.position=CLLocationCoordinate2D(latitude: friend.lat, longitude: friend.long)
friendMarker.title=friend.name
friendMarker.map=mapView
mapView?.selectedMarker=friendMarker
if friend.name=="Virat Singh"{
friendMarker.icon=UIImage(named: "ViratPin.png")
}
else if friend.name=="Raunak Trikha"{
friendMarker.icon=UIImage(named: "currentLocation.png")
}
}
do {
mapView?.mapStyle = try GMSMapStyle(jsonString: kMapStyle)
} catch {
NSLog("One or more of the map styles failed to load. \(error)")
}
}
func configureMapView(){
let camera = GMSCameraPosition.camera(withLatitude: latPass, longitude: longPass, zoom: 10)
self.mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
view = mapView
mapView?.settings.scrollGestures = true
mapView?.settings.zoomGestures = true
mapView?.settings.myLocationButton = true
//mapView?.addSubview(searchBar)
//mapView?.addSubview(searchSupporter)
//mapView?.bringSubview(toFront: searchBar)
for gesture in (mapView?.gestureRecognizers!)! {
mapView?.removeGestureRecognizer(gesture)
}
}
当我打印fetchLat
& fetchLong
我得到相同的坐标4次,这与我的自定义标记图像重叠,从而产生奇怪的效果。
答案 0 :(得分:1)
由于您添加特定Locator
结构的代码被多次调用,请检查您的数组以确保它在将本地数组添加到数组之前不包含完全相同的结构。
这将评估您的结构数组并确定它是否没有值。但它也假设结构的name
属性是每个结构的唯一标识符,这可能不是你的情况。您也可以比较filter
闭包中要确保不重复的任何值,即i。即lat
和long
。
let locator = Locator(name: "Raunak Trikha", long: fetchLong, lat: fetchLat)
if self.friendLocator.filter({ $0.name == locator.name }).count == 0 {
self.friendLocator.append(locator)
}
self.locateFriend()
答案 1 :(得分:0)
只要您的位置发生更改/更新或GPS定位(预热),您就会调用此函数func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
。
我注意到您正在使用firebase单事件obeserver函数进行数据库更新,使用。observeSingleEvent()
这是正确的,但是由于您已在上述didUpdateLocations函数中定义了调用,因此将多次调用它。
要么将对Firebase的调用移出函数,要么只提供一些条件来调用firebase一次。即只有在位置变化超过X范围/距离等时才更新。