| 1 | package
|
|---|
| 2 | {
|
|---|
| 3 | import flash.geom.Point;
|
|---|
| 4 |
|
|---|
| 5 | public class GMapCalc
|
|---|
| 6 | {
|
|---|
| 7 | public static const DEFAULT_TILE_SIZE:int = 256;
|
|---|
| 8 | public static var TILE_SIZE:int = DEFAULT_TILE_SIZE;
|
|---|
| 9 |
|
|---|
| 10 | private static function bround(v:Number, min:Number, max:Number):Number
|
|---|
| 11 | {
|
|---|
| 12 | if (v>max) return max;
|
|---|
| 13 | if (v<min) return min;
|
|---|
| 14 | return v;
|
|---|
| 15 | }
|
|---|
| 16 |
|
|---|
| 17 | public static function XYtoLatLng(x:Number, y:Number, out:LatLng):Boolean
|
|---|
| 18 | {
|
|---|
| 19 | const PI:Number = Math.PI;
|
|---|
| 20 | const DPI:Number = PI * 2.0;
|
|---|
| 21 | const HPI:Number = PI / 2.0;
|
|---|
| 22 |
|
|---|
| 23 | var lng:Number = bround((x-0.5) * DPI, -PI, PI);
|
|---|
| 24 |
|
|---|
| 25 | var g:Number = (y-0.5) * -DPI;
|
|---|
| 26 | var lat:Number = 2.0 * Math.atan( Math.exp(g) ) - HPI;
|
|---|
| 27 |
|
|---|
| 28 | out.lat = lat;
|
|---|
| 29 | out.lng = lng;
|
|---|
| 30 | return true;
|
|---|
| 31 | }
|
|---|
| 32 |
|
|---|
| 33 | public static function LatLngToXY(lat:Number, lng:Number, out:Point):Boolean
|
|---|
| 34 | {
|
|---|
| 35 | const DPI:Number = Math.PI * 2.0;
|
|---|
| 36 | const HPI:Number = Math.PI / 2.0;
|
|---|
| 37 |
|
|---|
| 38 | var x:Number = lng/DPI + 0.5;
|
|---|
| 39 |
|
|---|
| 40 | var s:Number = Math.sin(lat);
|
|---|
| 41 | var c:Number = Math.cos(lat);
|
|---|
| 42 | var y:Number = Math.log((1+c+s)/(1+c-s)) / -DPI + 0.5;
|
|---|
| 43 |
|
|---|
| 44 | out.x = x;
|
|---|
| 45 | out.y = y;
|
|---|
| 46 |
|
|---|
| 47 | return true;
|
|---|
| 48 | }
|
|---|
| 49 |
|
|---|
| 50 | public static function calcMapSize(zoom:int):int
|
|---|
| 51 | {
|
|---|
| 52 | return Math.pow(2, zoom+8);
|
|---|
| 53 | }
|
|---|
| 54 |
|
|---|
| 55 | public static function calcMapTileCols(zoom:int):int
|
|---|
| 56 | {
|
|---|
| 57 | return Math.pow(2, zoom);
|
|---|
| 58 | }
|
|---|
| 59 |
|
|---|
| 60 | public static function calcTileData(x:Number, y:Number, zoom:int, layer:int):Object
|
|---|
| 61 | {
|
|---|
| 62 | if (zoom < 0)
|
|---|
| 63 | throw "illegal zoom level";
|
|---|
| 64 |
|
|---|
| 65 | var mapSize:Number = calcMapSize(zoom);
|
|---|
| 66 | var ox:int = Math.floor(x*mapSize) % TILE_SIZE;
|
|---|
| 67 | var oy:int = Math.floor(y*mapSize) % TILE_SIZE;
|
|---|
| 68 | var tx:int = Math.floor(x*mapSize / TILE_SIZE);
|
|---|
| 69 | var ty:int = Math.floor(y*mapSize / TILE_SIZE);
|
|---|
| 70 |
|
|---|
| 71 | switch(layer)
|
|---|
| 72 | {
|
|---|
| 73 | case 0:
|
|---|
| 74 | return {tile_index: {x:tx, y:ty}, offset: {x:ox, y:oy}, tile_zoom: (17-zoom)};
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | throw "illegal layer index";
|
|---|
| 78 | }
|
|---|
| 79 |
|
|---|
| 80 | public static function calcTilePath(tx:int, ty:int, zoom:int):String
|
|---|
| 81 | {
|
|---|
| 82 | var path:Array = [];
|
|---|
| 83 | var qrts:String = "qrts";
|
|---|
| 84 |
|
|---|
| 85 | for (var i:int = 0;i < zoom;i++)
|
|---|
| 86 | {
|
|---|
| 87 | var t:int = (tx & 1) | ((ty & 1) << 1);
|
|---|
| 88 | tx /= 2;
|
|---|
| 89 | ty /= 2;
|
|---|
| 90 | path.unshift( qrts.charAt(t) );
|
|---|
| 91 | }
|
|---|
| 92 | path.unshift( 't' );
|
|---|
| 93 |
|
|---|
| 94 | return path.join('');
|
|---|
| 95 | }
|
|---|
| 96 |
|
|---|
| 97 | public static function calcMapsZoomIndex(z:int):int
|
|---|
| 98 | {
|
|---|
| 99 | return 17-z;
|
|---|
| 100 | }
|
|---|
| 101 | }
|
|---|
| 102 | } |
|---|