positionToPixel method

Scalable2i positionToPixel(
  1. covariant Position position, {
  2. int zoom = 0,
})

Transforms position to pixel coordinates at zoom.

Coordinate value ranges:

  • pixel x (int): 0 .. mapWidth(zoom) - 1
  • pixel y (int): 0 .. mapHeight(zoom) - 1

Implementation

Scalable2i positionToPixel(covariant Position position, {int zoom = 0}) {
  // map size: number of pixels for x and y at the given zoom level
  final width = mapWidth(zoom);
  final height = mapHeight(zoom);

  // project (geographic or projected) position to pixel coordinates
  final px = converter
      .toScaledX(position.x, width: width)
      .floor()
      .clamp(0, width - 1);
  var py = converter
      .toScaledY(position.y, height: height)
      .floor()
      .clamp(0, height - 1);

  // handle origin variations
  switch (origin) {
    case CanvasOrigin.topLeft:
      // nop
      break;
    case CanvasOrigin.bottomLeft:
      py = (height - 1) - py;
      break;
  }

  return Scalable2i(
    zoom: zoom,
    x: px,
    y: py,
  );
}