addMarkers method

  1. @override
Future<List<Marker>> addMarkers({
  1. required List<Marker> markers,
})
override

Add the given markers to the map. The markers must have a valid image path. The image will be loaded from the asset and converted to base64 before sending to the native platform. Width and height of each marker must be both provided or both null

Implementation

@override
Future<List<Marker>> addMarkers({required List<Marker> markers}) async {
  final markersJson = <Map<String, dynamic>>[];
  await Future.forEach(
    markers,
    (Marker m) async {
      try {
        final bytesData = await rootBundle.load(m.imagePath);
        final imageBytes = bytesData.buffer.asUint8List();
        final base64Image = base64Encode(imageBytes);

        final markerJson = m.toJson();
        markerJson['base64Encoded'] = base64Image;

        markersJson.add(markerJson);
      } catch (e) {
        debugPrint(e.toString());
      }
    },
  );

  final responseMarkersJson = await methodChannel.invokeListMethod<int>(
    Events.addMarkers,
    markersJson,
  );

  /// If the response is not empty, set the markerId for each marker.
  if (responseMarkersJson?.isNotEmpty ?? false) {
    for (var i = 0; i < markers.length; i++) {
      markers[i].markerId = responseMarkersJson![i];
    }
    return markers;
  } else {
    return [];
  }
}