代码之家  ›  专栏  ›  技术社区  ›  Naveen Saini

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

  •  0
  • Naveen Saini  · 技术社区  · 7 年前

    这会在我的自定义标记图像文件上产生奇怪的效果。

    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次,这重叠了我的自定义标记图像,创造了奇怪的效果。

    2 回复  |  直到 7 年前
        1
  •  1
  •   NSGangster    7 年前

    由于您的代码添加了特定的 Locator 多次调用struct,请检查数组,确保它不包含完全相同的struct,然后将其本地添加到数组中。

    name 结构的属性是每个结构的唯一标识符,这可能不是您的情况。您也可以比较 filter 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()
    
        2
  •  0
  •   Alex McPherson    7 年前

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) 只要您的位置发生变化/更新,或GPS定位(预热)到您的位置,就会收到呼叫。

    observeSingleEvent() 然而,这是正确的,因为您已经在上面的didUpdateLocations函数中定义了调用,它将被多次调用。