customMap.vue 6.7 KB

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