How to add or draw the polylines in Flutter Maps?
In Syncfusion® Flutter Maps, you can easily add polylines and customize it by following these steps:
Step 1: Add the syncfusion_flutter_maps packages to your dependencies in the pubspec.yaml file.
Step 2: Initialize the polyline points in the initState() method.
late List<MapLatLng> _polylinePoints; late AnimationController _animationController; late Animation _animation; @override void initState() { _polylinePoints = <MapLatLng>[ MapLatLng(13.0827, 80.2707), MapLatLng(13.6373, 79.5037), MapLatLng(14.4673, 78.8242), MapLatLng(14.9091, 78.0092), MapLatLng(16.2160, 77.3566), MapLatLng(17.1557, 76.8697), MapLatLng(18.0975, 75.4249), MapLatLng(18.5204, 73.8567), MapLatLng(19.4760, 72.8777), ]; _animationController = AnimationController( duration: Duration(seconds: 3), vsync: this, ); _animation = CurvedAnimation( parent: _animationController, curve: Curves.easeInOut, ); _animationController.forward(from: 0); super.initState(); }
Step 3: Add MapPolylineLayer to the MapTileLayer sublayers property and set the polyline points to the MapPolylineLayer polylines property. You can customize the polygon by using the MapPolylineLayer animation, color, and width properties.
@override Widget build(BuildContext context) { return SfMaps( layers: [ MapTileLayer( urlTemplate: 'https://tile.openstreetmap.org/{z}/{x}/{y}.png', initialZoomLevel: 5, initialFocalLatLng: MapLatLng(16.2160, 77.3566), initialMarkersCount: _polylinePoints.length, markerBuilder: (BuildContext context, int index) { if (index == _polylinePoints.length - 1) { return MapMarker( latitude: _polylinePoints[index].latitude, longitude: _polylinePoints[index].longitude, child: Transform.translate( offset: Offset(0.0, -8.0), child: Icon(Icons.location_on, color: Colors.red, size: 30), ), ); } else { return MapMarker( latitude: _polylinePoints[index].latitude, longitude: _polylinePoints[index].longitude, iconType: MapIconType.circle, iconColor: Colors.white, iconStrokeWidth: 2.0, size: index == 0 ? Size(12.0, 12.0) : Size(8.0, 8.0), iconStrokeColor: Colors.black, ); } }, sublayers: [ MapPolylineLayer( polylines: { MapPolyline( points: _polylinePoints, color: Color.fromRGBO(0, 102, 255, 1.0), width: 6.0, ) }, animation: _animation, ), ], ), ], ); } }
Screenshot
Check the following links for more features in Syncfusion® Flutter Maps:
- https://www.syncfusion.com/flutter-widgets/flutter-maps
- https://help.syncfusion.com/flutter/maps/vector-layers/polyline-layer
- https://pub.dev/packages/syncfusion_flutter_maps
Live samples
- https://play.google.com/store/apps/details?id=com.syncfusion.flutter.examples
- https://apps.apple.com/us/app/syncfusion-flutter-ui-widgets/id1475231341
- https://flutter.syncfusion.com/#/
- https://www.microsoft.com/store/productId/9NHNBWCSF85D
- https://snapcraft.io/syncfusion-flutter-gallery
Conclusion
I hope you enjoyed learning how to add/draw polylines in Flutter Maps.
You can refer to our Flutter Maps feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our Flatter Maps example to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!