The library I'm using for displaying interactive maps, reports the visible region in the following form:

  • latitude specifies north-south position on the Earth of the point at the center of the screen
  • longitude specifies east-west position on the Earth of the point at the center of the screen
  • latitudeDelta specifies north-south span of the visisble area
  • longitudeDelta specifies east-west span of the visisble area

Map is rendered using Mercator projection. I would like to use this information to find coordinates of the edges of visisble area.

The problem

Finding boundaries for longitude is simple:

$$ lonEast, lonWest = lon \pm \frac{lonD}{2} $$

For latitudes it's not as simple. Here's the function y(φ) for the Mercator projection:

$$ f(φ) = ln(tan(\frac{φ}{2} + \frac{\pi}{4})) $$

In relation to the screen, the edges remain equally spaced from the central point. This will not be true in geographical coordinates, unless central point belongs to the equator.

Here's a graph of the above function to help illustrate the point:

mercator projection

With latitude and latitudeDelta known, how do I figure out latSouth, latNorth?


I think it comes down to solving:

$$ f(latS) + f(latS + latD) = 2f(lat) $$

$$ f(φ) = ln(tan(\frac{φ}{2} + \frac{\pi}{4})) $$

Which expands to:

$$ ln(tan(\frac{latS}{2} + \frac{\pi}{4})) + ln(tan(\frac{latS + latD}{2} + \frac{\pi}{4})) = 2 * ln(tan(\frac{lat}{2} + \frac{\pi}{4})) $$


Let $g$ be the inverse function of $f$, then

$$f \left({\varphi}\right) = v \Leftrightarrow \tan \left(\frac{{\varphi}}{2}+\frac{{\pi}}{4}\right) = {e}^{v} \Leftrightarrow \frac{{\varphi}}{2}+\frac{{\pi}}{4} = \arctan \left({e}^{v}\right) \Leftrightarrow {\varphi} = \boxed{g \left(v\right) = 2 \arctan \left({e}^{v}\right)-\frac{{\pi}}{2}}$$

If ${\varphi}$ is the latitude, let $\boxed{v = f \left({\varphi}\right)}$, one has ${{\varphi}}_{S} = g \left(v-h\right)$ and ${{\varphi}}_{N} = g \left(v+h\right)$ for some $h$.

Let ${\Delta} = {{\varphi}}_{N}-{{\varphi}}_{S}$, one has

$${\Delta} = g \left(v+h\right)-g \left(v-h\right) = 2 \arctan \left({e}^{v+h}\right)-2 \arctan \left({e}^{v-h}\right)$$

Using the well known formula $\tan \left(a-b\right) = \frac{\tan \left(a\right)-\tan \left(b\right)}{1+\tan \left(a\right) \tan \left(b\right)}$, it gives

$$\tan \left(\frac{{\Delta}}{2}\right) = \frac{{e}^{v+h}-{e}^{v-h}}{1+{e}^{v+h} {e}^{v-h}} = \frac{{e}^{h}-{e}^{{-h}}}{{e}^{{-v}}+{e}^{v}} = \frac{\sinh \left(h\right)}{\cosh \left(v\right)}$$

Let now $\boxed{u = \cosh \left(v\right) \tan \left(\frac{{\Delta}}{2}\right)}$, one has

$$\sinh \left(h\right) = u \Leftrightarrow \boxed{h = \text{argsinh} \left(u\right) = \ln \left(u+\sqrt{1+{u}^{2}}\right)}$$

One can now compute the south and north latitudes ${{\varphi}}_{S}$ and ${{\varphi}}_{N}$. The use of exponential and logarithms can be completely eliminated by setting

$$t = \tan \left(\frac{{\varphi}}{2}+\frac{{\pi}}{4}\right)$$

then one has

$$u = \frac{1}{2} \left(t+\frac{1}{t}\right) \tan \left(\frac{{\Delta}}{2}\right)$$

$${{\varphi}}_{S} = 2 \arctan \left(\frac{t}{u+\sqrt{1+{u}^{2}}}\right)-\frac{{\pi}}{2}$$

$${{\varphi}}_{N} = 2 \arctan \left(t \left(u+\sqrt{1+{u}^{2}}\right)\right)-\frac{{\pi}}{2}$$

  • $\begingroup$ Thank you. It's pretty accurate in practice, the distance between projected boundaries matches up to the 4th decimal point when compared on the far north and far south at low zoom level. I blame nuances of floating point operations for this inaccuracy. $\endgroup$ – mpontus Aug 15 '17 at 14:01
  • $\begingroup$ You could perhaps check this more accurately with a multiprecision library. $\endgroup$ – Gribouillis Aug 15 '17 at 14:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.