我对使用GeoFire和RXJS很陌生。
我需要根据Geofire查询显示附近的用户。我尝试了很多方法,但是要花很长时间,大约需要9段(仅5-6个测试用户)。我该如何优化呢?
非常感谢您的帮助
在Firebase中
-locations
- userID
- g
- l
- 0:lat
- 1:long
- users
-userID
- firstname: "fname"
- lastname...
Geofire服务
hits = new BehaviorSubject([]);
getNearLocations(radius: number, coords: Array<number>) {
this.geoFire.query({
center: coords,
radius: radius
})
.on('key_entered', (key, location, distance) => {
let hit = {
key: key,
location: location,
distance: distance,
}
let currentHits = this.hits.value
//this.hits.push(hit)
currentHits.push(hit)
this.hits.next(currentHits)
})
people.ts(构造函数)
this.locateActiveUser().then((pos) => {
this.geofire.getNearLocations(2, [pos.coords.latitude, pos.coords.longitud])
this.subscription = this.geofire.hits
.subscribe( nearUserArray => {
Promise.all(
nearUserArray.map(nearUser => {
this.afDatabase.object(`users/${nearUser.key}`).query.once('value')
.then( user => {
nearUser["user"]= user.val(); })
return nearUser
})).then( a => {
console.log("nearUserArray: ", nearUserArray);
this.nearbyUsers$ = of(nearUserArray);
})
})
})
people.html
<ion-item *ngFor="let nearby of nearbyUsers$ | async" class="item item-block item-md">
<ion-row>
<ion-col col-3 >...
</ion-col>
<ion-col col-9 (click)="navigateTo('user-profile', nearby.user)">
<ion-row>
<ion-col col-4 >
<span>A {{nearby.distance | roundNumber }} km</span>
</ion-col>
</ion-row>
<ion-row>
<h3> {{nearby.user.firstname}} {{nearby.user["lastname"]}}</h3>
</ion-row>
</ion-col>
</ion-row>
</ion-item>
浏览器控制台
答案 0 :(得分:0)
因此,我想说您可能正在尽力而为。geofire
更像是一个索引,您可以在Firebase RTDB中获得对实际文档/对象的引用。有诸如geofirestore
(我的个人宝贝)之类的Firestore库,还有geofirex
这样的Firestore库,这些库仅允许您根据位置进行一次查询并获取文档。
geofirestore
在幕后工作方式上与geofire
更相似,并且可能更容易过渡到(如果您有兴趣的话)