我有一个具有GeoPoint字段的( location ),我希望根据当前用户的位置进行查询,以找到附近的位置。
看起来Cloud还不能做到这一点,但是geofirestore似乎是可行的选择。我试图查询坐标为34.0103,118.4962的(位置)。但是,我得到了以下错误:
2019-05-26T19:28:26.891Z @firebase/ Firestore :,Firestore (6.0.4):内部未处理错误:,configureNetworkMonitoring
以下是我所看到的:
云修复数据:
{
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",
}
React组件(搜索):
// 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
发布于 2019-11-22 08:10:39
正如@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",
}
您的geopoint必须替换为g,并且必须添加名为l的geohash children
。
希望能帮上忙!
https://stackoverflow.com/questions/56316821
复制