map.html 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <!DOCTYPE html>
  2. <html style="">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport"
  6. content="width=device-width,user-scalable=no,initial-scale=1.0,maximum-scale=1.0,minimum-scale=1.0">
  7. <title></title>
  8. <script src="./js/jquery3.5.js" type="text/javascript"></script>
  9. <!-- 引入uniapp -->
  10. <script src="./js/hybrid_html_uni.webview.1.5.5.js" type="text/javascript"></script>
  11. <!-- 引入天地图 -->
  12. <script src="http://api.tianditu.gov.cn/api?v=4.0&tk=ffb54337376426c1941e6d6bf5b5f8e5" type="text/javascript"></script>
  13. <script>
  14. let map; //地图实例
  15. let zoom = 16; //缩放等级
  16. //等待dom加载
  17. function onLoad() {
  18. let lnglat = {};
  19. map = new T.Map('mapDiv'); //实例化地图 120.139726,33.345562
  20. map.centerAndZoom(new T.LngLat(120.139726, 33.345562), zoom); //默认数值
  21. //创建比例尺控件对象
  22. var scale = new T.Control.Scale();
  23. //添加比例尺控件
  24. map.addControl(scale);
  25. //创建缩放平移控件对象
  26. var control_zoom = new T.Control.Zoom();
  27. control_zoom.setPosition(T_ANCHOR_BOTTOM_RIGHT);
  28. // control_zoom.setOffset();
  29. //添加缩放平移控件
  30. map.addControl(control_zoom);
  31. // 添加控件
  32. var ctrl = new T.Control.MapType([
  33. {
  34. title: "地图", //地图控件上所要显示的图层名称
  35. icon: "http://api.tianditu.gov.cn/v4.0/image/map/maptype/vector.png", //地图控件上所要显示的图层图标(默认图标大小80x80)
  36. layer: TMAP_NORMAL_MAP //地图类型对象,即MapType。
  37. },
  38. {
  39. title: "卫星",
  40. icon: "http://api.tianditu.gov.cn/v4.0/image/map/maptype/satellite.png",
  41. layer: TMAP_SATELLITE_MAP
  42. }
  43. ]);
  44. // map.addControl(ctrl);
  45. let geolocation = new T.Geolocation()
  46. //获取当前用户经纬度
  47. // geolocation.getCurrentPosition(function(e) {
  48. // //根据经纬度重新设置地图
  49. // if (this.getStatus() == 0) {
  50. // map.centerAndZoom(e.lnglat, 16)
  51. // }
  52. // if (this.getStatus() == 1) {
  53. // map.centerAndZoom(e.lnglat, e.level)
  54. // }
  55. // });
  56. // 等待uniapp加载完成后发送信息
  57. document.addEventListener('UniAppJSBridgeReady', function() {
  58. // uni.postMessage({
  59. // data: {
  60. // title: '天地图',
  61. // msg: "zzww"
  62. // },
  63. // })
  64. })
  65. $.ajax({
  66. url: "/api/map/mapVisualization/getBuildingList",
  67. dataType: "json",
  68. type: "get",
  69. success: function (data) {
  70. console.log( data );
  71. if (data != null && data.length > 0) {
  72. batchMark( data );
  73. }
  74. },
  75. error:function(){
  76. alert( "请求楼宇信息失败!" );
  77. },
  78. });
  79. } // function onLoad
  80. // 批量添加标注
  81. function batchMark(buildingData) {
  82. if (buildingData != null && buildingData.length > 0) {
  83. var pointArray = new Array();
  84. for (var i = 0; i < buildingData.length; i++) {
  85. var point = new T.LngLat(buildingData[i].building_longitude, buildingData[i].building_latitude);
  86. var marker = new T.Marker(point); // 创建标注
  87. marker.markerName = buildingData[i].building_name;
  88. marker.buildingId = buildingData[i].building_id;
  89. marker.addEventListener("click", markerClick);
  90. map.addOverLay(marker);
  91. pointArray[i] = point;
  92. }
  93. //让所有点在视野范围内
  94. map.setViewport(pointArray);
  95. }
  96. }
  97. function markerClick(e) {
  98. uni.postMessage({
  99. data: {
  100. buildingId: e.target.buildingId,
  101. buildingName: e.target.markerName
  102. },
  103. })
  104. }
  105. //传递楼宇信息
  106. window.msgFromUniapp = function(buildingData) {
  107. batchMark(buildingData)
  108. }
  109. </script>
  110. <style>
  111. body,
  112. html {
  113. padding: 0;
  114. margin: 0;
  115. width: 100%;
  116. height: 100%;
  117. transform: translateX(-1.5px);
  118. }
  119. .tdt-control-copyright {
  120. display: none !important;
  121. }
  122. .tdt-control-datasources {
  123. display: none !important;
  124. }
  125. </style>
  126. </head>
  127. <body style="width:100%; height:100%" onLoad="onLoad()">
  128. <div id="mapDiv" style="width:100%; height:100%"></div>
  129. </body>
  130. </html>