vue集成echarts导入百度地图实现个人轨迹的的可视化

2 篇文章 0 订阅
2 篇文章 0 订阅

第一步申请百度地图的ak

URL:http://lbsyun.baidu.com/apiconsole/key?application=key

第二步在vue中引入百度地图 

在public下的index.html中引入

如下

 <script type="text/javascript" src="http://api.map.baidu.com/api?v=3.0&ak=A3CklGvnFOjkAzKzay2dySgfdig0GKz4"></script>

在main.js中导入echarts

import echarts from "echarts";
import "echarts/extension/bmap/bmap";
Vue.prototype.$echarts = echarts

在组件中加载百度地图并实现轨迹可视化

<template>
  <div ref="map" class="map"></div>
</template>

<script>
/* eslint-disable */

export default {
  name: "Map",
  data() {
    return {
      chart: "",
      bmap: {}, //定义一个当前的bmap
      mapStyle: {
        styleJson: [
          {
            featureType: "land",
            elementType: "geometry",
            stylers: {
              color: "#081734"
            }
          },
          {
            featureType: "building",
            elementType: "geometry",
            stylers: {
              color: "#04406F"
            }
          },
          {
            featureType: "building",
            elementType: "labels",
            stylers: {
              visibility: "off"
            }
          },
          {
            featureType: "highway",
            elementType: "geometry",
            stylers: {
              color: "#015B99"
            }
          },
          {
            featureType: "highway",
            elementType: "labels",
            stylers: {
              visibility: "off"
            }
          },
          {
            featureType: "arterial",
            elementType: "geometry",
            stylers: {
              color: "#003051"
            }
          },
          {
            featureType: "arterial",
            elementType: "labels",
            stylers: {
              visibility: "off"
            }
          },
          {
            featureType: "green",
            elementType: "geometry",
            stylers: {
              visibility: "off"
            }
          },
          {
            featureType: "water",
            elementType: "geometry",
            stylers: {
              color: "#044161"
            }
          },
          {
            featureType: "subway",
            elementType: "geometry.stroke",
            stylers: {
              color: "#003051"
            }
          },
          {
            featureType: "subway",
            elementType: "labels",
            stylers: {
              visibility: "off"
            }
          },
          {
            featureType: "railway",
            elementType: "geometry",
            stylers: {
              visibility: "off"
            }
          },
          {
            featureType: "railway",
            elementType: "labels",
            stylers: {
              visibility: "off"
            }
          },
          {
            featureType: "all",
            elementType: "labels.text.stroke",
            stylers: {
              color: "#313131"
            }
          },
          {
            featureType: "all",
            elementType: "labels.text.fill",
            stylers: {
              color: "#FFFFFF"
            }
          },
          {
            featureType: "manmade",
            elementType: "geometry",
            stylers: {
              visibility: "off"
            }
          },
          {
            featureType: "manmade",
            elementType: "labels",
            stylers: {
              visibility: "off"
            }
          },
          {
            featureType: "local",
            elementType: "geometry",
            stylers: {
              visibility: "off"
            }
          },
          {
            featureType: "local",
            elementType: "labels",
            stylers: {
              visibility: "off"
            }
          },
          {
            featureType: "subway",
            elementType: "geometry",
            stylers: {
              lightness: -65
            }
          },
          {
            featureType: "railway",
            elementType: "all",
            stylers: {
              lightness: -40
            }
          },
          {
            featureType: "boundary",
            elementType: "geometry",
            stylers: {
              color: "#8b8787",
              weight: "1",
              lightness: -29
            }
          }
        ]
      },
      geoCoordMap: [
        {
          name: "西9",
          value: [108.92025114773189, 34.25127182306254],
          tooltip: {
            formatter: "时间:8:30am<br/>出发地:中海锦城南苑"
          }
        },
        {
          name: "技术研究所",
          value: [108.91814012978332, 34.2491905888672],
          tooltip: {
            formatter: "时间:8:30am<br/>出发地:中海锦城南苑"
          }
        },
        {
          name: "幼儿园",
          value: [108.92196691125595, 34.25130166210155],
          tooltip: {
            formatter: "时间:8:30am<br/>出发地:中海锦城南苑"
          }
        },
        {
          name: "3号楼",
          value: [108.9203050460625, 34.25211477180376],
          tooltip: {
            formatter: "时间:8:30am<br/>出发地:中海锦城南苑"
          }
        },
        {
          name: "书海楼",
          value: [108.91941572360761, 34.25026474923389],
          tooltip: {
            formatter: "时间:8:30am<br/>出发地:中海锦城南苑"
          }
        },
        {
          name: "公共管理硕士教育中心",
          value: [108.9202241985666, 34.25035426740013],
          tooltip: {
            formatter: "时间:8:30am<br/>出发地:中海锦城南苑"
          }
        },
        {
          name: "材料科技B段",
          value: [108.91815809589365, 34.25025728938237],
          tooltip: {
            formatter: "时间:8:30am<br/>出发地:中海锦城南苑"
          }
        },
        {
          name: "健翔楼",
          value: [108.92203877569675, 34.25215952990572],
          tooltip: {
            formatter: "时间:8:30am<br/>出发地:中海锦城南苑"
          }
        }
      ],
      line: [
        {
          fromName: "技术研究所",
          toName: "材料科技大楼B段",
          coords: [
            [108.91814012978332, 34.2491905888672],
            [108.91815809589365, 34.25025728938237]
          ]
        },
        {
          fromName: "材料科技B段",
          toName: "书海楼",
          coords: [
            [108.91815809589365, 34.25025728938237],
            [108.91941572360761, 34.25026474923389]
          ]
        },
        {
          fromName: "书海楼",
          toName: "公共管理硕士教育中心",
          coords: [
            [108.91941572360761, 34.25026474923389],
            [108.9202241985666, 34.25035426740013]
          ]
        },
        {
          fromName: "公共管理硕士教育中心",
          toName: "西9",
          coords: [
            [108.9202241985666, 34.25035426740013],
            [108.92025114773189, 34.25127182306254]
          ]
        },
        {
          fromName: "西9",
          toName: "3号楼",
          coords: [
            [108.9202241985666, 34.25035426740013],
            [108.9203050460625, 34.25211477180376]
          ]
        },
        {
          fromName: "3号楼",
          toName: "健翔楼",
          coords: [
            [108.9203050460625, 34.25211477180376],
            [108.92203877569675, 34.25215952990572]
          ]
        },
        {
          fromName: "健翔楼",
          toName: "幼儿园",
          coords: [
            [108.92203877569675, 34.25215952990572],
            [108.92196691125595, 34.25130166210155]
          ]
        }
      ],
      // COLORS:["red", "yellow", "pink", "#900"]
    };
  },
  mounted() {
    this.initMap();
  },
  methods: {
    initMap() {
      this.chart = this.$echarts.init(this.$refs.map);
      //初始化echarts
      this.chart.setOption({
        animation: false,
        legend: {
          orient: "vertical",
          top: 30,
          left: 30,
          // data: linesData.map(v => v.name),
          textStyle: {
            color: "#222222"
          },
          selectedMode: "multiple"
        },
        bmap: {
          // 加载 bmap 组件
          center: [108.92203428416926, 34.2510330778021],
          zoom: 18,
          roam: true,
          label: {
            show: true
          },
          mapStyle: this.mapStyle
        },
        // visualMap: {
        //   type: "piecewise",
        //   inverse: true,
        //   top: 10,
        //   left: 10,
        //   pieces: [
        //     {
        //       value: 0,
        //       color: this.COLORS[0]
        //     },
        //     {
        //       value: 1,
        //       color: this.COLORS[1]
        //     },
        //     {
        //       value: 2,
        //       color: this.COLORS[2]
        //     },
        //     {
        //       value: 3,
        //       color: this.COLORS[3]
        //     }
        //   ],
        //   borderColor: "#ccc",
        //   borderWidth: 2,
        //   backgroundColor: "#eee",
        //   dimension: 2,
        //   inRange: {
        //     color: this.COLORS,
        //     opacity: 0.7
        //   }
        // },
        tooltip: {
          // show: true,
          // trigger: "item",
          // hideDelay: 4000,
          // formatter: function(d) {
          //   var tool = "经度:" + d.value[0] + "<br/>";
          //   tool += "纬度:" + d.value[1];
          //   console.log(tool)
          //   return tool;
          // }
        },
        series: [
          {
            name: "地点",
            type: "effectScatter",
            coordinateSystem: "bmap",
            polyline: true,
            data: this.geoCoordMap,
            silent: true,
            // label: {
            //   normal: {
            //     show: true,
            //     position: "right",
            //     formatter: "{b}",
            //     color: "yellow"
            //   }
            // },
            symbolSize: 13,
            showEffectOn: "render",
            itemStyle: {
              normal: {
                color: "red"
              }
            },
            progressiveThreshold: 10,
            progressive: 400
          },
          {
            name: "轨迹路线",
            type: "lines",
            coordinateSystem: "bmap",
            silent: true, // 图形不响应和触发鼠标事件
            polyline: true,
            lineStyle: { color: "red", width: "4" },
            z: 15,
            data: this.line,
            effect: {
              show: true,
              period: 3, //轨迹移动的实体指向速度,值越小速度越快
              trailLength: 0.5, //特效尾迹长度[0,1]值越大,尾迹越长重
              color: "yellow",
              symbol:
                "path://M30.9,53.2C16.8,53.2,5.3,41.7,5.3,27.6S16.8,2,30.9,2C45,2,56.4,13.5,56.4,27.6S45,53.2,30.9,53.2z M30.9,3.5C17.6,3.5,6.8,14.4,6.8,27.6c0,13.3,10.8,24.1,24.101,24.1C44.2,51.7,55,40.9,55,27.6C54.9,14.4,44.1,3.5,30.9,3.5z M36.9,35.8c0,0.601-0.4,1-0.9,1h-1.3c-0.5,0-0.9-0.399-0.9-1V19.5c0-0.6,0.4-1,0.9-1H36c0.5,0,0.9,0.4,0.9,1V35.8z M27.8,35.8 c0,0.601-0.4,1-0.9,1h-1.3c-0.5,0-0.9-0.399-0.9-1V19.5c0-0.6,0.4-1,0.9-1H27c0.5,0,0.9,0.4,0.9,1L27.8,35.8L27.8,35.8z", //轨迹移动的实体
              symbolSize: 15 //图标大小
            }
          }
        ]
      });
      this.bmap = this.chart
        .getModel()
        .getComponent("bmap")
        .getBMap();
      this.bmap.addControl(
        new BMap.ScaleControl({ anchor: BMAP_ANCHOR_BOTTOM_RIGHT })
      ); // 在左下角显示比例尺控件
      // this.bmap.addEventListener("click", function(e) {
      //   alert(e.point.lng + "," + e.point.lat);
      // });
    }
  }
};
</script>

<style scoped>
.map {
  width: 100%;
  height: 900px;
}
</style>

到此就应用成功啦。。

网上的例子vue中的比较少,所以分享。给大家参考参考!

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值