location2tile static method

Tuple2<int, int> location2tile(
  1. int zoom,
  2. double lat,
  3. double lng
)

Convert tile zoom, latitude, longitude to tile x, y. cf. https://developers.google.com/maps/documentation/javascript/examples/map-coordinates

Implementation

static Tuple2<int, int> location2tile(int zoom, double lat, double lng) {
  final scale = 1 << zoom;
  var siny = sin(lat * pi / 180);
  siny = min(max(siny, -0.9999), 0.9999);

  return Tuple2<int, int>(
    ((0.5 + lng / 360) * scale).floor(),
    ((0.5 - log((1 + siny) / (1 - siny)) / (4 * pi)) * scale).floor(),
  );
}