customMap.vue 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <template>
  2. <view>
  3. <map style="width: 100%; height: 300px;" :latitude="data.latitude" :longitude="data.longitude"
  4. :markers="data.covers" :show-location="true"> </map>
  5. <input type="text" v-model="data.keyword" placeholder="请输入地址" style="padding: 10px 0">
  6. <view>{{result && result.data.data}}</view>
  7. <button @click="handleClick">查询</button>
  8. </view>
  9. </template>
  10. <script setup>
  11. import {
  12. reactive,ref
  13. } from "vue"
  14. import myIcon from "@/static/images/common/markIcon.png"
  15. import {
  16. onLoad, onHide, onShow
  17. } from "@dcloudio/uni-app"
  18. import {useCustomMap} from "@/components/customMap/useMap.js"
  19. const { getPositionSearchByKeyword } = useCustomMap();
  20. const result = ref(null);
  21. function handleClick() {
  22. if (data.keyword) {
  23. getPositionSearchByKeyword(data.keyword, `nearby(${data.latitude},${data.longitude},1000)`, onSuccess, onError, {page:1, size:3,})
  24. }
  25. }
  26. function onSuccess(data) {
  27. console.log('数据', data);
  28. result.value = data
  29. }
  30. function onError(err) {
  31. console.log('错误', err)
  32. }
  33. const data = reactive({
  34. keyword: '', // 关键字
  35. id: 0, // 使用 marker点击事件 需要填写id
  36. title: 'map',
  37. latitude: 0,
  38. longitude: 0,
  39. covers: [{
  40. latitude: 0,
  41. longitude: 0,
  42. iconPath: myIcon,
  43. width: 30,
  44. height: 30
  45. }]
  46. })
  47. // 初始化
  48. function initLocation() {
  49. console.log('init')
  50. uni.getLocation({
  51. type: 'gcj02',
  52. success: function(res) {
  53. console.log('res', res)
  54. data.longitude = res.longitude;
  55. data.latitude = res.latitude;
  56. data.covers[0].longitude = res.longitude;
  57. data.covers[0].latitude = res.latitude;
  58. console.log('当前位置的经度:' + res.longitude);
  59. console.log('当前位置的纬度:' + res.latitude);
  60. }
  61. });
  62. }
  63. onLoad(() => {
  64. console.log('初始化')
  65. initLocation();
  66. })
  67. </script>
  68. <style>
  69. </style>