百度离线地图JS API3.0(-04-18)

时间:2024-04-14 09:04:01

1、JS API3.0的下载

由于要离线,就是只能通过内网访问,所以要下载JS API

访问这个网址 http://api.map.baidu.com/api?v=3.0

百度离线地图JS API3.0(2020-04-18)在访问其中的

http://api.map.baidu.com/getscript?v=3.0&ak=&services=&t=20200415105918

将这些代码粘贴下来生成JS文件

百度离线地图JS API3.0(2020-04-18)

2修改本地SJ文件

<1>屏蔽Ak验证

建议:通过“1E5 * Math.random()”找到这段代码,再相对位置添加“if (/^http/.test(a)) return;”

function pa(a, b) {
    // 2020-04-18
    if (/^http/.test(a)) return;
    if (b) {
        var c = (1E5 * Math.random()).toFixed(0);
        D._rd["_cbk" + c] = function (a) {
            b && b(a);
            delete D._rd["_cbk" + c]
        };
        a += "&callback=BMap._rd._cbk" + c
    }

<2>引用本地工具资源

为了便于修改主文件里的一些内容,先创建一个map_load.js文件,加入下面代码:

var bmapcfg = {
    'imgext'      : '.png',   //瓦片图的后缀  根据需要修改,一般是 .png .jpg
    'tiles_dir'   : '',       //普通瓦片图的地址,为空默认在tiles/ 目录
};

var scripts = document.getElementsByTagName("script");
var JS__FILE__ = scripts[scripts.length - 1].getAttribute("src");  //获得当前js文件路径
bmapcfg.home = JS__FILE__.substr(0, JS__FILE__.lastIndexOf("/")+1); //地图API主目录
(function(){
    window.BMap_loadScriptTime = (new Date).getTime();
     console.log('<script type="text/javascript" src="'+bmapcfg.home+'BaiDuAPI3.js"></script>');
    //加载地图API主文件
    document.write('<script type="text/javascript" src="'+bmapcfg.home+'BaiDuAPI3.js"></script>');
})();

百度离线地图JS API3.0(2020-04-18)

接下来,再通过“case "main_domain_nocdn"”定位到如下代码块,添加与注销如下代码:

window.BMAP_PROTOCOL && "https" === window.BMAP_PROTOCOL && (window.HOST_TYPE = 2);
D.Pt = window.HOST_TYPE || "0";
D.url = D.b0[D.Pt];
D.Vo = D.url.proto + D.url.domain.baidumap + "/";
D.md = D.url.proto + ("2" == D.Pt ? D.url.domain.main_domain_nocdn.other : D.url.domain.main_domain_nocdn.baidu) + "/";
//注销
// D.oa = D.url.proto + ("2" == D.Pt ? D.url.domain.main_domain_cdn.other[0] : D.url.domain.main_domain_nocdn.baidu) + "/";
// 2020-04-18
D.oa = bmapcfg.home;//添加
D.Si = D.url.proto + D.url.domain.main_domain_cdn.webmap[0] + "/";
D.Yh = function (a, b) {
    var c, e, b = b || "";
    switch (a) {
        case "main_domain_nocdn":
            c = D.md + b;
            break;
        case "main_domain_cdn":
            c = D.oa + b;
            break;
        default:

再通过“&mod=”找到如下代码块:

load: function (a, b, c) {
    var e = this.mb(a);
    if (e.Ke == this.uj.Cp) c && b(); else {
        if (e.Ke == this.uj.NF) {
            this.EJ(a);
            this.TM(a);
            var f = this;
            f.UB == t && (f.UB = p, setTimeout(function () {
                for (var a = [], b = 0, c = f.Pd.Xm.length; b < c; b++) {
                    var e = f.Pd.Xm[b], n = "";
                    ja.by.zJ(e) ? n = ja.by.get(e) : (n = "", a.push(e + "_" + Sb[e]));
                    f.Pd.sv.push({kM: e, cE: n})
                }
                f.UB = t;
                f.Pd.Xm.length = 0;

                // 2020-04-18
               //注销
                // 0 == a.length ? f.nK() : pa(f.EF.rP + "&mod=" + a.join(","))
                0 == a.length ? f.nK() : pa(bmapcfg.home+'aaa.js');//添加
                console.log(a);   //打印所需模块,添加

最后,通过“("normal")”找到如下代码块,并进行修改(作用:加载自己本地的瓦片):‘

Gd.getTilesUrl = function (a, b, c) {
    var e = a.x, a = a.y, f = Rb("normal"), g = 1, c = Fd[c];
    var tdir = bmapcfg.tiles_dir.length>0?bmapcfg.tiles_dir: "tiles";
    console.log(tdir + "/" + b + "/" + e + "/" + a + bmapcfg.imgext)
    return tdir + "/" + b + "/" + e + "/" + a + bmapcfg.imgext; // 使用本地的瓦片
    // 2020-04-18
    // this.map.$w() && (g = 2);
    // e = this.map.Ye.Vv(e, b).Sl;
    // return (Ed[Math.abs(e + a) % Ed.length] + "?qt=vtile&x=" + (e + "").replace(/-/gi, "M") + "&y=" + (a + "").replace(/-/gi, "M") + "&z=" + b + "&styles=" + c + "&scaler=" + g + (6 == z.fa.na ? "&color_dep=32&colors=50" : "") + "&udt=" + f + "&from=jsapi3_0").replace(/-(\d+)/gi, "M$1")
};

结果展示:

百度离线地图JS API3.0(2020-04-18)

百度离线地图JS API3.0(2020-04-18)

参考:https://blog.****.net/yb305/article/details/103419764

参考:https://blog.****.net/GentleMannn/article/details/86523227

github项目地址:https://github.com/Randy777/bmap/tree/master