So i was able to add marker but there is no MapTileLayerController were i can tell the map that a line is inserted so how i can add a line and update the map.
Hi Neelay,
You can use the below code snippet to add the
MapLineLayer dynamically by clicking a button.
late List late int _index; @override void initState() { _index = 0; data = DataModel(MapLatLng(28.7041, 77.1025), MapLatLng(56.1304, -106.3468)), DataModel(MapLatLng(28.7041, 77.1025), MapLatLng(-9.1900, -75.0152)), DataModel(MapLatLng(28.7041, 77.1025), MapLatLng(61.5240, 105.3188)), DataModel(MapLatLng(28.7041, 77.1025), MapLatLng(-25.2744, 133.7751)), ]; dynamicData = DataModel(MapLatLng(28.7041, 77.1025), MapLatLng(68.32696524655333, 148.44983561102708)), DataModel(MapLatLng(28.7041, 77.1025), MapLatLng(63.92855351840212, -135.96422712579988)), DataModel(MapLatLng(28.7041, 77.1025), MapLatLng(72.22938936025503, -37.87828899297634)), DataModel(MapLatLng(28.7041, 77.1025), MapLatLng(66.1523976337016, 169.8951486221323)), DataModel(MapLatLng(28.7041, 77.1025), MapLatLng(-18.422048339038128, 126.06293841531215)), DataModel(MapLatLng(28.7041, 77.1025), MapLatLng(47.43940631069836, 172.8207507688099)), DataModel(MapLatLng(28.7041, 77.1025), MapLatLng(41.816550455132685, -50.77299853062276)), ]; super.initState(); } @override Widget build(BuildContext context) { return Scaffold( body: Padding( padding: EdgeInsets.all(10), child: Center( child: Column( children: [ SizedBox( height: 500, child: SfMaps( layers: [ MapTileLayer( urlTemplate: 'https://tile.openstreetmap.org/{z}/{x}/{y}.png', sublayers: [ MapLineLayer( lines:
List return MapLine( from: data[index].from, to: data[index].to, ); }).toSet(), ), ], ), ], ), ), SizedBox(height: 20), ElevatedButton( onPressed: () { setState( () { _index = _index + 1; if (_index <= dynamicData.length - 1) { data.add(dynamicData[_index]); } }, ); }, child: Text('Add Lines')) ], ), ), ), ); } |
class DataModel { DataModel(this.from, this.to); final MapLatLng from; final MapLatLng to; } |
Please let us know if you need further assistance in this.
Regards,
Madasamy M