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