【发布时间】: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());
});
}
}
此准则有什么问题?帮我解决这个问题!
【问题讨论】:
-
考虑到这一点,您能否尝试将
radius降低到 8 或 9 以进行测试? -
@gso_gabriel 是的。你说的我已经做了。但它仍然返回 LA 作为输出。我将半径保持为 6 和 8,结果也相同。直到 5 它返回 LA,当它 4 时它什么也不返回。请帮我解决这个问题。请!
-
职位字段的类型是什么?细绳? myLocation.data 不是字符串。谢谢
标签: firebase flutter dart google-cloud-firestore location