我正在将谷歌地图中的选择纬度和经度传递到我的flutter应用程序的另一个屏幕上,在那里我正在计算两个位置之间的距离。我已经得到了很好的值,但有一个widget
无法在初始化器问题中访问。我使用的是谷歌地图,我必须将widget.lat
widget.long
值传递给userLocation
标记。
顺便说一下,我正在使用本教程的代码获取位置之间的距离
这是我的代码
class CalculateDistance extends StatefulWidget {
const CalculateDistance({super.key, required this.lang, required this.lat});
final double lang;
final double lat;
@override
// ignore: library_private_types_in_public_api
_CalculateDistanceState createState() => _CalculateDistanceState();
}
class _CalculateDistanceState extends State<CalculateDistance> {
GoogleMapController? mapController; //contrller for Google map
PolylinePoints polylinePoints = PolylinePoints();
String googleAPiKey = "YOUR_API_KEY";
Set<Marker> markers = {}; //markers for google map
Map<PolylineId, Polyline> polylines = {}; //polylines to show direction
LatLng storeLocation =
const LatLng(-30.600164342582726, 23.508854043469647); // Store location
// This is where I can't use the passed values
LatLng userLocation = LatLng(widget.lat, widget.lang); // User location
double distance = 0.0;
@override
void initState() {
markers.add(Marker(
//add start location marker
markerId: MarkerId(storeLocation.toString()),
position: storeLocation, //position of marker
infoWindow: const InfoWindow(
//popup info
title: 'Store Location',
snippet: 'Store Marker',
),
icon: BitmapDescriptor.defaultMarker, //Icon for Marker
));
markers.add(Marker(
//add distination location marker
markerId: MarkerId(userLocation.toString()),
position: userLocation, //position of marker
infoWindow: const InfoWindow(
//popup info
title: 'User Location',
snippet: 'User Marker',
),
icon: BitmapDescriptor.defaultMarker, //Icon for Marker
));
getDirections(); //fetch direction polylines from Google API
super.initState();
}
getDirections() async {
List<LatLng> polylineCoordinates = [];
PolylineResult result = await polylinePoints.getRouteBetweenCoordinates(
googleAPiKey,
PointLatLng(storeLocation.latitude, storeLocation.longitude),
PointLatLng(userLocation.latitude, userLocation.longitude),
travelMode: TravelMode.driving,
);
if (result.points.isNotEmpty) {
for (var point in result.points) {
polylineCoordinates.add(LatLng(point.latitude, point.longitude));
}
} else {
print(result.errorMessage);
}
//polulineCoordinates is the List of longitute and latidtude.
double totalDistance = 0;
for (var i = 0; i < polylineCoordinates.length - 1; i++) {
totalDistance += calculateDistance(
polylineCoordinates[i].latitude,
polylineCoordinates[i].longitude,
polylineCoordinates[i + 1].latitude,
polylineCoordinates[i + 1].longitude);
}
print(totalDistance);
setState(() {
distance = totalDistance;
});
//add to the list of poly line coordinates
addPolyLine(polylineCoordinates);
}
addPolyLine(List<LatLng> polylineCoordinates) {
PolylineId id = const PolylineId("poly");
Polyline polyline = Polyline(
polylineId: id,
color: Colors.deepPurpleAccent,
points: polylineCoordinates,
width: 8,
);
polylines[id] = polyline;
setState(() {});
}
double calculateDistance(lat1, lon1, lat2, lon2) {
var p = 0.017453292519943295;
var a = 0.5 -
cos((lat2 - lat1) * p) / 2 +
cos(lat1 * p) * cos(lat2 * p) * (1 - cos((lon2 - lon1) * p)) / 2;
return 12742 * asin(sqrt(a));
}
// Scaffold ahead
用late
关键字声明此LatLng
为_CalculateDistanceState
类的成员:
class _CalculateDistanceState extends State<CalculateDistance> {
late LatLng _userLocation;
(...)
}
然后在initState
中,您将可以访问小部件:
void initState() {
super.initState();
_userLocation = LatLng(widget.lat, widget.lang);
(...)
}