customMap.vue 7.4 KB

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