【问题标题】:Get all objects within specific range in GeoFire在 GeoFire 中获取特定范围内的所有对象
【发布时间】:2015-05-10 16:22:07
【问题描述】:

是否可以使用 Firebase 的 GeoFire 检索特定范围内的所有键? 我知道您可以接收地理查询的事件,例如:输入键、退出键或移动键,但是我目前正在寻找更像 FEventTypeValue 的东西(针对特定地理区域读取一次值),因为我的地图对象不动。

在文档中找不到任何内容:https://github.com/firebase/geofire-objc

【问题讨论】:

  • 如果您的对象没有移动,那么您将获得每条记录的一个键输入事件。您可以监视[query observeWithReadyBlock... 以了解所有事件何时加载,然后使用removeObserverWithHandle 停止侦听。

标签: ios objective-c firebase geofire


【解决方案1】:

您可以使用输入键事件首先将查询的所有键保存在字典中,然后使用就绪事件确定何时添加了所有键:

NSMutableDictionary *allKeys = [NSMutableDictionary dictionary];
[query observeEventType:GFEventTypeKeyEntered withBlock:^(NSString *key, CLLocation *location) {
    [allKeys setObject:location forKey:key];
}];
[query observeReadyWithBlock:^{
    // Create an immutable copy of the keys in the query
    NSDictionary *valueData = [allKeys copy];
    NSLog(@"All keys within a query: %@", valueData);
}];

之后别忘了清理你的听众。

【讨论】:

  • 就是这样。对于未来的读者 - 请注意,在方法中创建键时,您应该选择一些有意义且唯一的 ID:[geofire setLocation:forKey:I_E_FIREBASE_UNIQUE_ID] 将 geofire 对象存储在树中的单个节点下,例如:“geofireSth”并创建geofireSth 的“g”节点上的索引。这样,您将能够使用来自@johnnydee 示例的查询来检索特定半径内的对象。使用从查询中检索到的键 (I_E_FIREBASE_UNIQUE_ID),执行服务器请求以获取有关对象的其他数据。
【解决方案2】:

这不正是您要找的吗?

GeoFire 允许您使用 GFQuery 对象查询地理区域内的所有键。

目标-C:

  CLLocation *center = [[CLLocation alloc] initWithLatitude:37.7832889 longitude:-122.4056973];
    // Query locations at [37.7832889, -122.4056973] with a radius of 600 meters
    GFCircleQuery *circleQuery = [geoFire queryAtLocation:center withRadius:0.6];

    // Query location by region
    MKCoordinateSpan span = MKCoordinateSpanMake(0.001, 0.001);
    MKCoordinateRegion region = MKCoordinateRegionMake(center.coordinate, span);
    GFRegionQuery *regionQuery = [geoFire queryWithRegion:region];

斯威夫特:

let center = CLLocation(latitude: 37.7832889, longitude: -122.4056973)
// Query locations at [37.7832889, -122.4056973] with a radius of 600 meters
var circleQuery = geoFire.queryAtLocation(center, withRadius: 0.6)

// Query location by region
let span = MKCoordinateSpanMake(0.001, 0.001)
let region = MKCoordinateRegionMake(center.coordinate, span)
var regionQuery = geoFire.queryWithRegion(region)

【讨论】:

  • 嗯,我是这么认为的,但是我可能遗漏了一些东西,但我看不到访问在特定区域内找到的对象列表的方法。
  • 我现在无法测试它,但circleQuery 对象中不应该有所有结果吗?
  • 根据文档,没有列表或类似的东西:geofire-ios.firebaseapp.com/docs/Classes/GFCircleQuery.html 我们可以观察到查询完成,但是块属性是无效的,这与 Firebase 中的 FEventTypeValue 不同
猜你喜欢
  • 1970-01-01
  • 2016-08-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多