Flutter Google Maps Polyline-无法将参数类型“ LatLng”分配给参数类型“ List <LatLng>”

时间:2019-11-27 08:18:09

标签: json google-maps flutter dart polyline

我正在Flutter项目中使用Google Maps。我试图解析资产中的两个JSON文件,以便可以在折线和标记中使用它们的经度和纬度。标记工作正常,但是现在我在折线的Future构建器中添加了另一个未来,并且出现以下错误:

  

不能将参数类型LatLng分配给参数类型List<LatLng>

Future _future;

Future _futuree;

Future<String> loadString() async => await rootBundle.loadString('assets/busStops/stops_${widget.selectStation}.json');
List<Marker> allMarkers = [];
GoogleMapController _controller;

@override
void initState() {
  // TODO: implement initState
  super.initState();

  //future 1
  _future = loadString();

  //future 2
  _futuree = loadMyCoord();

}

Future<String> loadMyCoord() async {
  var x = await rootBundle
      .loadString('assets/route/coords_Centurion.json');
  return x;
}

@override
Widget build(BuildContext context) {
  createMarker(context);
  return Scaffold(
    appBar: AppBar(
      title: Text("Bus Routes"),
      centerTitle: true,
      backgroundColor: Color.fromRGBO(59, 62, 64, 1),
      actions: <Widget>[
        FlatButton(
          textColor: Colors.white,
          onPressed: () {
            Navigator.push(
              context,
              MaterialPageRoute(builder: (context) => busStationList()),
            );
          },
          child: Icon(Icons.add),
        ),
      ],
    ),
    body: Stack(children: [
      Container(
        height: MediaQuery.of(context).size.height,
        width: MediaQuery.of(context).size.width,
        child: FutureBuilder(

          // Futures
          future: Future.wait(
            [
              //[0]
              _future,

              //[1]
              _futuree,
            ]
          ),

          builder: (
                  context, 
                  AsyncSnapshot<List<dynamic>> snapshot,
          ) {
            // Check hasData once for all futures.
            if (!snapshot.hasData) {
              return CircularProgressIndicator();
            }

            List<dynamic> parsedJson = jsonDecode(snapshot.data[0]);
            allMarkers = parsedJson.map((element) {
              return Marker(
                  icon: customIcon,
                  markerId: MarkerId(element["Latitude"].toString()),
                  position: LatLng(element['Latitude'] ?? 0.0,
                      element['Longitude'] ?? 0.0));
            }).toList();

            List<dynamic> parseJson = jsonDecode(snapshot.data[1]);
            List<Polyline> allPolylinesByPosition = [];
            parseJson.forEach((element){
              List<dynamic> coords = element["coords"];

              coords.forEach((i) {
                double lat = double.tryParse(i["latitude"]);
                double lng = double.tryParse(i["longitude"]);

                allPolylinesByPosition.add(Polyline(
                  polylineId: PolylineId('lines'),
                  points: points: LatLng(lat ?? 0.0, lng ?? 0.0);
                  visible: true,
                  color: Colors.red

                ));
              }

              );
            });

            return GoogleMap(
              initialCameraPosition: CameraPosition(
                  target: LatLng(-26.1711459, 27.9002758), zoom: 9.0),
              markers: Set.from(allMarkers),
              onMapCreated: mapCreated,
              polylines: Set.from(allPolylinesByPosition),
            );
          },
        ),
      ),
    ]),
  );
}

void mapCreated(controller) {
  setState(() {
    _controller = controller;
  });
}

2 个答案:

答案 0 :(得分:0)

没什么要注意的,points的{​​{1}}参数仅接受PolylineLatLng)的列表,而不接受List<LatLng>

此行应在您的代码中更改。尝试添加LatLng的列表,而不是在LatLng参数中传递单个LatLng实例。

points

Polyline.dart

points: LatLng(lat ?? 0.0, lng ?? 0.0);

答案 1 :(得分:0)

嗨,我现在已经完成了以下操作,并提取了数据,但未在地图上显示

List<dynamic> parseJson = jsonDecode(snapshot.data[1]);

             Set<Polyline> allPolylinesByPosition = {};

             parseJson.forEach((element) {
               List<dynamic> coords = element["coords"];

               coords.forEach((i) {

                 List<LatLng> latlng = [

                   LatLng( double.tryParse(i["latitude"]) ?? 0.0 ,double.tryParse(i["longitude"]) ?? 0.0)
              ];

                 allPolylinesByPosition.add(Polyline(
                     polylineId: PolylineId((_lastMapPosition.toString())),
                     points:latlng,
                     visible: true,
                     width: 4,
                     color: Colors.red

                 ));
                 print(PolylineId);
               }

               );
             });


             return GoogleMap(
               initialCameraPosition: CameraPosition(
                   target: LatLng(-26.1711459, 27.9002758), zoom: 2.0),
               markers: Set.from(allMarkers),
               onMapCreated: mapCreated,
               polylines: allPolylinesByPosition,
             );
           },
         ),

'''