latitudeToTileY method

  1. @override
int latitudeToTileY(
  1. double latitude
)
override

Converts a latitude coordinate (in degrees) to a tile Y number at a certain zoom level.

@param latitude the latitude coordinate that should be converted. @param zoomLevel the zoom level at which the coordinate should be converted. @return the tile Y number of the latitude value.

Implementation

@override
int latitudeToTileY(double latitude) {
  const double pi180 = pi / 180;
  const double pi4 = 4 * pi;
  double sinLatitude = sin(latitude * pi180);
  // exceptions for 90 and -90 degrees
  if (sinLatitude == 1.0) return 0;
  if (sinLatitude == -1.0) return _scalefactor.scalefactor.floor() - 1;
  double tileY = (0.5 - log((1 + sinLatitude) / (1 - sinLatitude)) / pi4);
  // print(
  //     "tileY: $tileY, sinLat: $sinLatitude, log: ${log((1 + sinLatitude) / (1 - sinLatitude))}");
  int result = (tileY * _scalefactor.scalefactor).floor();
  //print("Mercator: ${tileY * _scalefactor.scalefactor}");
  // seems with Latitude boundingBox.maxLatitude we get -1.5543122344752192e-15 so correct it to 0
  if (result < 0) return 0;
  if (result >= _maxTileCount) return _maxTileCount - 1;
  return result;
}