Public Class VEHelper Public Shared Function GetVETileUrl(ByVal lat As Double, ByVal lon As Double, ByVal zoomLevel As Integer, ByVal mapType As String) As String Dim meterY As Integer = VEHelper.LatitudeToYAtZoom(lat, zoomLevel) Dim meterX As Integer = VEHelper.LongitudeToXAtZoom(lon, zoomLevel) Dim imageSize As Integer = 256 ' Constant Dim tx As Integer = meterX \ imageSize Dim ty As Integer = meterY \ imageSize Dim quadKey As String = VEHelper.TileToQuadKey(tx, ty, zoomLevel) Dim mapExtension As String If (mapType = "h" OrElse mapType = "a") Then mapExtension = ".jpeg" Else ' road mapExtension = ".png" End If 'TODO: what is the ?g= hanging off the end 1 or 15? GetVETileUrl = "http://" + mapType + quadKey.Chars(quadKey.Length - 1) + ".ortho.tiles.virtualearth.net/tiles/" + mapType + quadKey + mapExtension + "?g=1" End Function Private Const earthRadius As Double = 6378137 Private Const earthCircum As Double = earthRadius * 2.0 * Math.PI Private Const earthHalfCirc As Double = earthCircum / 2 Private Shared Function LatitudeToYAtZoom(ByVal lat As Double, ByVal zoom As Integer) As Integer Dim arc As Double = earthCircum / ((1 << zoom) * 256) Dim sinLat As Double = Math.Sin(DegToRad(lat)) Dim metersY As Double = earthRadius / 2 * Math.Log((1 + sinLat) / (1 - sinLat)) Dim y As Integer = CInt(Math.Round((earthHalfCirc - metersY) / arc)) LatitudeToYAtZoom = y End Function Private Shared Function LongitudeToXAtZoom(ByVal lon As Double, ByVal zoom As Integer) As Integer Dim arc As Double = earthCircum / ((1 << zoom) * 256) Dim metersX As Double = earthRadius * DegToRad(lon) Dim x As Integer = CInt(Math.Round((earthHalfCirc + metersX) / arc)) LongitudeToXAtZoom = x End Function Private Shared Function DegToRad(ByVal d As Double) As Double DegToRad = d * Math.PI / 180.0 End Function Private Shared Function MetersPerPixel(ByVal zoom As Integer) As Double MetersPerPixel = earthCircum / ((1 << zoom) * 256) End Function Private Shared Function TileToQuadKey(ByVal tx As Integer, ByVal ty As Integer, ByVal zl As Integer) As String Dim quad As String = "" Dim i As Integer = 0 For i = zl To 1 Step -1 Dim mask As Integer = 1 << (i - 1) Dim cell As Integer = 0 If ((tx And mask) <> 0) Then cell = cell + 1 If ((ty And mask) <> 0) Then cell = cell + 2 quad = quad + cell.ToString() Next TileToQuadKey = quad End Function End Class