H5唤醒本地APP(谷歌地图、百度地图等相通)

说一下兼容性问题:
IOS:safari浏览器,google浏览器,UC浏览器可用QQ浏览器无法使用,其他的没测。
Android:只有google浏览器可以打开谷歌地图,百度地图没测试。

首先:判断当前操作环境与浏览器内核:

let browser = {
        versions: function () {
            let u = navigator.userAgent,
                app = navigator.appVersion;
            return {
                trident: u.indexOf('Trident') > -1, /*IE内核*/
                presto: u.indexOf('Presto') > -1, /*opera内核*/
                webKit: u.indexOf('AppleWebKit') > -1, /*苹果、谷歌内核*/
                gecko: u.indexOf('Gecko') > -1 && u.indexOf('KHTML') == -1, /*火狐内核*/
                mobile: !!u.match(/AppleWebKit.*Mobile.*/), /*是否为移动终端*/
                ios: !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), /*ios终端*/
                // android: u.indexOf('Android') > -1 || u.indexOf('Linux') > -1, /*android终端或者uc浏览器*/
                android: u.indexOf('Android') > -1 || u.indexOf('Adr') > -1,
                iPhone: u.indexOf('iPhone') > -1, /*是否为iPhone或者QQHD浏览器*/
                iPad: u.indexOf('iPad') > -1, /*是否iPad*/
                webApp: u.indexOf('Safari') == -1, /*是否web应该程序,没有头部与底部*/
                souyue: u.indexOf('souyue') > -1,
                superapp: u.indexOf('superapp') > -1,
                weixin: u.toLowerCase().indexOf('micromessenger') > -1,
                Safari: u.indexOf('Safari') > -1,
                uc: u.indexOf('Linux') > -1
            };

        }(),
        language: (navigator.browserLanguage || navigator.language).toLowerCase()
    };

然后就可以附上你的代码了: 

function openApp() {
        if (browser.versions.ios) {
            window.location.href = "https://www.google.com/maps/dir/?api=1&origin="+startAddress+"&destination="+endAddress+"&travelmode=driving";
        }
        else if (browser.versions.android) {
             window.location.href = "https://www.google.com/maps/dir/?api=1&origin="+startAddress+"&destination="+endAddress+"&travelmode=driving";
        }
    }

以上的是 JS唤醒本地谷歌地图,传入起点终点的小demo。目前没发现什么其他问题。 
如果,是在iframe的子页面中编写以上代码,在请求地图数据时会有跨域请求的错误! 
解决方法: 
以上代码写在父页面中,在子页面中通过window.parent.openApp();调用即可。 
 

猜你喜欢

转载自blog.csdn.net/qq_42445490/article/details/89154823