【发布时间】:2020-08-07 17:31:58
【问题描述】:
我被困在处理未来和从 Firebase 提取地址并显示在标记上的距离上, 虽然我已经设法编写了一些代码来获取当前用户位置、计算距离并将地址转换为 LatLng,但我仍然面临困难。
下面我附上了我的代码,并突出显示了我要计算距离的位置(内部小部件 setMapPins())
我已将地址存储在收藏店内,并将名为 Address 的文档存储在 firebase 中
请帮助我计算 Streambuilder 内的距离并将其显示在标记上。提前致谢。
这是我完整的map.dart 文件中的link
'necessary imports'
'necessary initialization'
_getCurrentLocation() async {
await _geolocator
.getCurrentPosition(desiredAccuracy: LocationAccuracy.high)
.then((Position position) async {
setState(() {
_currentPosition = position;
sourceLocation =
LatLng(_currentPosition.latitude, _currentPosition.longitude);
print('CURRENT POS: $_currentPosition');
mapController.animateCamera(
CameraUpdate.newCameraPosition(
CameraPosition(
target: LatLng(position.latitude, position.longitude),
zoom: 14.0,
),
),
);
});
await _getAddress();
}).catchError((e) {
print(e);
});
}
// Method for retrieving the address
_getAddress() async {
try {
List<Placemark> p = await _geolocator.placemarkFromCoordinates(
_currentPosition.latitude, _currentPosition.longitude);
Placemark place = p[0];
setState(() {
_currentAddress =
"${place.name}, ${place.locality}, ${place.postalCode}, ${place.country}";
startAddressController.text = _currentAddress;
_startAddress = _currentAddress;
});
} catch (e) {
print(e);
}
}
// Method for calculating the distance between two places
Future<bool> _calculateDistance() async {
try {
// Retrieving placemarks from addresses
List<Placemark> startPlacemark =
await _geolocator.placemarkFromAddress(_startAddress);
List<Placemark> destinationPlacemark =
await _geolocator.placemarkFromAddress(_destinationAddress);
if (startPlacemark != null && destinationPlacemark != null) {
Position startCoordinates = _startAddress == _currentAddress
? Position(
latitude: _currentPosition.latitude,
longitude: _currentPosition.longitude)
: startPlacemark[0].position;
Position destinationCoordinates = destinationPlacemark[0].position;
await _createPolylines(startCoordinates, destinationCoordinates);
double totalDistance = 0.0;
// Calculating the total distance by adding the distance
// between small segments
for (int i = 0; i < polylineCoordinates.length - 1; i++) {
totalDistance += _coordinateDistance(
polylineCoordinates[i].latitude,
polylineCoordinates[i].longitude,
polylineCoordinates[i + 1].latitude,
polylineCoordinates[i + 1].longitude,
);
}
setState(() {
_placeDistance = totalDistance.toStringAsFixed(2);
return true;
}
} catch (e) {
print(e);
}
return false;
}
// formula
double _coordinateDistance(lat1, lon1, lat2, lon2) {
var p = 0.017453292519943295;
var c = cos;
var a = 0.5 -
c((lat2 - lat1) * p) / 2 +
c(lat1 * p) * c(lat2 * p) * (1 - c((lon2 - lon1) * p)) / 2;
return 12742 * asin(sqrt(a));
}
// Create the polylines for showing the route between two places
_createPolylines(start, destination) async {
polylinePoints = PolylinePoints();
List<PointLatLng> result = await polylinePoints.getRouteBetweenCoordinates(
googleAPIKey, // Google Maps API Key
start.latitude, start.longitude,
destination.latitude, destination.longitude,
);
if (result.isNotEmpty) {
result.forEach((PointLatLng point) {
polylineCoordinates.add(LatLng(point.latitude, point.longitude));
});
}
}
Widget setMapPins() {
return StreamBuilder(
stream: Firestore.instance.collection('shops').snapshots(),
builder: (context, snapshot) {
if (!snapshot.hasData) return Text('Loading maps... Please Wait');
for (int i = 0; i < snapshot.data.documents.length; i++) {
print(snapshot.data.documents[i]);
/// here i want to calculate distance by extracting address from Firebase and then displaying on marker
}
return Container();
});
}
@override
void initState() {
super.initState();
_getCurrentLocation();
}
【问题讨论】:
-
我已经更新了我的答案,请检查。
-
@MadhavamShahi 问题实际上是关于在 StreamBuilder 中处理未来,我应该如何动态计算从 firebase 获取目标坐标并获取当前地址的距离,然后最终在 StreamBuilder 中为每个数据计算距离。因此,如果您能为此提供详细的代码,我将不胜感激。
-
streambuilder中的stream是目的坐标?
-
对不起,我没有理解清楚,你能告诉我你想要达到的目标吗,我的意思是,你想计算用户当前位置和目的地坐标之间的距离(..from firebase )?如果可以,请发布您的文档结构的图片,这将非常有帮助。
-
github.com/sutkarsh-s/map/blob/master/map.dart.......这是我完整地图文件的链接,如果你真的想做,只需复制粘贴代码,做一些更改并试试你那里的代码.....感谢你的所有努力
标签: firebase google-maps flutter