customMap.vue 7.8 KB

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