Skip to content

Performance improvement with zoom/scale #2183

@monsieurtanuki

Description

@monsieurtanuki

What do you want implemented?

Not sure how much performance improvements we can get from this.

We have a couple of methods for projecting LatLng.

  (double, double) latLngToXY(LatLng latlng, double scale);

  /// Similar to [latLngToXY] but converts the XY coordinates to an [Offset].
  Offset latLngToOffset(LatLng latlng, double zoom) {
    final (x, y) = latLngToXY(checkLatLng(latlng), scale(zoom));
    return Offset(x, y);
  }

The most used method is latLngToOffset, that computes a scale.

  /// Zoom to Scale function.
  double scale(double zoom) => 256.0 * math.pow(2, zoom);

The thing is that we constantly compute math.pow(2, zoom).
Given that most of the time (always?) we work on a single zoom level for all points, we could cache the latest zoom/scale values.
Something like that:

  double _latestZoom = -1;
  double _latestScale = -1;

  double scale(double zoom) {
    if ((_latestZoom == -1 && _latestScale == -1) || _latestZoom != zoom) {
      _latestZoom = zoom;
      return _latestScale = 256.0 * math.pow(2, zoom);
    }
    return _latestScale;
  }

What other alternatives are available?

No response

Can you provide any other information?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions