错误:'for'循环中使用的类型'PolylineResult'必须实现'Iterable<dynamic>'



我正在尝试创建一个谷歌地图,并在两个坐标之间绘制一条折线。但我得到了下面给出的错误1。我在尝试了建议的修复方法https://dart.dev/tools/diagnostic-messages#for_in_of_invalid_type

即用CCD_ 2代替CCD_。在这种情况下,我得到错误2。

有人能告诉我为什么这是一个问题吗(以及为什么修复不起作用(?如果你能为这个问题提出一个解决方案,我也会很感激?

错误1:

Error: The type 'PolylineResult' used in the 'for' loop must implement 'Iterable<dynamic>'.
- 'PolylineResult' is from 'package:flutter_polyline_points/src/utils/polyline_result.dart' ('../../../flutter/.pub-cache/hosted/pub.dartlang.org/flutter_polyline_points-0.2.4/lib/src/utils/polyline_result.dart').
- 'Iterable' is from 'dart:core'.
for (PointLatLng point in result) {

错误2:

Error: The getter 'values' isn't defined for the class 'PolylineResult'.
- 'PolylineResult' is from 'package:flutter_polyline_points/src/utils/polyline_result.dart' ('../../../flutter/.pub-cache/hosted/pub.dartlang.org/flutter_polyline_points-0.2.4/lib/src/utils/polyline_result.dart').
Try correcting the name to the name of an existing getter, or defining a getter or field named 'values'.
for (PointLatLng point in result.values) {

代码:

void main() =>
runApp(MaterialApp(debugShowCheckedModeBanner: false, home: MapPage()));
const double CAMERA_ZOOM = 13;
const double CAMERA_TILT = 0;
const double CAMERA_BEARING = 30;
const LatLng SOURCE_LOCATION = LatLng(42.7477863, -71.1699932);
const LatLng DEST_LOCATION = LatLng(42.6871386, -71.2143403);
const PointLatLng POINT_SOURCE_LOCATION = PointLatLng(42.7477863, -71.1699932);
const PointLatLng POINT_DEST_LOCATION = PointLatLng(42.6871386, -71.2143403);
class MapPage extends StatefulWidget {
@override
State<StatefulWidget> createState() => MapPageState();
}
class MapPageState extends State<MapPage> {
Completer<GoogleMapController> _controller = Completer();
Set<Marker> _markers = {};
Set<Polyline> _polylines = {};
List<LatLng> polylineCoordinates = [];
PolylinePoints polylinePoints = PolylinePoints();
String googleAPIKey = "<YOUR_API_KEY>";
BitmapDescriptor sourceIcon;
BitmapDescriptor destinationIcon;
double pinPillPosition = -100;
PinInformation currentlySelectedPin = PinInformation(
pinPath: '',
avatarPath: '',
location: LatLng(0, 0),
locationName: '',
labelColor: Colors.grey);
PinInformation sourcePinInfo;
PinInformation destinationPinInfo;
@override
void initState() {
super.initState();
setSourceAndDestinationIcons();
}
void setMapPins() {
// source pin
_markers.add(Marker(
// This marker id can be anything that uniquely identifies each marker.
markerId: MarkerId('sourcePin'),
position: SOURCE_LOCATION,
onTap: () {
setState(() {
currentlySelectedPin = sourcePinInfo;
pinPillPosition = 0;
});
},
icon: sourceIcon));
sourcePinInfo = PinInformation(
locationName: "Start Location",
location: SOURCE_LOCATION,
pinPath: "assets/driving_pin.png",
avatarPath: "assets/friend1.jpg",
labelColor: Colors.blueAccent);
// destination pin
_markers.add(Marker(
// This marker id can be anything that uniquely identifies each marker.
markerId: MarkerId('destPin'),
position: DEST_LOCATION,
onTap: () {
setState(() {
currentlySelectedPin = destinationPinInfo;
pinPillPosition = 0;
});
},
icon: destinationIcon));
destinationPinInfo = PinInformation(
locationName: "End Location",
location: DEST_LOCATION,
pinPath: "assets/destination_map_marker.png",
avatarPath: "assets/friend2.jpg",
labelColor: Colors.purple);
}
void setSourceAndDestinationIcons() async {
sourceIcon = await BitmapDescriptor.fromAssetImage(
ImageConfiguration(devicePixelRatio: 2.5), 'assets/driving_pin.png');
destinationIcon = await BitmapDescriptor.fromAssetImage(
ImageConfiguration(devicePixelRatio: 2.5),
'assets/destination_map_marker.png');
}
void onMapCreated(GoogleMapController controller) {
controller.setMapStyle(Utils.mapStyles);
_controller.complete(controller);
setMapPins();
setPolylines();
}
@override
Widget build(BuildContext context) {
CameraPosition initialLocation = CameraPosition(
zoom: CAMERA_ZOOM,
bearing: CAMERA_BEARING,
tilt: CAMERA_TILT,
target: SOURCE_LOCATION);
return Scaffold(
body: Stack(children: <Widget>[
GoogleMap(
myLocationEnabled: true,
compassEnabled: true,
tiltGesturesEnabled: false,
markers: _markers,
polylines: _polylines,
mapType: MapType.normal,
initialCameraPosition: initialLocation,
onMapCreated: onMapCreated,
onTap: (LatLng location) {
setState(() {
pinPillPosition = -100;
});
},
),
MapPinPillComponent(
pinPillPosition: pinPillPosition,
currentlySelectedPin: currentlySelectedPin)
]));
}
setPolylines() async {
PolylineResult result = await polylinePoints.getRouteBetweenCoordinates(
googleAPIKey,
POINT_SOURCE_LOCATION,
POINT_DEST_LOCATION,
);
if (result != null) {
for (PointLatLng point in result) {
polylineCoordinates.add(LatLng(point.latitude, point.longitude));
}
}
setState(() {
Polyline polyline = Polyline(
polylineId: PolylineId("poly"),
color: Color.fromARGB(255, 40, 122, 198),
points: polylineCoordinates);
_polylines.add(polyline);
});
}
}
class PinInformation {
String pinPath;
String avatarPath;
LatLng location;
String locationName;
Color labelColor;
PinInformation({this.pinPath, this.avatarPath, this.location, this.locationName, this.labelColor});
}

列表存储在PolylineResultpoints字段中。通过result.points访问它们。

相关内容

  • 没有找到相关文章

最新更新