customMap.vue 6.8 KB

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