posChart.vue 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <script setup name="posChart">
  2. import { ref, reactive, shallowRef, defineProps, watch, nextTick, onActivated, defineEmits } from 'vue'
  3. import AMapLoader from '@amap/amap-jsapi-loader';
  4. import { ElMessage } from 'element-plus';
  5. import request from '@/api/axios.js'
  6. // import stationPosRes from './stationPos.json'
  7. const emits = defineEmits(['rightClick','mapDone'])
  8. const map = shallowRef(null);
  9. const aMap = ref(null)
  10. const container = ref()
  11. const funMapSet = (callback = (flag) => {}) => {
  12. AMapLoader.load({
  13. key:" 540080009cfdae95b6b5a4f47af24f90", // 申请好的Web端开发者Key,首次调用 load 时必填
  14. version:"2.0", // 指定要加载的 JSAPI 的版本,缺省时默认为 1.4.15
  15. plugins:[''], // 需要使用的的插件列表,如比例尺'AMap.Scale'等
  16. }).then((AMap)=>{
  17. aMap.value = AMap;
  18. map.value = new AMap.Map(container.value,{ //设置地图容器id
  19. viewMode:"3D", //是否为3D地图模式
  20. zoom:9, //初始化地图级别
  21. center:[106.065974,37.428168], //初始化地图中心点位置
  22. layers: [
  23. // 卫星
  24. new AMap.TileLayer.Satellite(),
  25. // 路网
  26. new AMap.TileLayer.RoadNet()
  27. ]
  28. });
  29. callback(true)
  30. }).catch(e=>{
  31. console.error(e);
  32. callback(false)
  33. })
  34. }
  35. const funStationPos = async () => {
  36. const res = await request.get('/base/station', {params: {}})
  37. // const res = stationPosRes
  38. if(res.code === 200){
  39. if(res.data && res.data.length){
  40. map.value.clearMap()
  41. res.data.forEach(item => {
  42. const marker = new aMap.value.Marker({
  43. icon: "https://webapi.amap.com/theme/v1.3/markers/n/mark_r.png",
  44. position: [Number(item.longitude), Number(item.latitude)],
  45. anchor:'bottom-center'
  46. });
  47. map.value.add(marker)
  48. // marker.setTitle(item.name);
  49. // 设置label标签
  50. // label默认蓝框白底左上角显示,样式className为:amap-marker-label
  51. marker.setLabel({
  52. direction:'right',
  53. offset: new aMap.value.Pixel(10, 0), //设置文本标注偏移量
  54. content: `${item.name}`, //设置文本标注内容
  55. });
  56. })
  57. map.value.setFitView()
  58. }
  59. }
  60. }
  61. const props = defineProps({
  62. windList: {
  63. type: Array,
  64. default: () => []
  65. },
  66. height: {
  67. type: String,
  68. default: '100%'
  69. }
  70. })
  71. /** 右键后对象数据的接收 */
  72. const rightObj = ref(null)
  73. /**监听windList */
  74. watch(() => props.windList, (val) => {
  75. if(val && val.length){
  76. map.value.clearMap()
  77. val.forEach(item => {
  78. const marker = new aMap.value.Marker({
  79. icon: "https://webapi.amap.com/theme/v1.3/markers/n/mark_r.png",
  80. position: [Number(item.longitude), Number(item.latitude)],
  81. anchor:'bottom-center'
  82. });
  83. //创建右键菜单
  84. const contextMenu = new aMap.value.ContextMenu();
  85. // {
  86. // "id": "NG01_05",
  87. // "code": "NG01-05",
  88. // "windpowerstationid": "NSS_FDC",
  89. // "longitude": 106.0223579,
  90. // "latitude": 37.74820326,
  91. // "modelid": "UP82",
  92. // "status": "NG01_005",
  93. // "projectid": "NSS01_GC",
  94. // "lineid": "NSS01_XL",
  95. // "firstintegratedtime": "2011-12-01 00:00:00",
  96. // "photo": null,
  97. // "name": "牛首山05号风机",
  98. // "standardid": "NG01_05",
  99. // "prepareId": null,
  100. // "processId": "1077593559936270336",
  101. // "fittingId": "1078321689411977217"
  102. // }
  103. /**右键菜单可根据props中传参定义 */
  104. //右键1
  105. contextMenu.addItem("功率曲线拟合", (e) => {
  106. console.log(rightObj.value)
  107. // funCombineGet(rightObj.value)
  108. emits('rightClick', {
  109. menuIndex: 0,
  110. current: rightObj.value,
  111. })
  112. }, 0);
  113. //右键2
  114. contextMenu.addItem("对风偏差分析", () => {
  115. emits('rightClick', {
  116. menuIndex: 1,
  117. current: rightObj.value,
  118. })
  119. }, 1);
  120. // //右键2
  121. // contextMenu.addItem("温度与功率分析", () => {
  122. // emits('rightClick', {
  123. // menuIndex: 2,
  124. // current: rightObj.value
  125. // })
  126. // }, 2);
  127. map.value.add(marker)
  128. // marker.setTitle(item.name);
  129. // 设置label标签
  130. // label默认蓝框白底左上角显示,样式className为:amap-marker-label
  131. marker.setLabel({
  132. direction:'right',
  133. offset: new aMap.value.Pixel(10, 0), //设置文本标注偏移量
  134. content: `${item.name}`, //设置文本标注内容
  135. });
  136. //绑定鼠标右击事件——弹出右键菜单
  137. marker.on('rightclick', function (e) {
  138. rightObj.value = item
  139. contextMenu.open(map.value, e.lnglat);
  140. });
  141. })
  142. map.value.setFitView()
  143. setTimeout(() => {
  144. let zoom = map.value.getZoom()
  145. if(zoom > 16){
  146. zoom = 16
  147. map.value.setZoom(zoom)
  148. }
  149. },1000)
  150. }else{
  151. funStationPos()
  152. }
  153. })
  154. //created
  155. onActivated(() => {
  156. funMapSet(mapStatus => {
  157. if(!mapStatus){
  158. ElMessage.error('地图未加载成功, 请刷新重试或检查网络!')
  159. return false
  160. }
  161. // funStationPos()
  162. emits('mapDone', mapStatus)
  163. })
  164. })
  165. </script>
  166. <template>
  167. <div :style="{height: props.height}">
  168. <div class="h-full" ref="container">
  169. <el-empty description="地图未加载成功, 请刷新重试或检查网络!"></el-empty>
  170. </div>
  171. </div>
  172. </template>
  173. <style scoped></style>