2016-09-30 9 views
4

나는 다음과 같은 중포 기지 데이터베이스 구조, I는 일정한 거리 매개 변수 내에서 게시물 필터링해야필터 중포 기지 데이터베이스 위치 반경에 따라

"-KSupX7CppMD1zbqIy0y" : { 
    "addedByUser" : "zQpb7o18VzYsSoTQtT9DNhOqTUn2", 
    "content" : "Post 1", 
    "cost" : "20", 
    "duration" : "Weekly", 
    "latitude" : "40.7594479995956", 
    "longitude" : "-73.9838934062393", 
    "timestamp" : "Fri 30 Sep" 
}, 
"-KSuphuqO6a0lnrJYUkt" : { 
    "addedByUser" : "zQpb7o18VzYsSoTQtT9DNhOqTUn2", 
    "content" : "Post 2", 
    "cost" : "10", 
    "duration" : "Daily", 
    "latitude" : "40.7594329996462", 
    "longitude" : "-73.9846261181847", 
    "timestamp" : "Fri 30 Sep" 
} 

(50m, 100m 미만, 150m & 200m) 나는를 얻을 수 있습니다 좌표 "경도"& "위도"하지만 나는 게시물을 필터링하는 고투하고 있어요. 어떤 도움이라도 대단히 감사하겠습니다.

미리 감사드립니다. 초보자부터 Firebase & Swift.

+1

Geofire : https://github.com/firebase/geofire-objc –

답변

0

다음은 시도해 볼 수있는 해결책입니다. 여기서는 모든 게시물을 Firebase에서 앱에 다운로드하여 배열에 저장했다고 가정합니다. 당신은 또한 개인적으로 사용하지 않은 GeoFire를 사용할 수 있습니다 (하지만). 필자는 위치에 따라 Firebase를 쿼리 할 수 ​​있으므로 먼저 사용자의 장치에 모든 게시물을 다운로드하고 클라이언트 측에서 필터링하지 않아도됩니다. 이 같은 JSON에 대한

// assumes you have set up the app to get the user's current location 
var currentLocation: CLLocation? 

// Your array of posts downloaded from Firebase 
var postArray = [post]() 

let metersInTwentyFiveMiles = 40233.6 

// You could put this in viewDidAppear after you load the post array from Firebase 
for post in postArray { 

    var distanceInMeters: CLLocationDistance = 0 

    let postLocation = CLLocation(latitude: post.latitude, longitude: post.longitude) 


    if let currentLocation = self.currentLocation { 
     // Set distanceInMeters to the distance between the user's current location and the location of the post. 
     distanceInMeters = currentLocation.distanceFromLocation(postLocation) 
     // If this distance is not within 25 miles, remove this post from the array 
     if self.metersInTwentyFiveMiles < distanceInMeters { 
      self.posts = self.posts.filter{ $0 != post } 
     } 
    } 
} 
0

: -

UsersLocation :{ 
    autoID1 : {....}, 
    autoID2 : {.....} 
      } 

이 시도 : -

FIRDatabase.database().reference().child("UsersLocation").queryOrdered(byChild: "lat").queryStarting(atValue: 30).queryEnding(atValue: 60).observeSingleEvent(of: .value, with: {(snap) in 

     print(snap) 


     if let snapDict = snap.value as? [String:AnyObject]{ 

      for each in snapDict{ 

       print(each) 

      } 
     } 
    }) 
} 

이 당신에게 autoID 키와 모든 사용자를 줄 것이다, 30과 60 사이에 자신의 위도와 , 데이터를 검색하는 순서는 무작위이므로 오름차 순으로 반복하려면받은 사전을 정렬 할 수 있습니다.

+0

문제가 해결되면 답변을 수락 해주십시오. 해피 코딩 – Dravidian