mapbar的地图,在地标标注上是有优势的
Mapbar基本算法
因为我当初是用在PHP下的,所以用了php表示算法,基本差不多,应该看得懂:
基础网址:"http://img.mapbar.com/maplite/mapbank/baidu/"
每层地图切片存放在文件夹名称:
$levelstr=array("W","0","1","2","3","4","5","6","7","8","9","10","11","12","15");
即第0层地图切片放在"http://img.mapbar.com/maplite/mapbank/baidu/W/" 下
每层地图切片所跨的经度数(纬度跨度需要*0.8这个系数):
$clipnum=array(90,40,20,10,5,2,1,0.5,0.2,0.1,0.05,0.02,0.01,0.005,0.002);
如第1层的每切片跨经度是90度,跨纬度是90*0.8=72度。即如经度-180~180,共360度,东西半球各需要180/90=2块切片,mapbar的每切片像素为300x300,所以完整地图长度像素应是300*4=1200像素。纬度是-90~90,共180度,南北半球各需要90/72=1.25,需要取大于此数的最小整数即2,所以南北半球加起来仍需要4块,而不是180/72=2.5取整后的3块。从中间0度赤道向两边平铺的,因此上下两块各有3/4块区域留有空白。
哈哈,有点缺乏耐心啦,将就着结合一小段程序来理解吧
//每层的最大横向像素数(除以2得到是纵向最大像素)
function MaxPixel($zoom){
return 256*pow(2,$zoom+2);
}
//经度换算
function PixelToLng($x,$zoom){
$c= MaxPixel($zoom);
return ($x - $c / 2) / ($c / 360);
}
function LngToPixel($lng,$zoom){
$c= MaxPixel($zoom);
return (int)($c / 2 + $lng * $c / 360 + 0.5);
}
//纬度换算
function PixelToLat($y,$zoom){
$c= MaxPixel($zoom);
$g = ($y - $c / 2) / (-$c / (2 * M_PI));
return (2 * Atan(Exp($g)) - M_PI / 2) / (M_PI / 180);
}
function LatToPixel($lat,$zoom){
$c= MaxPixel($zoom);
$f = Sin($lat * (M_PI / 180));
return (int)($c / 2 + 0.5 * Log((1 + $f) / (1 - $f)) * -$c / (2 * M_PI) + 0.5);
}
$blocksize=array(10,10,10,10,10,10,10,10,10,10,50,50,50,50,50);
//$para['ClipXNo']是横向切片序号,注意这个序号不是从0开始的
//横向切片序号运算公式,已知经度是$para['x1']计算所在切片序号:$para['ClipXNo']=floor($para['x1']/$clipnum[$level]);
//纵向切片序号运算公司,已知纬度是$para['y1']计算所在切片序号:$para['ClipYNo']=floor($para['y1']/($clipnum[$level]*0.8));
//所以倒如第一层第一块切片(经纬度分别为-180,90所在的切片)的序号是
$para['ClipXNo']=floor(-180/90)=-2;$para['ClipYNo']=floor(90/72)=2;
$para['FolderXNo']=(int)floor($para['ClipXNo']/$blocksize[$level]);//切片分文件夹存放位置
$para['FolderYNo']=(int)floor($para['ClipYNo']/$blocksize[$level]);
if($para['FolderXNo']<0){
$para['FolderXNo']++;
}
if($para['FolderYNo']<0){
$para['FolderYNo']++;
}
//计算文件序号
$para['FileXNo']=$para['ClipXNo']-$para['FolderXNo']*$blocksize[$level];
$para['FileYNo']=$para['ClipYNo']-$para['FolderYNo']*$blocksize[$level];
//计算出完整切片位置
$para['url']=
"http://img.mapbar.com/maplite/mapbank/baidu/"
.$levelstr[$level]."/".$para['FolderXNo']."_".$para['FolderYNo'].
"/".$para['FileXNo']."_".$para['FileYNo'].".png";