1

我有一个包含 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
4

1 回答 1

-1

正如@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

我希望它有帮助!

于 2019-11-22T16:10:39.507 回答