首页 > 解决方案 > 使用 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 数据:

{
  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

标签: reactjsfirebasereact-nativegoogle-cloud-firestoregeofirestore

解决方案


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

我希望它有帮助!


推荐阅读