MapContainer.vue 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <template>
  2. <div id="container"></div>
  3. </template>
  4. <script setup>
  5. import { onMounted, onUnmounted, ref, defineEmits } from "vue";
  6. import AMapLoader from "@amap/amap-jsapi-loader";
  7. let map = null;
  8. let position = ref();
  9. let props = defineProps({
  10. position: {
  11. type: Object,
  12. default: () => ({})
  13. },
  14. radius: {
  15. type: Number,
  16. default: () => (50)
  17. },
  18. address: {
  19. type: String,
  20. default: () => ('打卡点')
  21. }
  22. });
  23. const emit = defineEmits(['update','err']);
  24. onMounted(() => {
  25. const loading = ElLoading.service({
  26. lock: true,
  27. text: '正在定位中,请稍候'
  28. })
  29. AMapLoader.load({
  30. key: "d1f123693def8a412c976184daa4b60e",
  31. version: "2.0",
  32. })
  33. .then((AMap) => {
  34. map = new AMap.Map("container", {
  35. viewMode: '2D', //地图模式
  36. zoom: 18, //初始化地图层级
  37. center: props.position,
  38. });
  39. AMap.plugin(["AMap.ToolBar", "AMap.Scale", "AMap.Geolocation", "AMap.CircleEditor", "AMap.Geocoder"], function () {
  40. var geolocation = new AMap.Geolocation({
  41. enableHighAccuracy: true,
  42. timeout: 10000,
  43. buttonPosition: 'RB', //定位按钮的停靠位置
  44. buttonOffset: new AMap.Pixel(10, 20),//定位按钮与设置的停靠位置的偏移量,默认:Pixel(10, 20)
  45. zoomToAccuracy: true,
  46. useNative: true,
  47. GeoLocationFirst:true
  48. });
  49. map.addControl(geolocation);
  50. //绘制签到范围
  51. var circle = new AMap.Circle({
  52. center: props.position,
  53. radius: props.radius, //签到范围半径
  54. borderWeight: 1,
  55. strokeOpacity: 1,
  56. strokeOpacity: 0.2,
  57. fillOpacity: 0.4,
  58. })
  59. circle.setMap(map)
  60. // 缩放地图到合适的视野级别
  61. map.setFitView([circle])
  62. new AMap.CircleEditor(map, circle)
  63. const window = `<small>打卡点</small>`;
  64. const infoWindow = new AMap.InfoWindow({
  65. content: window,
  66. anchor: "top-left",
  67. });
  68. infoWindow.open(map, props.position);
  69. //临时定位方式 后续换为精度更高的企业微信定位
  70. geolocation.getCurrentPosition(function (status, result) {
  71. if (status == 'complete') {
  72. position.value = result.position;
  73. var distance = AMap.GeometryUtil.distance(result.position, props.position).toFixed(2);
  74. emit('update', distance);
  75. loading.close();
  76. } else {
  77. onError(result);
  78. loading.close();
  79. }
  80. })
  81. let toolbar = new AMap.ToolBar();
  82. map.addControl(toolbar);
  83. let scale = new AMap.Scale();
  84. map.addControl(scale);
  85. });
  86. const marker = new AMap.Marker({
  87. position: props.position,
  88. title: "打卡点",
  89. });
  90. map.add(marker);
  91. })
  92. .catch((e) => {
  93. console.log(e);
  94. });
  95. });
  96. //解析定位错误信息
  97. function onError(data) {
  98. alert(data.message);
  99. }
  100. onUnmounted(() => {
  101. map?.destroy();
  102. });
  103. </script>
  104. <style scoped>
  105. #container {
  106. width: 100%;
  107. height: 450px;
  108. }
  109. @media only screen and (max-width: 768px) {
  110. #container {
  111. height: 400px;
  112. }
  113. }
  114. </style>