createPolygonCommandBatch function

JsonCommandBatch createPolygonCommandBatch(
  1. PolygonDto polygon, {
  2. bool fitCamera = true,
  3. double padding = 80,
  4. double maxZoom = 16,
  5. CommandBuilderOptions options = const CommandBuilderOptions(namespace: 'polygons'),
})

Implementation

JsonCommandBatch createPolygonCommandBatch(
  PolygonDto polygon, {
  bool fitCamera = true,
  double padding = 80,
  double maxZoom = 16,
  CommandBuilderOptions options = const CommandBuilderOptions(
    namespace: 'polygons',
  ),
}) {
  requireName(polygon.id, 'id');
  assertJsonValue(polygon.properties);
  if (!polygon.fillOpacity.isFinite ||
      polygon.fillOpacity < 0 ||
      polygon.fillOpacity > 1) {
    throw ArgumentError('opacity must be 0..1');
  }
  final resolved = _resolvePolygon(polygon);
  final ids = getPolygonCommandIds(
    polygon.id,
    namespace: options.namespace,
    origin: options.origin,
  );
  return buildCommandBatch([
    mapCommand('source', 'remove', {'sourceId': ids.sourceId}),
    sourceAction(ids.sourceId, [
      {
        'type': 'Feature',
        'id': polygon.id,
        'geometry': resolved.geometry,
        'properties': {
          ...polygon.properties,
          'id': polygon.id,
          'label': polygon.label ?? '',
          'kind': 'polygon',
        },
      },
    ]),
    layerAction(
      ids.sourceId,
      'fill',
      'fill',
      paint: {
        'fill-color': polygon.fillColor,
        'fill-opacity': polygon.fillOpacity,
      },
    ),
    if (polygon.strokeColor != null)
      layerAction(
        ids.sourceId,
        'stroke',
        'line',
        layout: {'line-cap': 'round', 'line-join': 'round'},
        paint: {
          'line-color': polygon.strokeColor,
          'line-width': polygon.strokeWidth,
        },
      ),
    if (polygon.label != null)
      layerAction(
        ids.sourceId,
        'label',
        'symbol',
        layout: {'text-field': polygon.label, 'text-size': 12},
        paint: {
          'text-color': '#1E293B',
          'text-halo-color': '#FFFFFF',
          'text-halo-width': 2,
        },
      ),
    if (fitCamera)
      mapCommand('camera', 'fitBounds', {
        'bounds': boundsOf(resolved.points),
        'padding': padding,
        'maxZoom': maxZoom,
      }),
  ], options: options);
}