卓越2008

用一颗谦虚的心面对大家,用一颗坚定的心面对困难,用一颗执著的心面对理想,用一颗虔诚的心面对技术。

导航

google map 经纬度转换成ASCII码的方法

Posted on 2007-11-27 03:45  Casm  阅读(798)  评论(0)    收藏  举报
//经纬度转ASCII码
function pointEncoding(value, isStart, startValue)
{
    var hasFlag = true;     //没有符号
    var strNum = value.toString();
    if (strNum.indexOf('.') == -1)  //如果没有小数点
    {
        strNum += "00000";
    }
    else if (strNum.length - strNum.indexOf('.') < 5)   //小数点后位数不足5位
    {
        var stratLen = strNum.length;
        for (var i = 0; i < 6 - (stratLen - strNum.indexOf('.')); i++)
        {
            strNum += "0";
        }
    }
    else    //仅保留小数点后5位
    {
        strNum = strNum.substring(0, strNum.indexOf('.') + 6);
    }
    strNum = strNum.replace('.', '');
   
    if (!isStart && startValue)
    {
        if (startValue.indexOf('.') == -1)  //如果没有小数点
        {
            startValue += "00000";
        }
        else if (startValue.length - startValue.indexOf('.') < 5)   //小数点后位数不足5位
        {
            stratLen = startValue.length;
            for (var i = 0; i < 6 - (stratLen - startValue.indexOf('.')); i++)
            {
                startValue += "0";
            }
        }
        else    //仅保留小数点后5位
        {
            startValue = startValue.substring(0, startValue.indexOf('.') + 6);
        }
        startValue = startValue.replace('.', '');
       
        strNum = parseInt(strNum) - parseInt(startValue);
    }
   
    strNum = complementCode(strNum);
//    strNum = parseInt(parseInt(strNum, 10), 2);
   
    //左移一位,以符号为填充
    if (strNum.charAt(0) == "0")
    {
        hasFlag = false;
        strNum = (strNum + strNum.charAt(0)).substring(1, strNum.length + 1);
    }
    else
    {
        //取补码
        var strTemp = "";
        for (i = 0; i < strNum.length; i++)
        {
            strTemp += strNum.charAt(i) == "0" ? "1" : "0";
        }
//       
//        var cy = 1;
//       
//        strNum = "";
//        for (i = strTemp.length - 1; i >= 0; i--)
//        {
//            strNum = (cy + parseInt(strTemp.charAt(i)) % 2).toString() + strNum;
//            cy = (cy + parseInt(strTemp.charAt(i)) == 2) ? 1 : 0;
//        }
        strNum = (strTemp + "1").substring(1, strTemp.length + 1);
    }
    //切割成5位2进制数组
    var bitList = new Array();
    stratLen = strNum.length % 5;
    for (i = 0; i < (5 - stratLen); i++)
    {
        strNum = "0" + strNum;
    }
   
    var index = 0;
    for (i = 0; i < strNum.length /5; i++)
    {
        if (strNum.substring(i * 5, (i + 1) * 5) == "00000" && hasFlag)
        {
            continue;
        }
        bitList[index++] = strNum.substring(i * 5, (i + 1) * 5);
    }
   
    //颠倒顺序
    var bitStrTemp = "";
    for (i = 0; i < bitList.length / 2; i++)
    {
        bitStrTemp = bitList[i];
        bitList[i] = bitList[bitList.length - i - 1];
        bitList[bitList.length - i - 1] = bitStrTemp;
    }
   
    //对没一组字位同0x20做或运算,不包括最后一组,然后转换成10进制数,加63,并以ascii的方式存入
    strNum = "";
    for (i = 0; i < bitList.length; i++)
    {
        if (i != bitList.length - 1)
        {
            bitList[i] = parseInt(parseInt(bitList[i], 2), 10) | 0x20;
            bitList[i] = bitList[i] + 63;
        }
        else
        {
            bitList[i] = parseInt(parseInt(bitList[i], 2), 10) + 63;
        }
        strNum += String.fromCharCode(bitList[i]);
    }
   
    return strNum;
}
// 补码转换函数
function complementCode(n)
{
var cc = "";//补码
var Num = Number(n);
var aBit = "";
var countFor1 = 0;
var absNum = Math.abs(Num);
var Num2 = absNum.toString(2); //转换成二进制
if (Num < 0)
{
    for(var i = Num2.length; i > 0; i--)
  {
      aBit = Num2.substring(i - 1, i);
      if(countFor1 >= 1)
   {
        cc = (1 ^ aBit) + cc;
      }
   else
   {
        if(aBit == "1")
    {
     countFor1=countFor1 + 1;
    }
        cc = aBit + cc;
      }
    }
    cc = "1" + cc;  //补标志位
   
    var stratLen = cc.length % 8;
    for (i = 0; i < (8 - stratLen); i++)
    {
        cc = "1" + cc;
    }
}
else //整数的补码不变
{
    cc = "0"+Num2;  //补标志位 
}
return cc;
}
//偏差调整
function pointAdjust(value, adjustValue)
{
    return value + adjustValue;
}