判断请求是浏览器发出还是小程序发出
直接上代码:
<script type="text/javascript" src="https://res.wx.qq.com/open/js/jweixin-1.3.2.js"></script> <script type="text/javascript"> function openMap() { const ua = window.navigator.userAgent.toLowerCase(); if (ua.indexOf('micromessenger') == -1) {//不在微信或者小程序中 window.location.href = 'https://apis.map.qq.com/uri/v1/marker?marker=coord:31.235371,121.488571;title:国拍大楼;'; } else { wx.miniProgram.getEnv(function (res) { //true代表在小程序里 if (res.miniprogram) { wx.miniProgram.navigateTo({ url: '../../pages/gpmap/gpmap' }) } else { window.open("https://api.map.baidu.com/marker?location=31.24119,121.495083&title=我的位置&content=国拍大楼&output=html") } }) } } </script>