customMap.vue 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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`;
  98. const url2 = `https://apis.map.qq.com/ws/geocoder/v1/?address=${city}&key=${Key}`;
  99. // #ifdef H5
  100. jsonp(url, {
  101. address: `${city}`,
  102. key: Key,
  103. output: 'jsonp'
  104. }, 'ccc1').then(res => {
  105. console.log('res1222222', res);
  106. if (res.status === 0) {
  107. const cityLocation = res.result.location;
  108. console.log('cityLocation', cityLocation);
  109. if (cityLocation) {
  110. data.latitude = cityLocation.lat;
  111. data.longitude = cityLocation.lng;
  112. data.covers[0].latitude = cityLocation.lat;
  113. data.covers[0].longitude = cityLocation.lng;
  114. // 重新搜索附近的地点
  115. getPositionSearchByKeyword(data.keyword || '公司',
  116. `nearby(${cityLocation.lat},${cityLocation.lng},1000)`,
  117. onSuccess, onError, {
  118. page: 1,
  119. size: 3,
  120. });
  121. }
  122. } else {
  123. console.error('失败:', res);
  124. }
  125. }).catch(err => {
  126. console.log('err', err);
  127. }).finally(() => {
  128. })
  129. // #endif
  130. // #ifdef APP
  131. uni.request({
  132. url: url2,
  133. method: 'get',
  134. timeout: 20000,
  135. }).then(res => {
  136. console.log('res', res);
  137. if (res.data && res.data.result && res.data.result.location) {
  138. const cityLocation = res.data.result.location;
  139. console.log('cityLocation', cityLocation);
  140. if (cityLocation) {
  141. data.latitude = cityLocation.lat;
  142. data.longitude = cityLocation.lng;
  143. data.covers[0].latitude = cityLocation.lat;
  144. data.covers[0].longitude = cityLocation.lng;
  145. // 重新搜索附近的地点
  146. getPositionSearchByKeyword(data.keyword || '公司',
  147. `nearby(${cityLocation.lat},${cityLocation.lng},1000)`,
  148. onSuccess, onError, {
  149. page: 1,
  150. size: 3,
  151. });
  152. }
  153. }
  154. }).catch(err => {
  155. console.log('err')
  156. }).finally(() => {
  157. })
  158. // #endif
  159. }
  160. function handleClick() {
  161. if (data.keyword) {
  162. getPositionSearchByKeyword(data.keyword, `nearby(${data.latitude},${data.longitude},1000)`, onSuccess,
  163. onError, {
  164. page: 1,
  165. size: 3,
  166. })
  167. }
  168. }
  169. function onSuccess(res) {
  170. console.log('数据', res);
  171. result.value = res
  172. // #ifdef H5
  173. if (res && res.data && res.data.length > 0) {
  174. // 获取第一个搜索结果的经纬度
  175. const firstResult = res.data[0];
  176. const newLatitude = firstResult.location.lat; // 纬度
  177. const newLongitude = firstResult.location.lng; // 经度
  178. // 更新地图的中心点
  179. data.latitude = newLatitude;
  180. data.longitude = newLongitude;
  181. // 更新标记点
  182. data.covers = [{
  183. latitude: newLatitude,
  184. longitude: newLongitude,
  185. iconPath: myIcon, // 使用你的图标路径
  186. width: 30,
  187. height: 30
  188. }];
  189. }
  190. // #endif
  191. // #ifdef APP
  192. if (res.data && res.data.data && res.data.data.length > 0) {
  193. // 获取第一个搜索结果的经纬度
  194. const firstResult = res.data.data[0];
  195. const newLatitude = firstResult.location.lat; // 纬度
  196. const newLongitude = firstResult.location.lng; // 经度
  197. // 更新地图的中心点
  198. data.latitude = newLatitude;
  199. data.longitude = newLongitude;
  200. // 更新标记点
  201. data.covers = [{
  202. latitude: newLatitude,
  203. longitude: newLongitude,
  204. iconPath: myIcon, // 使用你的图标路径
  205. width: 30,
  206. height: 30
  207. }];
  208. }
  209. // #endif
  210. }
  211. function onError(err) {
  212. console.log('错误', err)
  213. }
  214. function goUpPage(err) {
  215. console.log('错误', err)
  216. emits('close');
  217. }
  218. function xuanzeAdress(item) {
  219. console.log('item', item);
  220. emits('xuanzeAdress', item.address);
  221. emits('currentWeizhi', item.location);
  222. }
  223. const data = reactive({
  224. keyword: '', // 关键字
  225. id: 0, // 使用 marker点击事件 需要填写id
  226. title: 'map',
  227. latitude: 0,
  228. longitude: 0,
  229. covers: [{
  230. latitude: 0,
  231. longitude: 0,
  232. iconPath: myIcon,
  233. width: 30,
  234. height: 30
  235. }]
  236. })
  237. function getCityName(latitude, longitude) {
  238. const Key = `KBYBZ-FS3KZ-U2FXR-7VPDX-L7J46-23F55`
  239. const url1 = `https://apis.map.qq.com/ws/geocoder/v1/?location=${latitude},${longitude}&key=${Key}`;
  240. const url = `https://apis.map.qq.com/ws/geocoder/v1`;
  241. //const url = `https://apis.map.qq.com/ws/geocoder/v1/`;
  242. // #ifdef H5
  243. jsonp(url, {
  244. location: `${latitude},${longitude}`,
  245. key: Key,
  246. output: 'jsonp'
  247. }, 'ccc').then(res => {
  248. console.log('res11111', res);
  249. if (res.status === 0) {
  250. const city = res.result.address_component.city;
  251. currentCity.value = city; // 更新当前城市
  252. console.log('当前城市:', city);
  253. } else {
  254. console.error('逆地理编码失败:', res);
  255. }
  256. }).catch(err => {
  257. console.log('err', err);
  258. }).finally(() => {
  259. })
  260. // #endif
  261. // #ifdef APP
  262. uni.request({
  263. url: url1,
  264. success: (res) => {
  265. if (res.data.status === 0) {
  266. console.log('当前城市:', res);
  267. const city = res.data.result.address_component.city;
  268. currentCity.value = city; // 更新当前城市
  269. console.log('当前城市:', city);
  270. } else {
  271. console.error('逆地理编码失败:', res.data.message);
  272. uni.showToast({
  273. title: '获取城市信息失败',
  274. icon: 'none',
  275. });
  276. }
  277. },
  278. fail: (err) => {
  279. console.error('请求失败:', err);
  280. uni.showToast({
  281. title: '请求失败',
  282. icon: 'none',
  283. });
  284. }
  285. });
  286. // #endif
  287. }
  288. // 初始化
  289. function initLocation() {
  290. console.log('init')
  291. uni.getLocation({
  292. type: 'gcj02',
  293. success: function(res) {
  294. console.log('res', res)
  295. data.longitude = res.longitude;
  296. data.latitude = res.latitude;
  297. data.covers[0].longitude = res.longitude;
  298. data.covers[0].latitude = res.latitude;
  299. console.log('当前位置的经度:' + res.longitude);
  300. console.log('当前位置的纬度:' + res.latitude);
  301. getCityName(res.latitude, res.longitude)
  302. // 获取当前位置附近的数据
  303. getPositionSearchByKeyword('公司', `nearby(${res.latitude},${res.longitude},1000)`, onSuccess,
  304. onError, {
  305. page: 1,
  306. size: 3,
  307. });
  308. }
  309. });
  310. }
  311. onLoad(() => {
  312. console.log('初始化')
  313. initLocation();
  314. })
  315. </script>
  316. <style>
  317. </style>