Flutter Google Maps Polyline-无法将参数类型"LatLng"分配给参数类型"List< LatLng>" [英] Flutter Google Maps Polyline - The argument type 'LatLng' can't be assigned to the parameter type 'List<LatLng>'

查看:425
本文介绍了Flutter Google Maps Polyline-无法将参数类型"LatLng"分配给参数类型"List< LatLng>"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using Google Maps within my Flutter project. I am trying to parse two JSON files in my assets so that I can use their latitudes and longitudes in a Polyline and markers. The markers work fine but now I am adding another future to my Future builder for the polylines and I receive the following error:

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

The argument type LatLng can't be assigned to the parameter type 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;
  });
}

推荐答案

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

Hi I have now done the following , and pulling data but it is not showing on the map

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,
             );
           },
         ),

'''

这篇关于Flutter Google Maps Polyline-无法将参数类型"LatLng"分配给参数类型"List&lt; LatLng&gt;"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆