Created
September 30, 2013 23:01
-
-
Save tmcw/6771554 to your computer and use it in GitHub Desktop.
[wip]
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // the marker positions, as an array | |
| // of arrays of numbers. | |
| var markers = [[0, 0], [180, 0]]; | |
| var D2R = Math.PI / 180, | |
| R2D = 180 / Math.PI, | |
| A = 6378137, | |
| MAXEXTENT = 20037508.34, | |
| tox = A * D2R; | |
| function getStaticUrl(markers, width, height, maxZoom) { | |
| // if you don't provide a maxZoom, default to 15. it's a safe number, | |
| // but fit to taste. | |
| if (maxZoom === undefined) maxZoom = 15; | |
| // there's only one marker, the extent is just that, there's nothing to | |
| // fit, and we just return that marker at the maximum zoom level. | |
| if (markers.length === 1) { | |
| return [markers[0][0], markers[0][1], maxZoom]; | |
| } | |
| var extent = [ | |
| [markers[0][0], markers[0][1]], | |
| [markers[0][0], markers[0][1]] | |
| ]; | |
| for (var i = 1; i < markers.length; i++) { | |
| var x = markers[i][0], | |
| y = markers[i][1]; | |
| // if this point is outside of the extent, extend it. | |
| if (x < extent[0][0]) extent[0][0] = x; | |
| if (x > extent[1][0]) extent[1][0] = x; | |
| if (y < extent[0][1]) extent[0][1] = y; | |
| if (y > extent[1][1]) extent[1][1] = y; | |
| } | |
| // project this extent into mercator | |
| var extent_projected = [[ | |
| extent[0][0] * tox, | |
| A * Math.log(Math.tan((Math.PI * 0.25) + (0.5 * extent[0][1] * D2R))) | |
| ], [ | |
| extent[1][0] * tox, | |
| A * Math.log(Math.tan((Math.PI * 0.25) + (0.5 * extent[1][1] * D2R))) | |
| ]]; | |
| var mwidth = extent_projected[1][0] - extent_projected[0][0], | |
| mheight = extent_projected[1][1] - extent_projected[0][1]; | |
| var fitted_dimension = Math.max(mwidth, mheight); | |
| var fit_zoom = maxZoom; | |
| while (MAXEXTENT / Math.pow(2, fit_zoom) < fitted_dimension) { | |
| fit_zoom--; | |
| } | |
| console.log(fit_zoom); | |
| } | |
| getStaticUrl(markers); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment