customMap.vue 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. <!-- 查询职业 -->
  8. <view class="phone-search-box">
  9. <input class="search-input" placeholder="请输入地址" v-model="data.keyword" />
  10. <view class="search-icon" @click="handleClick">
  11. <uni-icons type="search" size="24" color="#fff"></uni-icons>
  12. </view>
  13. </view>
  14. <map style="width: 100%; height: 300px;" :latitude="data.latitude" :longitude="data.longitude"
  15. :markers="data.covers" :show-location="true"> </map>
  16. <view v-if="result" class="map-card-list">
  17. <view v-for="(item,index) in result.data.data" key="index" @click="xuanzeAdress(item)" class="map-card-box">
  18. <view>
  19. <view class="map-card-title"><text v-if="index==0" class="dqwz-text">当前位置</text>{{item.title}}
  20. </view>
  21. <view class="map-card-address">{{item.address}}</view>
  22. </view>
  23. <icon class="map-active-icon" v-if="index==0"></icon>
  24. </view>
  25. </view>
  26. </view>
  27. </template>
  28. <script setup>
  29. import {
  30. reactive,
  31. ref
  32. } from "vue"
  33. import myIcon from "@/static/images/common/markIcon.png"
  34. import {
  35. onLoad,
  36. onHide,
  37. onShow
  38. } from "@dcloudio/uni-app"
  39. import {
  40. useCustomMap
  41. } from "@/components/customMap/useMap.js"
  42. const emits = defineEmits(['close', 'xuanzeAdress'])
  43. const {
  44. getPositionSearchByKeyword
  45. } = useCustomMap();
  46. const result = ref(null);
  47. function handleClick() {
  48. if (data.keyword) {
  49. getPositionSearchByKeyword(data.keyword, `nearby(${data.latitude},${data.longitude},1000)`, onSuccess,
  50. onError, {
  51. page: 1,
  52. size: 3,
  53. })
  54. }
  55. }
  56. function onSuccess(res) {
  57. console.log('数据', res);
  58. result.value = res
  59. if (res.data && res.data.data && res.data.data.length > 0) {
  60. // 获取第一个搜索结果的经纬度
  61. const firstResult = res.data.data[0];
  62. const newLatitude = firstResult.location.lat; // 纬度
  63. const newLongitude = firstResult.location.lng; // 经度
  64. // 更新地图的中心点
  65. data.latitude = newLatitude;
  66. data.longitude = newLongitude;
  67. // 更新标记点
  68. data.covers = [{
  69. latitude: newLatitude,
  70. longitude: newLongitude,
  71. iconPath: myIcon, // 使用你的图标路径
  72. width: 30,
  73. height: 30
  74. }];
  75. }
  76. }
  77. function onError(err) {
  78. console.log('错误', err)
  79. }
  80. function goUpPage(err) {
  81. console.log('错误', err)
  82. emits('close');
  83. }
  84. function xuanzeAdress(item) {
  85. console.log('item');
  86. emits('xuanzeAdress', item.address);
  87. }
  88. const data = reactive({
  89. keyword: '', // 关键字
  90. id: 0, // 使用 marker点击事件 需要填写id
  91. title: 'map',
  92. latitude: 0,
  93. longitude: 0,
  94. covers: [{
  95. latitude: 0,
  96. longitude: 0,
  97. iconPath: myIcon,
  98. width: 30,
  99. height: 30
  100. }]
  101. })
  102. // 初始化
  103. function initLocation() {
  104. console.log('init')
  105. uni.getLocation({
  106. type: 'gcj02',
  107. success: function(res) {
  108. console.log('res', res)
  109. data.longitude = res.longitude;
  110. data.latitude = res.latitude;
  111. data.covers[0].longitude = res.longitude;
  112. data.covers[0].latitude = res.latitude;
  113. console.log('当前位置的经度:' + res.longitude);
  114. console.log('当前位置的纬度:' + res.latitude);
  115. // 获取当前位置附近的数据
  116. getPositionSearchByKeyword('公司', `nearby(${res.latitude},${res.longitude},1000)`, onSuccess,
  117. onError, {
  118. page: 1,
  119. size: 3,
  120. });
  121. }
  122. });
  123. }
  124. onLoad(() => {
  125. console.log('初始化')
  126. initLocation();
  127. })
  128. </script>
  129. <style>
  130. </style>