﻿var http_request = false;
var poi_encrypt_base_lon = 0,poi_encrypt_base_lat = 0;
var poi_encrypt_diff_lon = 0,poi_encrypt_diff_lat = 0;

function init_request(){
    http_request = false;
    if(window.XMLHttpRequest){
        http_request = new XMLHttpRequest();
        if (http_request.overrideMimeType){
            http_request.overrideMimeType("text/xml");
        }
    }
    else if (window.ActiveXObject){
        try{
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
        }catch (e){
            try{
                http_request = new ActiveXObject("Microsoft.XMLHTTP");
            }catch (e){}
        }
    }
    if (!http_request) {
        window.alert("XMLHttpRequest not support!");
        return false;
    }
    return true;
}

function reverseEncryptPoi(lon,lat){
    var lon_51 = Math.round(parseFloat(lon*100000));
    var lat_51 = Math.round(parseFloat(lat*100000));
    if(!(Math.pow(poi_encrypt_base_lon-lon_51,2)+Math.pow(poi_encrypt_base_lat-lat_51,2)>100000000)){//如果和校准点比较小于指定值，就不在去服务器上取了
        var lon__51 = parseInt(lon_51-poi_encrypt_diff_lon)/100000;
        var lat__51 = parseInt(lat_51-poi_encrypt_diff_lat)/100000;
        return lon__51+","+lat__51;   //经度和纬度
    }else{
        if(!init_request()) return lon+","+lat;
        http_request.open("GET","http://www.bbznl.com/js/map/srntl.aspx?ps="+lon_51+","+lat_51+"&accesskey="+Math.random(),false);         
        http_request.send(null); 
        if(http_request.readyState == 4){
            var encrypt_poi_51 = http_request.responseText.split(',');
            if(encrypt_poi_51.length == 2){
                poi_encrypt_base_lon = encrypt_poi_51[0];
                poi_encrypt_base_lat = encrypt_poi_51[1];
                poi_encrypt_diff_lon = lon_51-encrypt_poi_51[0];
                poi_encrypt_diff_lat = lat_51-encrypt_poi_51[1];
                var lon__51 = parseInt(lon_51-poi_encrypt_diff_lon)/100000;
                var lat__51 = parseInt(lat_51-poi_encrypt_diff_lat)/100000;
                return lon__51+","+lat__51;   //经度和纬度
            }else{
                return lon+","+lat;
            }
        } 
        return lon+","+lat;
    }

}
function EncryptPoi(lon,lat){                               //格式：加密位置，输入"经度 纬度"，输出"经度，纬度"
    var lon_51 = Math.round(parseFloat(lon*100000));
    var lat_51 = Math.round(parseFloat(lat*100000));
    if(!(Math.pow(poi_encrypt_base_lon-lon_51,2)+Math.pow(poi_encrypt_base_lat-lat_51,2)>100000000)){//如果和校准点比较小于指定值，就不在去服务器上取了
        var lon__51 = parseInt(lon_51+poi_encrypt_diff_lon)/100000;
        var lat__51 = parseInt(lat_51+poi_encrypt_diff_lat)/100000;
        return lon__51+","+lat__51;   //经度和纬度
    }else{
        if(!init_request()) return lon+","+lat;
        http_request.open("GET","http://www.bbznl.com/js/map/sntl.aspx?ps="+lon_51+","+lat_51+"&accesskey="+Math.random(),false);         
        http_request.send(null); 
        if(http_request.readyState == 4){
            var encrypt_poi_51 = http_request.responseText.split(',');
            if(encrypt_poi_51.length == 2){
                poi_encrypt_base_lon = lon_51;
                poi_encrypt_base_lat = lat_51;
                poi_encrypt_diff_lon = encrypt_poi_51[0]-lon_51;
                poi_encrypt_diff_lat = encrypt_poi_51[1]-lat_51;
                var lon__51 = parseInt(lon_51+poi_encrypt_diff_lon)/100000;
                var lat__51 = parseInt(lat_51+poi_encrypt_diff_lat)/100000;
                return lon__51+","+lat__51;   //经度和纬度
            }else{
                return lon+","+lat;
            }
        } 
        return lon+","+lat;
    }
}
