Skip to content

Instantly share code, notes, and snippets.

@stellarpower
Forked from praeclarum/TileSticher.cs
Last active April 13, 2022 14:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stellarpower/bd31a8d6c5f64307931b385af755fe73 to your computer and use it in GitHub Desktop.
Save stellarpower/bd31a8d6c5f64307931b385af755fe73 to your computer and use it in GitHub Desktop.
Downloads and stitches map tiles
// For a given latitude, longitude, and zoom, returns the URL to an image for that tile.
function tileIndex(latitude, longitude, zoom){
if ((latitude === undefined) || (longitude === undefined) || (zoom === undefined))
throw("Arguments mising in tileIndex");
var lat_rad = latitude / 180 * Math.PI;
var n = 1 << zoom;
var xtile = n * ((longitude + 180) / 360);
var ytile = n * (1 - (Math.log (Math.tan (lat_rad) + 1/Math.cos (lat_rad)) / Math.PI)) / 2;
return [Math.floor(xtile), Math.floor(ytile)]
}
function satelliteImage(latitude, longitude, zoom){
const Server = 'khms2'
const Version = 923
var [x, y] = tileIndex(latitude, longitude, zoom)
return 'https://' + Server + '.google.com/kh/v=' + Version + '?x=' + x + '&y=' + y + '&z=' + zoom
}
// Test: satelliteImage(51.4733514399, -0.00088499646, 15)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment