Analysis of Google map block algorithm epsg: 900913
The young man below me always does not understand the map API, the principle of this algorithm. I think many people are the same, so I 'd like to explain it.
Projection: "epsg: 900913", // The projection is 900913, and the Google mocato projection units: "M", // The Screen coordinate is in meters.
Maxresolution:156543.0339, // The unit value of the screen map per pixel in the maximum ratio maxextent: New openlayers. bounds (-20037508,-20037508,20037508, 20037508.34), numzoomlevels: 17. This is the description in openlayer. Most people cannot understand it.
Explanation start:
1. First understand a constant, a map of the world. How many meters does one pixel represent?
In the world, a 256*256 map is a world map (as shown below ). One pixel represents 156543.0339 meters. The area shown in the figure below is = World Area = 156543.0339*256*156543.0339*256
2. In fact, the worldwide mokto range is-20037508,-20037508.34, from the fourth quadrant to the First quadrant
In fact, this value is very simple, that is,-20037508 =-156543.0339*128. Why is it called 128? Because it is half of 256, A pixel * 128 exactly represents the length of a quadrant, that is, half the length of the world.
3. Slice level and scale level
We generally consider slicing to 18 levels, maxzoomlevel = 18; that is to say, a world image is magnified to the power of 18. The width and height are magnified to the power of 2 at the same time.
We can use this function to know how many meters each pixel represents at each scale. The 18-level meter represented by one pixel is 0.5971642833709717 (less than one pixel)
Map. getzoomunits = function (zoom)
{
VaR zu = [0, 0];
VaR A = math. Pow (2, (18-zoom ));
Zu [0] = 0.5971642833709717 *;
Zu [1] = 0.5971642833709717 *;
Return Zu;
};
4. Locate the X, Y, and Z Blocks Based on the latitude and longitude of mokto.
Use the mokto where the vertex is located minus the previous basic mokto value to get the relative absolute distance meter. Then use the total meter or the meter represented by each block at the current scale to know which XY block it is.
China is generally in the First quadrant.
Se. Map. Prototype. tomapid = function (point, zoom)
{
VaR x = point. MercatorLng-156543.0339 * 128;
Var y =-156543.0339 * 128-point.mercatorlat;
VaR zu = This. getzoomunits (zoom, true );
VaR spanx = zu [0] * 256; // zu [0] is the latitude represented by 1px, And this. imgsize is the size of the loaded image (256)
VaR spany = zu [1] * 256; // zu [1] is the longitude represented by 1px, And this. imgsize is the size of the loaded image (256)
VaR BX = parseint (x/spanx );
VaR by = parseint (y/spany );
Return [BX, by, (x-bx * spanx)/spanx * This. imgsize, (Y-by * spany)/spany * This. imgsize];
};
Attached: Algorithm functions for turning longitude and latitude to 84 and 84 to latitude and longitude:
/**
Convert w84 to Mercato coordinates (longitude and latitude to Mercato)
WGS84 is a type of geographic coordinates. There are many types of geographic coordinates, and Web mocato is a projection coordinate.
Lon: w84 longitude
Lat: w84 latitude
*/
Pg. tool. forwardmercator = function (Lon, Lat ){
Lon = parsefloat (Lon );
Lat = parsefloat (LAT );
VaR x = Lon * 20037508.34/180;
Var y = math. Log (math. Tan (90 + LAT) * Math. PI/360)/(math. PI/180 );
Y = y * 20037508.34/180;
Return [x, y];
};
/**
Convert Mercato coordinates to w84 (Mercato to latitude and longitude)
X: Mercato longitude
Y: mocato latitude
*/
Pg. tool. inversemercator = function (x, y ){
VaR Lon = (x/20037508.34) * 180;
VaR lat = (y/20037508.34) * 180;
Lat = 180/Math. Pi * (2 * Math. atan (math. exp (LAT * Math. PI/180)-math. PI/2 );
Return [Lon, lat];
};
OK. See it! It is actually a relationship between the world and the square. Contact me if you have any questions.