customMap.vue 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <template>
  2. <view class="map-popup-box">
  3. <view class="icon-title-navBar-box">
  4. <view @click="goUpPage" class="nav-bar-icon"></view>
  5. <text class="nav-bar-title">选择地址</text>
  6. </view>
  7. <!-- 查询职业 -->
  8. <view class="phone-search-box">
  9. <input class="search-input" placeholder="请输入地址" v-model="data.keyword" />
  10. <view class="search-icon" @click="handleClick">
  11. <uni-icons type="search" size="24" color="#fff"></uni-icons>
  12. </view>
  13. </view>
  14. <map style="width: 100%; height: 300px;" :latitude="data.latitude" :longitude="data.longitude"
  15. :markers="data.covers" :show-location="true"> </map>
  16. <view v-if="result" class="map-card-list">
  17. <!-- #ifdef H5 -->
  18. <view v-for="(item,index) in result.data" key="index" @click="xuanzeAdress(item)" class="map-card-box">
  19. <view>
  20. <view class="map-card-title"><text v-if="index==0" class="dqwz-text">当前位置</text>{{item.title}}
  21. </view>
  22. <view class="map-card-address">{{item.address}}</view>
  23. </view>
  24. <icon class="map-active-icon" v-if="index==0"></icon>
  25. </view>
  26. <!-- #endif -->
  27. <!-- #ifdef APP -->
  28. <view v-for="(item,index) in result.data.data" key="index" @click="xuanzeAdress(item)" class="map-card-box">
  29. <view>
  30. <view class="map-card-title"><text v-if="index==0" class="dqwz-text">当前位置</text>{{item.title}}
  31. </view>
  32. <view class="map-card-address">{{item.address}}</view>
  33. </view>
  34. <icon class="map-active-icon" v-if="index==0"></icon>
  35. </view>
  36. <!-- #endif -->
  37. </view>
  38. </view>
  39. </template>
  40. <script setup>
  41. import {
  42. reactive,
  43. ref
  44. } from "vue"
  45. import myIcon from "@/static/images/common/markIcon.png"
  46. import {
  47. onLoad,
  48. onHide,
  49. onShow
  50. } from "@dcloudio/uni-app"
  51. import {
  52. useCustomMap
  53. } from "@/components/customMap/useMap.js"
  54. const emits = defineEmits(['close', 'xuanzeAdress','currentWeizhi'])
  55. const {
  56. getPositionSearchByKeyword
  57. } = useCustomMap();
  58. const result = ref(null);
  59. function handleClick() {
  60. if (data.keyword) {
  61. getPositionSearchByKeyword(data.keyword, `nearby(${data.latitude},${data.longitude},1000)`, onSuccess,
  62. onError, {
  63. page: 1,
  64. size: 3,
  65. })
  66. }
  67. }
  68. function onSuccess(res) {
  69. console.log('数据', res);
  70. result.value = res
  71. // #ifdef H5
  72. if (res && res.data && res.data.length > 0) {
  73. // 获取第一个搜索结果的经纬度
  74. const firstResult = res.data[0];
  75. const newLatitude = firstResult.location.lat; // 纬度
  76. const newLongitude = firstResult.location.lng; // 经度
  77. // 更新地图的中心点
  78. data.latitude = newLatitude;
  79. data.longitude = newLongitude;
  80. // 更新标记点
  81. data.covers = [{
  82. latitude: newLatitude,
  83. longitude: newLongitude,
  84. iconPath: myIcon, // 使用你的图标路径
  85. width: 30,
  86. height: 30
  87. }];
  88. }
  89. // #endif
  90. // #ifdef APP
  91. if (res.data && res.data.data && res.data.data.length > 0) {
  92. // 获取第一个搜索结果的经纬度
  93. const firstResult = res.data.data[0];
  94. const newLatitude = firstResult.location.lat; // 纬度
  95. const newLongitude = firstResult.location.lng; // 经度
  96. // 更新地图的中心点
  97. data.latitude = newLatitude;
  98. data.longitude = newLongitude;
  99. // 更新标记点
  100. data.covers = [{
  101. latitude: newLatitude,
  102. longitude: newLongitude,
  103. iconPath: myIcon, // 使用你的图标路径
  104. width: 30,
  105. height: 30
  106. }];
  107. }
  108. // #endif
  109. }
  110. function onError(err) {
  111. console.log('错误', err)
  112. }
  113. function goUpPage(err) {
  114. console.log('错误', err)
  115. emits('close');
  116. }
  117. function xuanzeAdress(item) {
  118. console.log('item',item);
  119. emits('xuanzeAdress', item.address);
  120. emits('currentWeizhi', item.location);
  121. }
  122. const data = reactive({
  123. keyword: '', // 关键字
  124. id: 0, // 使用 marker点击事件 需要填写id
  125. title: 'map',
  126. latitude: 0,
  127. longitude: 0,
  128. covers: [{
  129. latitude: 0,
  130. longitude: 0,
  131. iconPath: myIcon,
  132. width: 30,
  133. height: 30
  134. }]
  135. })
  136. // 初始化
  137. function initLocation() {
  138. console.log('init')
  139. uni.getLocation({
  140. type: 'gcj02',
  141. success: function(res) {
  142. console.log('res', res)
  143. data.longitude = res.longitude;
  144. data.latitude = res.latitude;
  145. data.covers[0].longitude = res.longitude;
  146. data.covers[0].latitude = res.latitude;
  147. console.log('当前位置的经度:' + res.longitude);
  148. console.log('当前位置的纬度:' + res.latitude);
  149. // 获取当前位置附近的数据
  150. getPositionSearchByKeyword('公司', `nearby(${res.latitude},${res.longitude},1000)`, onSuccess,
  151. onError, {
  152. page: 1,
  153. size: 3,
  154. });
  155. }
  156. });
  157. }
  158. onLoad(() => {
  159. console.log('初始化')
  160. initLocation();
  161. })
  162. </script>
  163. <style>
  164. </style>