ap.js 3.38 KB
Newer Older
fengruiyu's avatar
fengruiyu committed
1 2 3 4 5 6
// var initZsMap = function (data) {
//     console.log(data)
// }

var isWindowLoad=false;
var BridgeIsLoad=false;
7

fengruiyu's avatar
fengruiyu committed
8 9 10 11 12 13 14 15 16 17 18 19 20 21
function tryAllIsOk(){
console.log("tryAllIsOk","isWindowLoad",isWindowLoad,"BridgeIsLoad",BridgeIsLoad)
    if(isWindowLoad){
    try {
                         doAppFun("allIsOk");
                     } catch (e) {
                         console.log('allisOk',e)
                         //  alert("app未接受开始自动跳转" + e)
                                  setTimeout(function(){
                                          doAppFun("allIsOk");

                                  },1500);
                     }
    }
22

fengruiyu's avatar
fengruiyu committed
23 24 25 26
}

function toVurl(a, innerData) {
    var data=JSON.stringify(innerData);
27 28
    if (a == 'b') {
        try {
fengruiyu's avatar
fengruiyu committed
29
            doAppFun("toBVr", data);
30
        } catch (e) {
fengruiyu's avatar
fengruiyu committed
31 32
            console.log('tobvr', data)
            //  alert("app未接受开始自动跳转" + e)
33 34 35 36 37
        }
    }

    if (a == 'g') {
        try {
fengruiyu's avatar
fengruiyu committed
38
            doAppFun("toVr", data);
39
        } catch (e) {
fengruiyu's avatar
fengruiyu committed
40 41 42
            console.log('toVr', data)

            //alert("app未接受开始自动跳转" + e)
43 44 45 46 47
        }
    }

}

fengruiyu's avatar
fengruiyu committed
48 49 50 51 52 53 54 55 56 57 58 59 60


function allIsOk(a, data) {
console.log("innerAllIsOk")
isWindowLoad=true;
tryAllIsOk();
//         setTimeout(function(){
//
//         },1000);

}


61 62 63 64 65 66 67 68 69 70 71 72 73
function doAppFun(funstr, params) {
    //参数一:调用java中的方法   submitFromWeb是方法名,必须和Android中注册时候的方法名称保持一致
    //参数二:返回给Android端的数据,可以为字符串,json等信息
    //参数三:js接收到Android传递过来的数据之后的相应处理逻辑

    window.WebViewJavascriptBridge.callHandler(
        funstr
        , params
        , function (responseData) {
            console.log(responseData)
        }
    );
}
74 75 76 77 78 79 80 81
function centerChanged() {
    try {
        doAppFun("centerChanged", msg);
    } catch (e) {
        console.log("tips",msg)
        //alert("app未弹框==>" + msg + e)
    }
}
82 83 84 85 86

function tips(msg) {
    try {
        doAppFun("tips", msg);
    } catch (e) {
fengruiyu's avatar
fengruiyu committed
87 88
        console.log("tips",msg)
        //alert("app未弹框==>" + msg + e)
89 90 91 92 93 94 95 96 97 98 99 100
    }
}

//JS注册事件监听
function connectWebViewJavascriptBridge(callback) {
    if (window.WebViewJavascriptBridge) {
        callback(WebViewJavascriptBridge)
    } else {
        document.addEventListener(
            'WebViewJavascriptBridgeReady'
            , function () {
                callback(WebViewJavascriptBridge)
fengruiyu's avatar
fengruiyu committed
101 102
                BridgeIsLoad=true;
               tryAllIsOk()
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
            },
            false
        );
    }
}


//注册回调函数,第一次连接时调用 初始化函数
connectWebViewJavascriptBridge(function (bridge) {
    //初始化
    bridge.init(function (message, responseCallback) {
        var data = {
            'Javascript Responds': 'Wee!'
        };
        responseCallback(data);
fengruiyu's avatar
fengruiyu committed
118

119 120 121 122 123 124 125 126 127 128
    });


    //Android调用js方法:functionInJs方法名称需要保持一致 ,并返回给Android通知

    bridge.registerHandler("initZsMap", function (data, responseCallback) {
        initZsMap(data);
        responseCallback("suc:initZsMap");
    });

129 130 131 132 133 134 135 136 137 138
    bridge.registerHandler("addZoom", function (data, responseCallback) {
        addZoom();
        responseCallback("suc:addZoom");
    });

    bridge.registerHandler("reduceZoom", function (data, responseCallback) {
        reduceZoom();
        responseCallback("suc:reduceZoom");
    });

139 140 141 142
})