当前位置:   article > 正文

vue + vue-amap + echarts-amap + echarts +高德地图开发在线省市地图并标记点._echart amap

echart amap

最终效果图:
在这里插入图片描述
首先需要引入:

1:在index.html中引入:

   <script type="text/javascript">
    window._AMapSecurityConfig = {
      securityJsCode: '自己去高德地图申请',
    }
  </script>
  <script src="https://webapi.amap.com/maps?key=高德地图官网申请key&v=1.4.15"></script>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

key需要自己去高德地图官网申请.

2.在画图的vue页面引入:

import * as echarts from "echarts";
import VueAMap from "vue-amap"; //高德地图插件
import "echarts-amap";
  • 1
  • 2
  • 3

3.开始画地图

 init() {
      let _this = this
      AMap.plugin(
        ['AMap.DistrictSearch', 'AMap.MapType', 'AMap.CustomLayer'],
        function () {
          var district = new AMap.DistrictSearch({
            extensions: 'all',
            level: 'province', //省市县对应的不同,根据自己的需求去改
          })
          district.search('河北省', function (status, result) { //河北省改成你需要的省份
            var bounds = result.districtList[0].boundaries
            var districts = result.districtList[0].districtList

            var mask = []
            for (var i = 0; i < bounds.length; i++) {
              mask.push([bounds[i]])
            }
            var myChart = echarts.init(document.getElementById('mapData')) //初始化容器

            var option = {
              animation: false,
              amap: {
                mask: mask,
                center: [116.397428, 39.90923],
                viewMode: '3D',
                zoom: 6.7,
                defaultCursor: 'auto',
              },
              series: _this.seriesData,
            }
            myChart.setOption(option)
            var map = myChart.getModel().getComponent('amap').getAMap()
            var layer = myChart.getModel().getComponent('amap').getLayer()
            var mapType = new AMap.MapType({
              defaultType: 1,
            })
            // console.log(JSON.stringify(mapType));
  
            map.addControl(mapType)
            mapType.hide()

            var LabelsData = []

            for (var i = 0; i < districts.length; i++) {
              var config = {
                name: '',
                // position: [116.12, 39.11],
                zooms: [4, 13],
                zIndex: 1,
                opacity: 1,
                text: {
                  content: '',
                  direction: 'center',
                  offset: [0, 0],
                  zooms: [3, 20],
                  style: {
                    fontSize: 12,
                    fontWeight: 'normal',
                    fillColor: '#fff',
                    strokeColor: '#fff',
                    strokeWidth: 1,
                  },
                },
              }

              var district = districts[i]
              var name = district.name

              config.text.content = name
              config.position = [district.center.lng, district.center.lat]

              LabelsData.push(config)

              var district2 = new AMap.DistrictSearch({
                extensions: 'all',
                level: 'city',
              })
             

              district2.search(name, function (status, result) {
                var bounds = result.districtList[0].boundaries
     
                for (var ii = 0; ii < bounds.length; ii++) {
                  new AMap.Polyline({
                    map: map,
                    path: bounds[ii],
                    strokeColor: '#E7D8BC',
                    strokeWeight: 2,
                  })
                }
              })
            }

            for (var i = 0; i < bounds.length; i++) {
              new AMap.Polyline({
                map: map,
                path: bounds[i],
                strokeColor: '#E7D8BC',
                strokeWeight: 6,
              })
            }

            map.on('complete', function () {
              var layer = new AMap.LabelsLayer({
                collision: false,
                animation: true,
              })

              for (var i = 0; i < LabelsData.length; i++) {
                var labelsMarker = new AMap.LabelMarker(LabelsData[i])
                layer.add(labelsMarker)
              }
              map.add(layer)
              document.getElementsByClassName(
                'amap-container'
              )[0].style.background = 'none'
            })
          })
        }
      )
    },
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121

4.完整代码

<!--  -->
<template>
  <div class="">
    <div class="amapBox">
      <div id="mapData" ref="mapData" style="width: 96%; height: 720px"></div>
    </div>
  </div>
</template>

<script>
import * as echarts from "echarts";

import VueAMap from "vue-amap"; //高德地图插件
import "echarts-amap";
import imgIcon1 from "@/assets/login/1.png";
import imgIcon2 from "@/assets/login/19.png";
import imgIcon3 from "@/assets/login/1.png";
import imgIcon4 from "@/assets/login/19.png";
// import AMap from 'AMap'
// import {findFcxxListByCompanyCodeAndFdlx} from '@/api/dashboard/dashboardApi'
// let AMap = Amap;
export default {
  props: {
    mapDataList: {
      type: Array,
      default: () => {
        return [];
      },
    },
  },
  data(vm) {
    return {
      mapData: null,
      goals: [
        {
          name: "风电场1",
          images: imgIcon1,
          value: [113.775669, 38.709836],
        },
        {
          name: "风电场2",
          images: imgIcon1,
          value: [114.775669, 40.709836],
        },
        {
          name: "光伏1",
          images: imgIcon2,
          value: [115.775669, 41.709836],
        },
        {
          name: "光伏2",
          symbol: "image://" + imgIcon2,

          value: [114.775669, 39.709836],
        },
      ],
      goalsTow: [],
      goalsThree: [],
      goalsfour: [],
      seriesData: [],
    };
  },
  watch: {},
  //方法集合
  methods: {
    initData() {
      console.log(VueAMap);
      this.seriesData = [
        {
          name: "点",
          type: "scatter",
          coordinateSystem: "amap",
          zlevel: 9999,

          rippleEffect: {
            brushType: "stroke",
          },
          hoverAnimation: true,
          label: {
            normal: {
                show: true,
                position: 'right',
                formatter: '{b}',
                color:'#fff',
                padding:[10,20,10,20],
                backgroundColor:'rgba(0,0,0,0.5)'
              },
            
          },
          itemStyle: {
            normal: {
              color: "#4892C2",
              shadowBlur: 12,
              shadowColor: "#fff",
            },
          },
          showAllSymbol: true,
          symbolSize: "20",
          symbol: `image://${imgIcon1}`,
          symbolRotate: 35,
          data: this.goals,
        },
        {
          name: "点",
          type: "scatter",
          coordinateSystem: "amap",
          zlevel: 9999,
          rippleEffect: {
            brushType: "stroke",
          },
          hoverAnimation: true,
          label: {
            show: false,
          },
          itemStyle: {
            normal: {
              color: "#4892C2",
              shadowBlur: 12,
              shadowColor: "#fff",
            },
          },
          showAllSymbol: true,
          symbolSize: 26,
          symbol: `image://${imgIcon2}`,
          symbolRotate: 35,
          data: this.goalsTow,
        },
        {
          name: "点",
          type: "scatter",
          coordinateSystem: "amap",
          zlevel: 9999,
          rippleEffect: {
            brushType: "stroke",
          },
          hoverAnimation: true,
          label: {
            show: false,
          },
          itemStyle: {
            normal: {
              color: "#4892C2",
              shadowBlur: 12,
              shadowColor: "#fff",
            },
          },
          showAllSymbol: true,
          symbolSize: 26,
          symbol: `image://${imgIcon3}`,
          symbolRotate: 35,
          data: this.goalsThree,
        },
        {
          name: "点",
          type: "scatter",
          coordinateSystem: "amap",
          zlevel: 9999,
          rippleEffect: {
            brushType: "stroke",
          },
          hoverAnimation: true,
          label: {
            show: false,
          },
          itemStyle: {
            normal: {
              color: "#4892C2",
              shadowBlur: 12,
              shadowColor: "#fff",
            },
          },
          showAllSymbol: true,
          symbolSize: 26,
          symbol: `image://${imgIcon4}`,
          symbolRotate: 35,
          data: this.goalsfour,
        },
      ];
      this.init();
    },
    dataReturnFn(v) {
      let that = this;
      let obj = {
        name: v.name,
        value: v.gps,
        label: {
          normal: {
            show: true,
            formatter: function (params) {
              return "{div|" + params.name + "}";
            },
            padding: [0, 0, 0, -120], //内边距属性

            rich: {
              div: {
                color: "#fff",

                padding: [5, 10, 5, 10],
                backgroundColor: "rgba(0,0,0,0.9)",
              },
            },
          },
        },
      };
      return {
        type1: () => {
          return that.goals.push(obj);
        },
        type2: () => {
          return that.goalsTow.push(obj);
        },
        type3: () => {
          return that.goalsThree.push(obj);
        },
        type4: () => {
          return that.goalsfour.push(obj);
        },
      };
    },
    handlerMapData(v, i) {
      this.dataReturnFn(v)["type" + i]();
    },
    init() {
      let _this = this;
      AMap.plugin(
        ["AMap.DistrictSearch", "AMap.MapType", "AMap.CustomLayer"],
        function () {
          var district = new AMap.DistrictSearch({
            extensions: "all",
            level: "province",
          });
          district.search("河北省", function (status, result) {
            var bounds = result.districtList[0].boundaries;
            var districts = result.districtList[0].districtList;

            var mask = [];
            for (var i = 0; i < bounds.length; i++) {
              mask.push([bounds[i]]);
            }
            var myChart = echarts.init(document.getElementById("mapData"));

            console.log(mask);

            console.log(districts);
            var option = {
              animation: false,
              amap: {
                mask: mask,
                center: [116.397428, 39.90923],
                viewMode: "3D",
                zoom: 6.7,
                defaultCursor: "auto",
              },
              series: _this.seriesData,
            };
            myChart.setOption(option);
            var map = myChart.getModel().getComponent("amap").getAMap();
            var layer = myChart.getModel().getComponent("amap").getLayer();
            console.log(layer);
            var mapType = new AMap.MapType({
              defaultType: 1,
            });
            // console.log(JSON.stringify(mapType));

            map.addControl(mapType);
            mapType.hide();

            var LabelsData = [];

            for (let i = 0; i < districts.length; i++) {
              var config = {
                name: "",
                zooms: [4, 13],
                zIndex: 1,
                opacity: 1,
                text: {
                  content: "",
                  direction: "center",
                  offset: [0, 0],
                  zooms: [3, 20],
                  style: {
                    fontSize: 12,
                    fontWeight: "normal",
                    fillColor: "#fff",
                    strokeColor: "#fff",
                    strokeWidth: 1,
                  },
                },
              };

              var district = districts[i];
              var name = district.name;

              config.text.content = name;
              config.position = [district.center.lng, district.center.lat];

              LabelsData.push(config);

              var district2 = new AMap.DistrictSearch({
                extensions: "all",
                level: "city",
              });

              district2.search(name, function (status, result) {
                var bounds = result.districtList[0].boundaries;

                for (var ii = 0; ii < bounds.length; ii++) {
                  new AMap.Polyline({
                    map: map,
                    path: bounds[ii],
                    strokeColor: "#E7D8BC",
                    strokeWeight: 2,
                  });
                }
              });
            }

            for (let i = 0; i < bounds.length; i++) {
              new AMap.Polyline({
                map: map,
                path: bounds[i],
                strokeColor: "#E7D8BC",
                strokeWeight: 6,
              });
            }

            map.on("complete", function () {
              var layer = new AMap.LabelsLayer({
                collision: false,
                animation: true,
              });

              for (var i = 0; i < LabelsData.length; i++) {
                var labelsMarker = new AMap.LabelMarker(LabelsData[i]);
                layer.add(labelsMarker);
              }
              map.add(layer);
              document.getElementsByClassName(
                "amap-container"
              )[0].style.background = "none";
            });
          });
        }
      );
    },
  },

  //生命周期 - 挂载完成(可以访问DOM元素)
  mounted() {
    this.initData();

    // const mapContainer = this.$refs.mapData;
    // console.log(mapContainer);
    // const map = new Map({
    //   layers: mapconfig.streetmap(), //在mapconfig.js封装的
    //   // controls: [FullScreen, ScaleLine, Zoom],
    //   target: mapContainer,
    //   view: new View({
    //     projection: "EPSG:4326",
    //     center: [116.397428, 39.90923],
    //     zoom: 11,
    //   }),
    // });
    //  // 添加鼠标点击事件
    // map.on("click", this.mapClick);
    // // 添加鼠标经过事件
    // map.on("pointermove", this.mapPointerMove);
    // // 保存地图
    // this.mapData = map;
  },
};
</script>
<style scoped>
.amapBox {
  display: flex;
  justify-content: center;
  height: 500px;
  width: 100%;
  position: relative;
  top: -40px;
}
::v-deep .amap-logo {
  display: none !important;
}
::v-deep .amap-copyright {
  display: none !important;
}

.amap-marker-label {
  -webkit-border-radius: 10px;
  -moz-border-radius: 10px;
  border-radius: 10px;
  border: none;
  -webkit-box-shadow: 1px 2px 4px #cccccc;
  -moz-box-shadow: 1px 2px 4px #cccccc;
  box-shadow: 1px 2px 4px #cccccc;
}
</style>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 289
  • 290
  • 291
  • 292
  • 293
  • 294
  • 295
  • 296
  • 297
  • 298
  • 299
  • 300
  • 301
  • 302
  • 303
  • 304
  • 305
  • 306
  • 307
  • 308
  • 309
  • 310
  • 311
  • 312
  • 313
  • 314
  • 315
  • 316
  • 317
  • 318
  • 319
  • 320
  • 321
  • 322
  • 323
  • 324
  • 325
  • 326
  • 327
  • 328
  • 329
  • 330
  • 331
  • 332
  • 333
  • 334
  • 335
  • 336
  • 337
  • 338
  • 339
  • 340
  • 341
  • 342
  • 343
  • 344
  • 345
  • 346
  • 347
  • 348
  • 349
  • 350
  • 351
  • 352
  • 353
  • 354
  • 355
  • 356
  • 357
  • 358
  • 359
  • 360
  • 361
  • 362
  • 363
  • 364
  • 365
  • 366
  • 367
  • 368
  • 369
  • 370
  • 371
  • 372
  • 373
  • 374
  • 375
  • 376
  • 377
  • 378
  • 379
  • 380
  • 381
  • 382
  • 383
  • 384
  • 385
  • 386
  • 387
  • 388
  • 389
  • 390
  • 391
  • 392
  • 393
  • 394
  • 395
  • 396
  • 397
  • 398

上述就是实现的功能,写的不好。欢迎补充。转载请标明出处,谢谢

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/108896
推荐阅读