ios - 如何从 Firebase 获取单个数据库引用

标签 ios swift google-maps firebase core-location

我正在与 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)

    }

}

当我打印 fetchLatfetchLong 时,我得到了 4 次相同的坐标,它与我的自定义标记图像重叠,产生了奇怪的效果。

最佳答案

由于添加特定 Locator 结构的代码被多次调用,因此在将其添加到本地数组之前检查您的数组以确保它不包含完全相同的结构。

这将评估您的结构数组并确定它是否没有值(value)。但它还假定结构的 name 属性是每个结构的唯一标识符,这可能不是您的情况。您也可以比较 filter 闭包中要确保没有重复的任何值,即。 e. latlong

    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()

关于ios - 如何从 Firebase 获取单个数据库引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46279864/

相关文章:

ios - 为什么该方法要在最后运行?

javascript - 使用 Cordova(ios 和 android)托管网页的最佳配置是什么

ios - 如何在Swift中为每个UUID生成随机但唯一的颜色?

android - MvvmCross + Fragments + Back Button = 崩溃或控件不响应/绑定(bind)

javascript - 通过画圆圈在 Google map 上选择自定义标记

ios - initWithData 返回(空)

ios - 为什么 instagram iOS 应用程序从不要求我再次登录,即使 token 已过期?

swift - 为什么我的 Playground 在 NSRange() 上崩溃

swift - 在 View 的不同部分点击时如何执行不同的功能?

android - 导航完成后,如何从 Android 应用程序中的谷歌地图获取回调