customMap.vue 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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}}</view>
  20. <view class="map-card-address">{{item.address}}</view>
  21. </view>
  22. <icon class="map-active-icon" v-if="index==0"></icon>
  23. </view>
  24. </view>
  25. </view>
  26. </template>
  27. <script setup>
  28. import {
  29. reactive,
  30. ref
  31. } from "vue"
  32. import myIcon from "@/static/images/common/markIcon.png"
  33. import {
  34. onLoad,
  35. onHide,
  36. onShow
  37. } from "@dcloudio/uni-app"
  38. import {
  39. useCustomMap
  40. } from "@/components/customMap/useMap.js"
  41. const emits = defineEmits(['close', 'xuanzeAdress'])
  42. const {
  43. getPositionSearchByKeyword
  44. } = useCustomMap();
  45. const result = ref(null);
  46. function handleClick() {
  47. if (data.keyword) {
  48. getPositionSearchByKeyword(data.keyword, `nearby(${data.latitude},${data.longitude},1000)`, onSuccess,
  49. onError, {
  50. page: 1,
  51. size: 3,
  52. })
  53. }
  54. }
  55. function onSuccess(data) {
  56. console.log('数据', data);
  57. result.value = data
  58. }
  59. function onError(err) {
  60. console.log('错误', err)
  61. }
  62. function goUpPage(err) {
  63. console.log('错误', err)
  64. emits('close');
  65. }
  66. function xuanzeAdress(item) {
  67. console.log('item');
  68. emits('xuanzeAdress',item.address);
  69. }
  70. const data = reactive({
  71. keyword: '', // 关键字
  72. id: 0, // 使用 marker点击事件 需要填写id
  73. title: 'map',
  74. latitude: 0,
  75. longitude: 0,
  76. covers: [{
  77. latitude: 0,
  78. longitude: 0,
  79. iconPath: myIcon,
  80. width: 30,
  81. height: 30
  82. }]
  83. })
  84. // 初始化
  85. function initLocation() {
  86. console.log('init')
  87. uni.getLocation({
  88. type: 'gcj02',
  89. success: function(res) {
  90. console.log('res', res)
  91. data.longitude = res.longitude;
  92. data.latitude = res.latitude;
  93. data.covers[0].longitude = res.longitude;
  94. data.covers[0].latitude = res.latitude;
  95. console.log('当前位置的经度:' + res.longitude);
  96. console.log('当前位置的纬度:' + res.latitude);
  97. }
  98. });
  99. }
  100. onLoad(() => {
  101. console.log('初始化')
  102. initLocation();
  103. })
  104. </script>
  105. <style>
  106. </style>