reactjs - 使用 Geofirestore 查询 Cloud Firestore 地理点时出错
问题描述
我有一个包含 GeoPoint 字段的 Cloud Firestore 集合(位置),我想根据当前用户的位置进行查询以查找附近的地方。
Cloud Firestore 似乎还不能做到这一点,但 geofirestore 似乎是可行的选择。我正在尝试使用坐标 34.0103、118.4962 查询 Cloud Firestore 集合(位置)。但是,我收到以下错误:
[2019-05-26T19:28:26.891Z] @firebase/firestore:, Firestore (6.0.4): INTERNAL UNHANDLED ERROR: , configureNetworkMonitoring
这是我看过的:
- 加载到 Cloud Firestore 的数据集合(位置)是正确的
- Cloud Firestore GeoPoint 的语法和数据正确
- 地理查询不起作用
Cloud Firestore 数据:
{
city: "Santa Monica",
geopoint: [34.0103, 118.4962],
location_id: "LA_00012",
location_name: "Santa Monica Pier",
state: "CA",
street: "200 Santa Monica Pier,
zip_code: "90401",
}
反应组件(搜索):
// Imports: Dependencies
import React, { Component } from 'react';
import { Button, SafeAreaView, StyleSheet, Text, View } from 'react-native';
import 'firebase/firestore';
import { GeoCollectionReference, GeoFirestore, GeoQuery, GeoQuerySnapshot } from 'geofirestore';
// Screen: Search
class Search extends Component {
constructor (props) {
super(props);
this.state = {
location: null,
errorMessage: null,
};
}
// Get Nearest Locations
getNearestLocations = async () => {
try {
// Create Firestore Reference
const collection = firebase.firestore().collection('locations');
// Query Limit (10)
const limitQuery = collection.limit(10);
// Geo Query
const query = new GeoQuery(limitQuery).near({
center: new firebase.firestore.GeoPoint(34.0103, 118.4962),
radius: 10,
});
query.get().then((value) => {
value.docs.forEach(doc => {
console.log(doc);
});
});
}
catch (error) {
console.log(error);
}
}
render() {
return (
<SafeAreaView style={styles.container}>
<Text>Search</Text>
<Button title="Search" onPress={this.getNearestLocations} />
</SafeAreaView>
);
}
}
// Styles
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
});
// Exports
export default Search
解决方案
正如@MichaelSolati 指出的那样,您的数据必须按如下结构构建:
{
city: "Santa Monica",
g: [34.0103, 118.4962],
l: "wwh32rqk3e",
location_id: "LA_00012",
location_name: "Santa Monica Pier",
state: "CA",
street: "200 Santa Monica Pier,
zip_code: "90401",
}
您的地理点必须替换为g并且您必须添加一个geohash children
名为l。
我希望它有帮助!
推荐阅读
- python - 不在字典中的键
- reactjs - React Hook 更新父状态落后一步
- javascript - 如何防止 js 文件下载并返回传输到 c# webBrowser?
- r - 如何转换从“tbl_df”“tbl”“data.frame”中导入的数据集。” 到“xts”?
- installation - Need to install OpenliteSpeed Server on Fedora 31
- python - 如何读取多个条件 Python Arduino Uno
- c# - 对 PageModel 的 POST 请求创建新实例,删除模型的先前值
- batch-file - 如何根据文件名将文件名排序到文件夹中
- c++ - 将 C++ 非捕获 lambda 转换为 void*
- python - 如何确定 UNIX shell 中的列数