Operations related to google map tiles (js, google Maps v3 ),
Some time ago, due to work reasons, I studied tile-related operations on Google Maps. The longitude and latitude of a point and the map level are known to obtain the url of the tile where the point is located. the url of a tile is known to calculate the longitude and latitude of the upper left corner of the tile. the longitude and latitude of a point and the map level are known, calculate the pixel value of this point on the world map.
1. Get the longitude and latitude of a point and the map level to get the url of the tile where the point is located.
Refer to the two blog: This blog (http://blog.csdn.net/lijun_xiao2009/article/details/8178578) in the principle, but the formula used is too difficult to calculate, it is combined with another blog (http://blog.csdn.net/zhaobmox/article/details/1253005) in the calculation.
// Manage the tile path function CalculationUrl (Val_lat, Val_lng) {Val_lng = parseFloat (Val_lng); Val_lat = parseFloat (Val_lat); var Val_zoom = MapAttr. CUTZOOM; // map level, set to the global variable var zoomN = Math. pow (2, Val_zoom); var x = CalculationLng (Val_lng); var y = CalculationLat (Val_lat); // UrlDivvar url_x = x * zoomN; var url_y = y * zoomN; return [Math. floor (url_x), Math. floor (url_y)];} // calculate the tile x value based on the longitude function CalculationLng (lng) {var x = (180.0 + parseFloat (lng)/360.0; return x ;} // calculate the y value of the tile Based on the latitude function CalculationLat (lat) {var PI = Math. PI; // 3.1415926535897; var y =-parseFloat (lat) * PI/180; y = 0.5 * Math. log (1 + Math. sin (y)/(1-Math. sin (y); y * = 1.0/(2 * PI); y + = 0.5; return y ;}
Splice the obtained data into ".../" + zoom + '/' + x + '/'policyunc'.png' ". zoom indicates the map level and xy indicates the data just computed.
2. If the url of a tile is known, calculate the longitude and latitude of the top left corner of the tile.
Refer to the blog: http://www.cnblogs.com/Tangf/archive/2012/04/07/2435545.html
Function CalculationLatlng (x, y) {var PI = Math. PI; var zoomN = Math. pow (2, MapAttr. CUTZOOM); // map level, set to global variable var lng = x/zoomN * 360.0-180.0; var a = PI * (1-2 * y/zoomN ); var e = Math. e; var rad = (Math. pow (e, a)-Math. pow (e,-a) * 0.5var latrad = Math. atan (rad); var lat = latrad * 180.0/PI; return {"lat": lat, "lng": lng };}
3. If we know the longitude and latitude of a point and the map level, calculate the pixel value of this point on the world map.
Reference Baidu Library: http://wenku.baidu.com/link? Url = LG31kM26TJHtYTmBkWMQ92lNEu7P-aUKEowFrEj62Rbsgrc7SiVe9vk2RQ64Sk0dlMvfs1zeiDMx44skLxVZYcT_nJw2guhJQyXksXBvmaK
// Calculate the pixel value of a point on the world map function CalculationPixelDistance (lat, lng) {var zoom = MapAttr. CUTZOOM; <span style = "font-family: Arial, Helvetica, sans-serif;"> // map level, set as global variable </span> // The pixel var X = lngToPixel_x (lng, zoom) of the first tile of the world map relative to the coordinate; var Y = latToPixel_y (lat, zoom); return {x: Math. floor (X), y: Math. floor (Y) };}// calculate the x-pixel value based on the longitude function lngToPixel_x (lng, zoom) {return (lng + 180) * (256 <zoom)/360 ;} // calculate the y pixel value based on the latitude function latToPixel_y (lat, zoom) {var siny = Math. sin (lat * Math. (PI/180); var y = Math. log (1 + siny)/(1-siny) return (128 <zoom) * (1-y/(2 * Math. PI ));}