Skip to content

Commit fa7c991

Browse files
Clamp latitude to 85 degrees (#123)
1 parent 0c95935 commit fa7c991

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,11 @@ bool fetchMap(LGFX_Sprite &map, double longitude, double latitude, uint8_t zoom,
9999
```
100100

101101
- Overflowing `longitude` are wrapped and normalized to +-180°.
102-
- Overflowing `latitude` are clamped to +-90°.
102+
- Overflowing `latitude` are clamped to +-85°.
103103
- Valid range for the `zoom` level is from `getMinZoom()` to `getMaxZoom()`.
104104
- `timeoutMS` can be used to throttle the amount of downloaded tiles per call.
105105
Setting it to anything other than `0` sets a timeout. Sane values start around ~100ms.
106-
**Note:** No more tiles will be downloaded after the timeout expires, but tiles that are downloading will be finished.
106+
**Note:** No more tile downloads will be started after the timeout expires, but tiles that are downloading will be finished.
107107
**Note:** You might end up with missing map tiles. Or no map at all if you set the timeout too short.
108108

109109
### Free the psram memory used by the tile cache

src/OpenStreetMap-esp32.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,8 +325,13 @@ bool OpenStreetMap::fetchMap(LGFX_Sprite &mapSprite, double longitude, double la
325325
return false;
326326
}
327327

328+
// Web Mercator projection only supports latitudes up to ~85.0511°.
329+
// See https://en.wikipedia.org/wiki/Web_Mercator_projection#Formulas
330+
// We use 85.0° as a safe and simple boundary.
331+
constexpr double MAX_MERCATOR_LAT = 85.0;
332+
328333
longitude = fmod(longitude + 180.0, 360.0) - 180.0;
329-
latitude = std::clamp(latitude, -90.0, 90.0);
334+
latitude = std::clamp(latitude, -MAX_MERCATOR_LAT, MAX_MERCATOR_LAT);
330335

331336
tileList requiredTiles;
332337
computeRequiredTiles(longitude, latitude, zoom, requiredTiles);

0 commit comments

Comments
 (0)