当前位置:   article > 正文

H5 显示百度地图并调起APP_iframe嵌套百度地图无法唤醒app

iframe嵌套百度地图无法唤醒app

本文示例,在H5中调用百度地图的js api,根据经纬度显示地图,同时可点击打开百度地图的APP,将当前定位传到APP中,实现路径规划。注意安卓和苹果的,调起APP的时候协议是不同的。

  1. <head>
  2. <title>示例</title>
  3. <script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=你的密钥"></script>
  4. <style type="text/css">
  5. #allmap
  6. {
  7. position:absolute;
  8. width: 100%;
  9. height: 100%;
  10. margin: 0;
  11. }
  12. </style>
  13. <script type="text/javascript">
  14. var scheme = "";
  15. $(function () {
  16. var map = new BMap.Map("allmap");
  17. map.centerAndZoom(new BMap.Point(116.331398, 39.897445), 11); //经纬度
  18. map.enableScrollWheelZoom(true);
  19. map.clearOverlays();
  20. var new_point = new BMap.Point(Number('@ViewBag.Longitude'), Number('@ViewBag.Latitude'));
  21. var marker = new BMap.Marker(new_point); // 创建标注
  22. map.addOverlay(marker); // 将标注添加到地图中
  23. map.panTo(new_point);
  24. map.setZoom(18);
  25. map.addControl(new BMap.ScaleControl({ anchor: BMAP_ANCHOR_BOTTOM_LEFT, offset: { height: 30, width: 5 } }));// 左下角,添加比例尺
  26. var cr = new BMap.CopyrightControl({ anchor: BMAP_ANCHOR_BOTTOM_RIGHT, offset: { height: 30, width: 5 } }); //设置版权控件位置
  27. map.addControl(cr); //添加版权控件
  28. cr.addCopyright({ id: 1, content: "<a οnclick='OpenApp()' href='#' style='font-size: 16px;background: #ebedf0;border: solid 1px #606366;border-radius: 4px;padding: 2px;color: #606366;text-decoration: none;'>打开百度地图</a>" });
  29. });
  30. function OpenApp() {
  31. var geolocation = new BMap.Geolocation();
  32. geolocation.getCurrentPosition(function (r) {
  33. if (this.getStatus() == BMAP_STATUS_SUCCESS) {
  34. if (r.point.lat != "" && r.point.lng != "") {
  35. scheme = "://map/direction?origin=latlng:" + r.point.lat + "," + r.point.lng + "|name:我的位置&destination=latlng:@ViewBag.Latitude,@ViewBag.Longitude|name:@ViewBag.Address&mode=driving";
  36. var I = navigator.userAgent;
  37. var isiPad = (I.match(/(iPad).*OS\s([\d_]+)/)) ? true : false;
  38. var isAndroid = I.match(/android/i) ? true : false;
  39. var isMac = (I.match(/(Mac\sOS)\sX\s([\d_]+)/)) ? true : false;
  40. var isiPhone = (!isiPad && I.match(/(iPhone\sOS)\s([\d_]+)/)) ? true : false;
  41. var isSafari = (isiPhone || isiPad) && I.match(/Safari/);
  42. var isMQQBrowser = I.match(/MQQBrowser\/([\d\.]+)/) ? true : false;
  43. var isUCBrowser = I.match(/UCBrowser\/([\d\.]+)/) ? true : false;
  44. var isQQ = (!isMQQBrowser) ? (I.match(/QQ\/([\d\.]+)/) ? true : false) : false;
  45. var safariVersion = 0;
  46. isSafari && (safariVersion = I.match(/Version\/([\d\.]+)/));
  47. try { safariVersion = parseFloat(safariVersion[1], 10) } catch (R) { }
  48. var isSAMSUNG = I.toUpperCase().indexOf("SAMSUNG-SM-N7508V") != -1;
  49. // 尝试打开手机APP
  50. if ((isSafari && safariVersion >= 9) || isMac || isSAMSUNG || isUCBrowser || isMQQBrowser || isQQ || isiPhone) {
  51. tryIOpen(scheme);
  52. } else {
  53. tryAOpen(scheme);
  54. }
  55. } else {
  56. alert("无法获取当前位置!");
  57. }
  58. }
  59. else {
  60. alert("无法获取当前位置!");
  61. console('无法获取当前位置' + this.getStatus());
  62. }
  63. }, { enableHighAccuracy: true, timeout: 1000 });
  64. }
  65. function tryAOpen(scheme) {
  66. var protocol = "bdapp";
  67. var iframe = document.createElement("iframe");
  68. iframe.Id = "i" + Math.random().toString().replace(".", "");
  69. iframe.src = protocol+scheme;
  70. iframe.style.display = "none";
  71. document.body.appendChild(iframe);
  72. setTimeout(function () {
  73. document.body.removeChild(iframe);
  74. }, 500)
  75. }
  76. function tryIOpen(scheme) {
  77. var protocol = "baidumap";
  78. var a = document.createElement("a");
  79. a.id = "a" + Math.random().toString().replace(".", "");
  80. a.href = protocol + scheme ;
  81. document.body.appendChild(a);
  82. var T = document.createEvent("HTMLEvents");
  83. T.initEvent("click", !1, !1),
  84. a.dispatchEvent(T)
  85. }
  86. </script>
  87. </head>
  88. <div id="allmap"> </div>
以上纯属个人独自研究成果,仅供参考,转载请注明出处
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/188471
推荐阅读
相关标签
  

闽ICP备14008679号