customMap.vue 6.1 KB

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