index.vue 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <template>
  2. <div id="container"></div>
  3. </template>
  4. <script>
  5. import AMapLoader from "@amap/amap-jsapi-loader";
  6. import markPNG from "../../assets/icons/mark.png"
  7. export default {
  8. name: "map-view",
  9. mounted() {
  10. //this.initAMap();
  11. },
  12. unmounted() {
  13. this.map?.destroy();
  14. },
  15. props: {
  16. targetPoint: {
  17. type: Array,
  18. Required: false
  19. }
  20. },
  21. watch:{
  22. targetPoint:{
  23. handler(nv){
  24. this.map?.destroy();
  25. this.initAMap();
  26. },
  27. immediate:true
  28. }
  29. },
  30. created() {
  31. },
  32. methods: {
  33. initAMap() {
  34. window._AMapSecurityConfig = {
  35. securityJsCode: "522c082a4dfbb2b5555c7b1d3ff453b8",
  36. };
  37. AMapLoader.load({
  38. key: "78291dcbd623bd91a9d00754bab6c29b", // 申请好的Web端开发者Key,首次调用 load 时必填
  39. version: "2.0", // 指定要加载的 JSAPI 的版本,缺省时默认为 1.4.15
  40. plugins: ["AMap.Scale", "AMap.ToolBar", "AMap.MapType", "AMap.ElasticMarker"], //需要使用的的插件列表,如比例尺'AMap.Scale',支持添加多个如:['...','...']
  41. }).then((AMap) => {
  42. var zoomStyleMapping = {
  43. 14: 0,
  44. 15: 0,
  45. 16: 0,
  46. 17: 0,
  47. 18: 0,
  48. 19: 0,
  49. 20: 0,
  50. };
  51. this.map = new AMap.Map("container", {
  52. // 设置地图容器id
  53. viewMode: "3D", // 是否为3D地图模式
  54. zoom: 15, // 初始化地图级别
  55. center: this.targetPoint[0].lngLat.split(",") // 初始化地图中心点位置
  56. });
  57. var toolbar = new AMap.ToolBar(); //创建工具条插件实例
  58. this.map.addControl(toolbar); //添加工具条插件到页面
  59. var scale = new AMap.Scale();
  60. this.map.addControl(scale);
  61. var mapType = new AMap.MapType();
  62. this.map.addControl(mapType);
  63. for (let i in this.targetPoint) {
  64. var stylesArray = [
  65. {
  66. icon: { //图标样式
  67. img: markPNG,
  68. size: [50, 50], //图标的原始大小
  69. anchor: "bottom-center", //锚点位置
  70. fitZoom: 0, //最合适的级别 在此级别显示为图标原始大小
  71. scaleFactor: 1, //地图放大一级的缩放比例系数
  72. maxScale: 1, //图片的最大放大比例,随着地图放大图标会跟着放大,最大为2
  73. minScale: 1, //图片的最小缩小比例,随着地图缩小图标会跟着缩小,最小为1
  74. },
  75. label: { //文本标注
  76. content: this.targetPoint[i].location, //文本内容
  77. position: "BM", //文本位置相对于图标的基准点,"BM"为底部中央
  78. minZoom: 0, //label的最小显示级别,即文本标注在地图15级及以上,才会显示
  79. },
  80. },
  81. ];
  82. //设置圆形位置
  83. var center = new AMap.LngLat(this.targetPoint[i].lngLat.split(",")[0],this.targetPoint[i].lngLat.split(",")[1]);
  84. //设置圆的半径大小
  85. var radius = 1000;
  86. //创建圆形 Circle 实例
  87. var circle = new AMap.Circle({
  88. center: center, //圆心
  89. radius: radius, //半径
  90. borderWeight: 1, //描边的宽度
  91. strokeColor: "#ff6154", //轮廓线颜色
  92. strokeOpacity: 0.5, //轮廓线透明度
  93. strokeWeight: 1, //轮廓线宽度
  94. fillOpacity: 0.2, //圆形填充透明度
  95. strokeStyle: "dashed", //轮廓线样式
  96. fillColor: "#ff6154", //圆形填充颜色
  97. zIndex: 50, //圆形的叠加顺序
  98. });
  99. var elasticMarker = new AMap.ElasticMarker({
  100. position: this.targetPoint[i].lngLat.split(","), //点标记位置
  101. styles: stylesArray, //指定样式列表
  102. zoomStyleMapping: zoomStyleMapping, //指定 zoom 与样式的映射
  103. clickable:true
  104. })
  105. this.map.add(elasticMarker); //添加到地图上
  106. this.map.add(circle);
  107. this.map.setFitView(); //缩放地图到合适的视野级别
  108. //鼠标移入事件
  109. // circle.on("click", function () {
  110. // console.log("鼠标点击");
  111. // });
  112. }
  113. })
  114. .catch((e) => {
  115. console.log(e);
  116. });
  117. },
  118. collectInfo(){
  119. }
  120. },
  121. };
  122. </script>
  123. <style scoped>
  124. #container {
  125. width: 100%;
  126. height: 600px;
  127. }
  128. .custom-content-marker {
  129. position: relative;
  130. width: 25px;
  131. height: 34px;
  132. }
  133. </style>