1

所以我试图根据用户位置检索附近的餐馆。我正在使用 Geofirestore 库尝试查询附近的餐厅,但没有运气。它只是创建一个包含 geohash、lat、long 和 store 等字段的文档。当我控制台记录附近的查询时,我得到一个空数组?代码如下

  export default () => {
  const [restaurantsList, setRestaurantsList] = useState([]); //Initialise restaurant list with setter
  const [errorMessage, setErrorMessage] = useState("");
  const [lat, setLat] = useState(0);
  const [lng, setLng] = useState(0);
  const[nearbyLocations, setnearbyLocations] = useState(null);
  
  //const type = route.params.('type', 'anonymous')

  console.log(restaurantsList)
  console.log(lat, lng)
  console.log(nearbyLocations)
// // Initialize the Firebase SDK
// firebase.initializeApp({
//   // ...
// });


const getUserLocation = () => {
  if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition((position) => {
      setLat(position.coords.latitude);
      setLng(position.coords.longitude);
    });
  }
}

  const getNearbyRestaurents = () => {
  //Create a Firestore reference
  const firestore = firebase.firestore();

  // Create a GeoFirestore reference
  const GeoFirestore = geofirestore.initializeApp(firestore);

  // Create a GeoCollection reference
  // Firebase reference where GeoFirestore will store its information
  const geocollection = GeoFirestore.collection('user_location');

  //Add a GeoDocument to a GeoCollection
  geocollection.add({
    name: 'Geofirestore',
    score: 100,
    // The coordinates field must be a GeoPoint!
    coordinates: new firebase.firestore.GeoPoint(lat, lng)
  })

  // Create a GeoQuery based on a location
  const query = geocollection.near({ center: new firebase.firestore.GeoPoint(lat, lng), radius: 20 });

  // Get query (as Promise)
  query.get().then((value) => {
    // All GeoDocument returned by GeoQuery, like the GeoDocument added above
    console.log(value.docs);
    setnearbyLocations(value.docs)
  });
  }

    const getRestaurants = async () => {
      try {
        const list = [];
        var snapshot = await firebase.firestore().collection("Restaurants").get(); // gets data from the restaurents table
        console.log("Here");
        snapshot.forEach((doc) => { // iterates through each document in the table and push
          list.push(doc.data());
        });
        setRestaurantsList([...list]);;
      } catch (e) {
        setErrorMessage(
          "There's nae bleeding restaurants, I told you to upload them!"
        );
      }
    };

  //Call when component is rendered
  useEffect(() => {
    getRestaurants();
  }, []);

所以我的目标是根据用户位置返回所有附近的餐馆。我在每个餐厅集合中有两个名为“纬度”和“经度”的字段

4

0 回答 0