Android Question Get Latitude and Longitude from map tile (x, y, zoom)

RB Smissaert

Well-Known Member
Licensed User
Longtime User
I can see the code to get this on this wiki page:

eg in Python:

Tile numbers to lon./lat.​

import math
def num2deg(xtile, ytile, zoom):
n = 2.0 ** zoom
lon_deg = xtile / n * 360.0 - 180.0
lat_rad = math.atan(math.sinh(math.pi * (1 - 2 * ytile / n)))
lat_deg = math.degrees(lat_rad)
return (lat_deg, lon_deg)

But so far been unable to translate this to B4A to the the right results.

Any suggestions?

RBS
 

TILogistic

Expert
Licensed User
Longtime User
sinh = hyperbolic sine

See;

Create calculation function Sinh
B4X:
Public Sub TileNumbersTolonlat(xtitle As Double, ytile As Double, Zoom As Int) As Double()
    Dim n As Int = Power(2, Zoom)
    Dim Lon_Deg As Double = xtitle / n * 360 - 180
    Dim Lat_Deg As Double = ToDeg(ATan(Sinh(cPI * (1 - 2 * ytile / n)))) 'create calculation function Sinh
    Return Array As Double(Lon_Deg, Lat_Deg)
End Sub

Public Sub ToDeg(Value As Double) As Double
    Return Value * 180 / cPI
End Sub
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
This is Basic4ppc code that I wrote about a year ago
B4X:
    'Lat Long from slippy tile numbers
    Dim Temp2(5)
    
    n = 2 ^ zoom
    lon_deg = xtile / n * 360.0 - 180.0
    
    lat_rad = ATan(sinh(cPI * (1 - 2 * ytile / n)))
    lat_deg = lat_rad * 180.0 / cPI
    
Sub sinh(x)
    s = (cE^x - cE^(0-x)) / 2
    Return s
End Sub
 
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
Not tested:
Edit
B4X:
Public Sub TileNumbersToLonLat(xTitle As Double, yTile As Double, Zoom As Double) As Double()
    Dim n As Double = Power(2, Zoom)
    Dim Lon_Deg As Double = xTitle / n * 360.0 - 180.0
    Dim Lat_Deg As Double = ToDeg(ATan(SinH(cPI * (1 - 2 * yTile / n))))
    Return Array As Double(Lon_Deg, Lat_Deg)
End Sub

Public Sub ToDeg(Value As Double) As Double
    Return Value * 180 / cPI
End Sub

Public Sub SinH(Value As Double) As Double
    Return (Power(cE, Value) - Power(cE, -Value)) / 2
End Sub
 
Last edited:
Upvote 0

RB Smissaert

Well-Known Member
Licensed User
Longtime User
not tested:
B4X:
Public Sub TileNumbersToLonLat(xTitle As Double, yTile As Double, Zoom As Int) As Double()
    Dim n As Int = Power(2, Zoom)
    Dim Lon_Deg As Double = xTitle / n * 360.0 - 180.0
    Dim Lat_Deg As Double = ToDeg(ATan(SinH(cPI * (1 - 2 * yTile / n))))
    Return Array As Double(Lon_Deg, Lat_Deg)
End Sub

Public Sub ToDeg(Value As Double) As Double
    Return Value * 180 / cPI
End Sub

Public Sub SinH(Value As Double) As Double
    Return (Power(cE, Value) - Power(cE, -Value)) / 2
End Sub
Thanks to all replies.

I actually had the right code to get the coordinates, but supplied the wrong arguments.
To test, I got the lowest numbers for the tile column and tile row (1010 and 670 resp.) and thought that
I could add any zoom value to this so passed 19. Obviously this was very wrong and once I supplied
the right zoom number (11) I got the right results (52.696361078274485 and -2.4609375)

RBS
 
Upvote 0
Top