【问题标题】:Error when Querying Cloud Firestore Geopoints using Geofirestore使用 Geofirestore 查询 Cloud Firestore 地理点时出错
【发布时间】:2019-05-26 19:43:47
【问题描述】:

我有一个包含 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",
}

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

【问题讨论】:

标签: reactjs firebase react-native google-cloud-firestore geofirestore


【解决方案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",
}

您的 geopoint 必须替换为 g,并且您必须添加一个名为 lgeohash children

希望对你有帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-29
    • 1970-01-01
    • 2019-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多