VillageMiniMap.vue 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <template>
  2. <div
  3. class="mini-village-map"
  4. :style="{
  5. borderColor: themeContext.resolveThemeColor('primary'),
  6. }"
  7. >
  8. <map
  9. id="prevMap"
  10. map-id="prevMap"
  11. class="mini-village-map-map"
  12. :enable-poi="false"
  13. :scale="12"
  14. :longitude="AppCofig.defaultLonLat[0]"
  15. :latitude="AppCofig.defaultLonLat[1]"
  16. />
  17. <NoticeBar
  18. v-if="currentNoticeContent"
  19. :content="currentNoticeContent"
  20. :innerStyle="{
  21. position: 'absolute',
  22. top: '20rpx',
  23. left: '20rpx',
  24. right: '20rpx',
  25. zIndex: 100,
  26. borderRadius: '30rpx',
  27. }"
  28. :textStyle="{
  29. fontSize: '26rpx',
  30. }"
  31. icon="https://xy.wenlvti.net/app_static/images/home/IconLightActive.png"
  32. :iconProps="{
  33. size: 34,
  34. }"
  35. textColor="#C9211F"
  36. backgroundColor="#D9492E10"
  37. />
  38. <Button
  39. :innerStyle="{
  40. position: 'absolute',
  41. bottom: '20rpx',
  42. right: '20rpx',
  43. zIndex: 100,
  44. backgroundColor: '#ffffff',
  45. }"
  46. icon="navigation"
  47. @click="getCurrentLonlat"
  48. >定位</Button>
  49. </div>
  50. </template>
  51. <script setup lang="ts">
  52. import { getCurrentInstance, onMounted, ref } from 'vue';
  53. import { waitTimeOut } from '@imengyu/imengyu-utils';
  54. import { useTheme } from '@/components/theme/ThemeDefine';
  55. import LightVillageApi from '@/api/light/LightVillageApi';
  56. import MapApi from '@/api/map/MapApi';
  57. import AppCofig from '@/common/config/AppCofig';
  58. import Button from '@/components/basic/Button.vue';
  59. import NoticeBar from '@/components/display/NoticeBar.vue';
  60. const instance = getCurrentInstance();
  61. const mapCtx = uni.createMapContext('prevMap', instance);
  62. const currentAddress = ref<string>('');
  63. const currentLonlat = ref<{ longitude: number, latitude: number }>({
  64. longitude: AppCofig.defaultLonLat[0],
  65. latitude: AppCofig.defaultLonLat[1],
  66. });
  67. const emit = defineEmits(['getedCurrentLonlat']);
  68. const props = defineProps<{
  69. startLonlat?: { longitude: number, latitude: number } | undefined;
  70. }>();
  71. const currentNoticeContent = ref('目前厦门市已被点亮一个社区,刚刚小亮贡献了10个光源,让湖里区点亮了一个社区');
  72. const themeContext = useTheme();
  73. function getCurrentLonlat() {
  74. uni.getLocation({
  75. type: 'wgs84',
  76. success: async (res) => {
  77. currentLonlat.value = {
  78. longitude: res.longitude,
  79. latitude: res.latitude,
  80. };
  81. const address = await MapApi.regeo(res.latitude, res.longitude);
  82. currentAddress.value = address.district;
  83. emit('getedCurrentLonlat', currentLonlat.value);
  84. mapCtx.moveToLocation({
  85. latitude: currentLonlat.value.latitude,
  86. longitude: currentLonlat.value.longitude,
  87. });
  88. },
  89. });
  90. }
  91. onMounted(async () => {
  92. if (props.startLonlat) {
  93. currentLonlat.value = props.startLonlat;
  94. mapCtx.moveToLocation({
  95. latitude: currentLonlat.value.latitude,
  96. longitude: currentLonlat.value.longitude,
  97. });
  98. }
  99. if (!props.startLonlat) {
  100. const res = await LightVillageApi.getIpAddress();
  101. currentLonlat.value = {
  102. longitude: Number(res.point.x),
  103. latitude: Number(res.point.y),
  104. };
  105. await waitTimeOut(100);
  106. currentAddress.value = res.address_detail.district;
  107. emit('getedCurrentLonlat', currentLonlat.value);
  108. mapCtx.moveToLocation({
  109. latitude: currentLonlat.value.latitude,
  110. longitude: currentLonlat.value.longitude,
  111. });
  112. }
  113. });
  114. </script>
  115. <style lang="scss">
  116. .mini-village-map {
  117. position: relative;
  118. width: 100%;
  119. height: 300rpx;
  120. border-radius: 20rpx;
  121. overflow: hidden;
  122. border-style: solid;
  123. border-width: 1px;
  124. .mini-village-map-map {
  125. width: 100%;
  126. height: 340rpx;
  127. }
  128. }
  129. </style>