customMap.vue 2.6 KB

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