【问题标题】:Firestore query doesn't return nearby locations with Geo flutter fireFirestore 查询不会返回具有 Geo Flutter Fire 的附近位置
【发布时间】:2020-10-09 04:56:22
【问题描述】:

我正在尝试获取附近的位置。在这里,想象一下我在比佛利山庄,在我的火库中存储了洛杉矶的纬度和经度。这两个地方的距离都是 12 英里 = 19.3 公里。在 GeoFlutterFire 中,我以比佛利山庄为中心提供了 10 公里的半径。但它仍然提供了洛杉矶的数据。

这里是代码

import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';
import 'package:geoflutterfire/geoflutterfire.dart';
import 'package:cloud_firestore/cloud_firestore.dart';

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  final geo = Geoflutterfire();
  final _firestore = FirebaseFirestore.instance;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      body: Container(
        child: geoFlut(),
      ),
    );
  }

  geoFlut() {
    GeoFirePoint myLocation = geo.point(latitude: 34.0522, longitude: 118.2437);
    _firestore
        .collection('locations')
        .add({'name': 'LA', 'position': myLocation.data});

    GeoFirePoint center = geo.point(latitude: 34.072165, longitude: 118.402624);

    var collectionReference = _firestore.collection('locations');

    double radius = 10;
    String field = 'position';

    Stream<List<DocumentSnapshot>> stream = geo
        .collection(collectionRef: collectionReference)
        .within(center: center, radius: radius, field: field);

    stream.listen((List<DocumentSnapshot> documentList) {
      // print(stream.length);
      print('Here');
      print(documentList[0].data());
    });
  }
}

此准则有什么问题?帮我解决这个问题!

【问题讨论】:

  • 嗨@Xor96 你试过降低radius 的值吗?看起来,这个纬度和经度可能会有所不同。例如,如果您是步行或乘车,它们的距离不同 - 查看谷歌地图,您提到的这 12 英里的距离似乎是通过汽车,如通过步行,它小于10 miles,如果您输入这个站点here,它是10公里。
  • 考虑到这一点,您能否尝试将radius 降低到 8 或 9 以进行测试?
  • @gso_gabriel 是的。你说的我已经做了。但它仍然返回 LA 作为输出。我将半径保持为 6 和 8,结果也相同。直到 5 它返回 LA,当它 4 时它什么也不返回。请帮我解决这个问题。请!
  • 职位字段的类型是什么?细绳? myLocation.data 不是字符串。谢谢

标签: firebase flutter dart google-cloud-firestore location


【解决方案1】:

是的,在许多错误之后我把它整理出来。问题在于添加strictmode: true 后我没有使用strict mode 的查询。问题解决了。

【讨论】:

    猜你喜欢
    • 2015-07-04
    • 1970-01-01
    • 1970-01-01
    • 2017-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多