customMap.vue 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. <!-- <view class="city-picker-box">
  8. <picker mode="region" @change="handleCityChange">
  9. <view class="city-picker">
  10. <text>{{ selectedCity }}</text>
  11. <uni-icons type="arrowdown" size="16" color="#666"></uni-icons>
  12. </view>
  13. </picker>
  14. </view> -->
  15. <t-index-address @select="select"></t-index-address>
  16. <!-- 查询职业 -->
  17. <view class="phone-search-box">
  18. <input class="search-input" placeholder="请输入地址" v-model="data.keyword" />
  19. <view class="search-icon" @click="handleClick">
  20. <uni-icons type="search" size="24" color="#fff"></uni-icons>
  21. </view>
  22. </view>
  23. <map style="width: 100%; height: 300px;" :latitude="data.latitude" :longitude="data.longitude"
  24. :markers="data.covers" :show-location="true"> </map>
  25. <view v-if="result" class="map-card-list">
  26. <!-- #ifdef H5 -->
  27. <view v-for="(item,index) in result.data" key="index" @click="xuanzeAdress(item)" class="map-card-box">
  28. <view>
  29. <view class="map-card-title"><text v-if="index==0" class="dqwz-text">当前位置</text>{{item.title}}
  30. </view>
  31. <view class="map-card-address">{{item.address}}</view>
  32. </view>
  33. <icon class="map-active-icon" v-if="index==0"></icon>
  34. </view>
  35. <!-- #endif -->
  36. <!-- #ifdef APP -->
  37. <view v-for="(item,index) in result.data.data" key="index" @click="xuanzeAdress(item)" class="map-card-box">
  38. <view>
  39. <view class="map-card-title"><text v-if="index==0" class="dqwz-text">当前位置</text>{{item.title}}
  40. </view>
  41. <view class="map-card-address">{{item.address}}</view>
  42. </view>
  43. <icon class="map-active-icon" v-if="index==0"></icon>
  44. </view>
  45. <!-- #endif -->
  46. </view>
  47. </view>
  48. </template>
  49. <script setup>
  50. import {
  51. reactive,
  52. ref
  53. } from "vue"
  54. import myIcon from "@/static/images/common/markIcon.png"
  55. import {
  56. onLoad,
  57. onHide,
  58. onShow
  59. } from "@dcloudio/uni-app"
  60. import {
  61. useCustomMap
  62. } from "@/components/customMap/useMap.js"
  63. const emits = defineEmits(['close', 'xuanzeAdress', 'currentWeizhi'])
  64. const {
  65. getPositionSearchByKeyword
  66. } = useCustomMap();
  67. const result = ref(null);
  68. const selectedCity = ref('选择城市'); // 默认显示“选择城市”
  69. function select(data){
  70. console.log('data',data);
  71. },
  72. function handleCityChange(e) {
  73. //const city = e.detail.value[1]; // 获取选择的城市
  74. // selectedCity.value = city;
  75. // 根据选择的城市更新地图中心点
  76. getCityLocation("北京");
  77. }
  78. function getCityLocation(city) {
  79. console.log('222222');
  80. const Key = `KBYBZ-FS3KZ-U2FXR-7VPDX-L7J46-23F55`
  81. const url = `https://apis.map.qq.com/ws/geocoder/v1/?address=${city}&key=${Key}`;
  82. uni.request({
  83. url: url,
  84. method: 'get',
  85. timeout: 20000,
  86. }).then(res => {
  87. console.log('res', res);
  88. if (res.data && res.data.result && res.data.result.location) {
  89. const cityLocation = res.data.result.location;
  90. console.log('cityLocation', cityLocation);
  91. if (cityLocation) {
  92. data.latitude = cityLocation.lat;
  93. data.longitude = cityLocation.lng;
  94. data.covers[0].latitude = cityLocation.lat;
  95. data.covers[0].longitude = cityLocation.lng;
  96. // 重新搜索附近的地点
  97. getPositionSearchByKeyword(data.keyword || '公司',
  98. `nearby(${cityLocation.lat},${cityLocation.lng},1000)`,
  99. onSuccess, onError, {
  100. page: 1,
  101. size: 3,
  102. });
  103. }
  104. }
  105. }).catch(err => {
  106. console.log('err')
  107. }).finally(() => {
  108. })
  109. return null;
  110. }
  111. function handleClick() {
  112. if (data.keyword) {
  113. getPositionSearchByKeyword(data.keyword, `nearby(${data.latitude},${data.longitude},1000)`, onSuccess,
  114. onError, {
  115. page: 1,
  116. size: 3,
  117. })
  118. }
  119. }
  120. function onSuccess(res) {
  121. console.log('数据', res);
  122. result.value = res
  123. // #ifdef H5
  124. if (res && res.data && res.data.length > 0) {
  125. // 获取第一个搜索结果的经纬度
  126. const firstResult = res.data[0];
  127. const newLatitude = firstResult.location.lat; // 纬度
  128. const newLongitude = firstResult.location.lng; // 经度
  129. // 更新地图的中心点
  130. data.latitude = newLatitude;
  131. data.longitude = newLongitude;
  132. // 更新标记点
  133. data.covers = [{
  134. latitude: newLatitude,
  135. longitude: newLongitude,
  136. iconPath: myIcon, // 使用你的图标路径
  137. width: 30,
  138. height: 30
  139. }];
  140. }
  141. // #endif
  142. // #ifdef APP
  143. if (res.data && res.data.data && res.data.data.length > 0) {
  144. // 获取第一个搜索结果的经纬度
  145. const firstResult = res.data.data[0];
  146. const newLatitude = firstResult.location.lat; // 纬度
  147. const newLongitude = firstResult.location.lng; // 经度
  148. // 更新地图的中心点
  149. data.latitude = newLatitude;
  150. data.longitude = newLongitude;
  151. // 更新标记点
  152. data.covers = [{
  153. latitude: newLatitude,
  154. longitude: newLongitude,
  155. iconPath: myIcon, // 使用你的图标路径
  156. width: 30,
  157. height: 30
  158. }];
  159. }
  160. // #endif
  161. }
  162. function onError(err) {
  163. console.log('错误', err)
  164. }
  165. function goUpPage(err) {
  166. console.log('错误', err)
  167. emits('close');
  168. }
  169. function xuanzeAdress(item) {
  170. console.log('item', item);
  171. emits('xuanzeAdress', item.address);
  172. emits('currentWeizhi', item.location);
  173. }
  174. const data = reactive({
  175. keyword: '', // 关键字
  176. id: 0, // 使用 marker点击事件 需要填写id
  177. title: 'map',
  178. latitude: 0,
  179. longitude: 0,
  180. covers: [{
  181. latitude: 0,
  182. longitude: 0,
  183. iconPath: myIcon,
  184. width: 30,
  185. height: 30
  186. }]
  187. })
  188. // 初始化
  189. function initLocation() {
  190. console.log('init')
  191. uni.getLocation({
  192. type: 'gcj02',
  193. success: function(res) {
  194. console.log('res', res)
  195. data.longitude = res.longitude;
  196. data.latitude = res.latitude;
  197. data.covers[0].longitude = res.longitude;
  198. data.covers[0].latitude = res.latitude;
  199. console.log('当前位置的经度:' + res.longitude);
  200. console.log('当前位置的纬度:' + res.latitude);
  201. // 获取当前位置附近的数据
  202. getPositionSearchByKeyword('公司', `nearby(${res.latitude},${res.longitude},1000)`, onSuccess,
  203. onError, {
  204. page: 1,
  205. size: 3,
  206. });
  207. }
  208. });
  209. }
  210. onLoad(() => {
  211. console.log('初始化')
  212. initLocation();
  213. })
  214. </script>
  215. <style>
  216. </style>