customMap.vue 7.6 KB

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