• 高德地图用法


    一.地图引入

    1. <script type="text/javascript"
    2. src="https://webapi.amap.com/maps?v=2.0&key=你的key">script>

    二.挂载地图

    创建一个盒子  给上宽高

    <div id="map">div>

    初始化地图

    1. this.map = new AMap.Map('map', {
    2. zoom: 16, //设置地图显示的缩放级别
    3. center: [114.366248, 30.53886],//设置地图中心点坐标
    4. // mapStyle: 'amap://styles/', //设置地图的显示样式
    5. viewMode: '2D', //设置地图模式
    6. });

    注销地图

    1. beforeDestory() {
    2. this.map.destory()
    3. },

    三.添加点位

    1. var marker = new AMap.Marker({
    2. position: arr,
    3. icon: new AMap.Icon({
    4. image: img,
    5. }),
    6. offset,
    7. })
    8. this.markArr.push(marker)
    9. // 弹框
    10. var infoWindow = new AMap.InfoWindow({
    11. content: this.createInfoWindow(content, name, inMid, tips, bottomName),
    12. anchor: 'bottom-center',
    13. isCustom: true,
    14. closeWhenClickMap: true,
    15. offset: infoOffset
    16. });
    17. // 点击事件
    18. marker.on('click', () => {
    19. infoWindow.open(map, arr);
    20. })
    21. map.add(marker);

    高德地图跟百度地图一样  自定义弹框就是往弹框盒子里自己添加 div 然后写样式

    1. createInfoWindow(content, name, inMid, tips, bottomName) {
    2. var info = document.createElement("div");
    3. var middle = document.createElement("div");
    4. var inMiddle = document.createElement("div")
    5. var spans = document.createElement("div")
    6. var divtop = document.createElement("div")
    7. var divnbottom = document.createElement("div")
    8. divtop.className = 'fff'
    9. divnbottom.className = bottomName
    10. divtop.innerHTML = content
    11. divnbottom.innerHTML = tips
    12. spans.appendChild(divtop);
    13. spans.appendChild(divnbottom);
    14. middle.className = name;
    15. inMiddle.className = inMid;
    16. middle.appendChild(inMiddle);
    17. middle.appendChild(spans);
    18. info.appendChild(middle);
    19. return info;
    20. },

    如果弹框内添加点击事件 只能去写定时器判断这个 dom 是否存在  存在用 js 动态添加 不能在动态添加 HTML 的时候在标签里写上 onClick 或者 @click 事件  这样是没有用的  动态添加 element 组件也是没用的 

    1. this.setint = setInterval(() => {
    2. if (document.getElementById("lastImg")) {
    3. let con = document.getElementById("lastImg")
    4. con.onclick = () => {
    5. let list = []
    6. this.showInDialogImgList = []
    7. this.showInDialogWhereList = []
    8. let devChnId = con.getAttribute('devChnId')
    9. this.positionList.forEach(item => {
    10. if (item[0].devChnId == devChnId) {
    11. list = item
    12. }
    13. })
    14. list.forEach(item => {
    15. console.log(item);
    16. if (item.imgUrlS3 && item.imgUrlS3.length) {
    17. this.showInDialogImgList.push(item.imgUrlS3)
    18. } else {
    19. this.showInDialogImgList.push(item.imgUrlDahua)
    20. }
    21. this.showInDialogWhereList.push({
    22. time: item.capDate,
    23. name: item.admDevEncoderChnEntity.channelName
    24. })
    25. })
    26. console.log(this.showInDialogWhereList);
    27. this.innerVisible = true
    28. };
    29. }
    30. }, 1000);

    记得清除

    1. destory() {
    2. clearInterval(this.setint)
    3. }

    四.添加轨迹

    1. let polyline = new AMap.Polyline({
    2. map: this.inmap, //初始化的地图变量
    3. path: this.markerList, //折线的存放数组,很重中。一般是好多经纬度组成的数组。
    4. outlineColor: '#0484E6',
    5. strokeColor: "#0484E6",
    6. strokeOpacity: 0.6,
    7. strokeWeight: 10,
    8. borderWeight: 1,
    9. strokeStyle: "solid",
    10. strokeDasharray: [0, 0, 0],
    11. lineJoin: 'round',
    12. lineCap: 'round',
    13. zIndex: 20,
    14. strokeStyle: "dashed",
    15. strokeDasharray: [40, 10]
    16. });
    17. polyline.setMap(this.inmap)

    markerList 是一个 点位 数组 

    this.markerList.push(new AMap.LngLat(lng, lat))

    五.清除点位

    找到你想清除的点位  将点位添加到数组中  类似于上方的 markerList

    this.map.remove(this.markArr)

    六.清除所有图层

    this.inmap.clearMap()

    七. 改变中心点位

    this.map.setZoomAndCenter(15, [114.123, 30.123])

  • 相关阅读:
    dubbo高可用:负载均衡机制(十二)
    python--第五章 python字典&&函数
    磁盘管理:磁盘结构
    超级实用网站+公众号合集
    【JS】Chapter14-深入面向对象
    Android Gradle 命令打包AAR
    C++ vector类模拟实现
    Godot Shader -变量的声明
    腾讯云入选挑战者象限,2023 Gartner容器管理魔力象限发布
    Spring AOP
  • 原文地址:https://blog.csdn.net/notagoodwriter/article/details/133694051