在 GeoFire 中获取特定范围内的所有对象

Get all objects within specific range in GeoFire

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

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

这不正是您要找的吗?

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

Objective-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];

Swift:

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)

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

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);
}];

之后别忘了清理听众。