资讯专栏INFORMATION COLUMN

VUE 记一次高德地图和Echarts(中国地图)

KoreyLee / 4335人阅读

摘要:效果图准备工作高德地图申请安装中国地图高德地图官方点我进入创建动态创建标签后的是需要的插件中国地图安装开始使用创建组件写入需要的分布指数高低写入高德地图需要的

效果图

准备工作

高德地图申请key

安装Echarts 中国地图

</>复制代码

  1. 高德地图

官方API:点我进入

创建AMap.js

</>复制代码

  1. export default function MapLoader () {
  2. return new Promise((resolve, reject) => {
  3. if (window.AMap) {
  4. resolve(window.AMap)
  5. } else {
  6. // 动态创建script标签
  7. var script = document.createElement("script")
  8. script.type = "text/javascript"
  9. script.async = true
  10. script.src = "https://webapi.amap.com/maps");;
  11. // plugin后的是需要的插件
  12. script.onerror = reject
  13. document.head.appendChild(script)
  14. }
  15. window.initAMap = () => {
  16. resolve(window.AMap)
  17. }
  18. })
  19. }

</>复制代码

  1. Echarts 中国地图

安装Echarts

</>复制代码

  1. npm install echarts -S

开始使用

创建组件 distribution.vue

写入Echarts 需要的DOM

</>复制代码

  1. 分布指数

写入高德地图需要的DOM

</>复制代码

引入高德地图、Echarts

</>复制代码

  1. import "echarts/map/js/china"; //中国地图js
  2. import MapLoader from "components/home/AMap"

Echarts方法

</>复制代码

  1. generateEcharts() {
  2. var echarts = require("echarts");
  3. var myChart = echarts.init(this.$refs.main); // 基于准备好的dom,初始化echarts实例
  4. this.$ajax.get(this.HOST + "/redesign/home/findEnterpriseistribution").then( (res) => {
  5. // 数据格式
  6. // [
  7. // {name: "北京", value: 3, ranking: 1},
  8. // {name: "河北", value: 2, ranking: 2},
  9. // {name: "上海", value: 1, ranking: 3},
  10. // {name: "广东", value: 1, ranking: 4}
  11. // ...
  12. // ]
  13. this.homebution = res.data.resultObject;
  14. var data = [];
  15. for (let i = 0; i < res.data.resultObject.length; i++) {
  16. this.homebutionnum += res.data.resultObject[i].value;
  17. let classnum = ""
  18. // 排名显示样式
  19. if(res.data.resultObject[i].ranking == 1) {
  20. classnum = "#1669e6"
  21. }else if(res.data.resultObject[i].ranking > 1 && res.data.resultObject[i].ranking <= 4) {
  22. classnum = "#31a4ed"
  23. }else if(res.data.resultObject[i].ranking > 4 && res.data.resultObject[i].ranking <= 10) {
  24. classnum = "#2cccfd"
  25. }else if(res.data.resultObject[i].ranking > 10 && res.data.resultObject[i].ranking <= 20) {
  26. classnum = "#7fe2fd"
  27. }else {
  28. classnum = "#d9f4fe"
  29. }
  30. data.push(
  31. {
  32. name: res.data.resultObject[i].name,
  33. itemStyle: {
  34. normal: {
  35. areaColor: classnum,
  36. // borderWidth: 2
  37. },
  38. emphasis: {//高亮状态下的多边形和标签样式
  39. borderWidth: 1,
  40. // borderColor: "#d9f4fe",
  41. areaColor: "#d9f4fe",
  42. label: {
  43. shadowColor: "#d9f4fe", //默认透明
  44. shadowBlur: 10,
  45. show: true,
  46. textStyle: {
  47. fontSize: 12,
  48. color: "#3db77f"
  49. }
  50. }
  51. }
  52. },
  53. label: {
  54. normal: {
  55. show: true,
  56. textStyle: {
  57. fontSize: 12,
  58. color: "#0d6fb8"
  59. }
  60. },
  61. emphasis: {
  62. show: true,
  63. color: "#3db77f"
  64. }
  65. },
  66. value: res.data.resultObject[i].value ");"",
  67. ranking: res.data.resultObject[i].ranking ");""
  68. }
  69. )
  70. }
  71. // 准备提示数据
  72. var option = {
  73. tooltip: {
  74. formatter: (params) => {
  75. var info = "";
  76. if(params.data) {
  77. info = `
  78. ${ params.name }

  79. ${ params.data.value }

  80. 排名:${ params.data.ranking }

  81. `
  82. }else {
  83. info = `
  84. ${ params.name }

  85. `
  86. }
  87. return info;
  88. },
  89. backgroundColor: "rgba(0,0,0,.6)",//提示标签背景颜色
  90. textStyle: { color: "#fff" } //提示标签字体颜色
  91. },
  92. //配置属性
  93. series: [{
  94. name: "数据",
  95. type: "map",
  96. mapType: "china",
  97. roam: true,
  98. label: {
  99. normal: {
  100. show: false //省份名称
  101. },
  102. emphasis: {
  103. show: false
  104. }
  105. },
  106. data: data //数据
  107. }],
  108. };
  109. //使用制定的配置项和数据显示图表
  110. myChart.setOption(option);
  111. })
  112. }

高德地图方法

</>复制代码

  1. Scottmap() {
  2. let that = this;
  3. that.map = "";
  4. MapLoader().then(AMap => {
  5. console.log("地图加载成功")
  6. that.map = new AMap.Map("container", {
  7. center: [116.05438, 38.98065],
  8. // center: [116.397428, 39.90923],
  9. zoom: 5
  10. })
  11. // 由于项目是按雄安地区使用 API暂时还未把雄安划分 所以决定把安新县、雄县、容城县(三县合一就是雄安)绘制成高亮
  12. // 绘制高亮
  13. var district = new AMap.DistrictSearch({
  14. // 返回行政区边界坐标等具体信息
  15. extensions: "all",
  16. // 设置查询行政区级别为 区
  17. level: "district"
  18. })
  19. var polygons=[];
  20. var polygons2=[];
  21. var polygons3=[];
  22. district.search("安新县", (status, result) => {
  23. if(polygons) {
  24. that.map.remove(polygons)//清除上次结果
  25. }
  26. polygons = [];
  27. let bounds = result.districtList[0].boundaries;
  28. if (bounds) {
  29. for (let i = 0; i < bounds.length; i++) {
  30. //生成行政区划polygon
  31. var polygon = new AMap.Polygon({
  32. strokeWeight: 1,
  33. path: bounds[i],
  34. fillOpacity: 0.4,
  35. fillColor: "#80d8ff",
  36. strokeColor: "#0091ea"
  37. });
  38. polygons.push(polygon);
  39. }
  40. }
  41. that.map.add(polygons);
  42. });
  43. district.search("雄县", (status2, result2) => {
  44. if(polygons2) {
  45. that.map.remove(polygons2)//清除上次结果
  46. }
  47. polygons2 = [];
  48. let bounds = result2.districtList[0].boundaries;
  49. if (bounds) {
  50. for (let i = 0; i < bounds.length; i++) {
  51. //生成行政区划polygon
  52. var polygon = new AMap.Polygon({
  53. strokeWeight: 1,
  54. path: bounds[i],
  55. fillOpacity: 0.4,
  56. fillColor: "#80d8ff",
  57. strokeColor: "#0091ea"
  58. });
  59. polygons2.push(polygon);
  60. }
  61. }
  62. that.map.add(polygons2)
  63. })
  64. district.search("容城县", (status2, result2) => {
  65. if(polygons3) {
  66. that.map.remove(polygons2)//清除上次结果
  67. }
  68. polygons3 = [];
  69. let bounds = result2.districtList[0].boundaries;
  70. if (bounds) {
  71. for (let i = 0; i < bounds.length; i++) {
  72. //生成行政区划polygon
  73. var polygon = new AMap.Polygon({
  74. strokeWeight: 1,
  75. path: bounds[i],
  76. fillOpacity: 0.4,
  77. fillColor: "#80d8ff",
  78. strokeColor: "#0091ea"
  79. });
  80. polygons3.push(polygon);
  81. }
  82. }
  83. that.map.add(polygons3)
  84. })
  85. // 构造矢量圆形(圆)
  86. let circle = new AMap.Circle({
  87. center: [116.05438, 38.98065], // 雄安的中心点
  88. radius: that.distance * 1000, //半径
  89. strokeColor: "#ffffff", //线颜色
  90. strokeOpacity: 1, //线透明度
  91. strokeWeight: 3, //线粗细度
  92. fillColor: "#276cd4", //填充颜色
  93. fillOpacity: 0.2 //填充透明度
  94. });
  95. that.map.add(circle);
  96. that.map.setFitView(circle);
  97. let lnglat = new AMap.LngLat(116.05438, 38.98065) // lng, lat 替换成传入的坐标
  98. let polylinePath = [
  99. new AMap.LngLat(116.05438, 38.98065),
  100. new AMap.LngLat(lnglat.offset(that.distance * 1000,0).lng,38.98065)
  101. ];
  102. // 创建线覆盖物
  103. let polyline = new AMap.Polyline({
  104. path: polylinePath,
  105. strokeColor: "#ffffff", //线颜色
  106. strokeOpacity: 1, //线透明度
  107. strokeWeight: 2, //线宽
  108. strokeStyle: "solid", //线样式
  109. strokeDasharray: [10, 5] //补充线样式
  110. });
  111. that.map.add(polyline);
  112. // 绘制字
  113. let textPos = lnglat.offset(that.distance * 500,that.distance * 50);
  114. let text = new AMap.Text({
  115. text: `${ that.distance }公里`,
  116. position: textPos,
  117. map: this.map,
  118. style:{
  119. "background": "transparent",
  120. "border": "0 none",
  121. "color": "#276cd4",
  122. "font-size":"14px"}
  123. })
  124. that.map.add(text);
  125. // 测试距离
  126. let Centralpoint = [116.05438, 38.98065];
  127. let targetpoint = this.region
  128. // debugger
  129. // 转经纬度
  130. let geocoder = new AMap.Geocoder({
  131. city: "全国", //城市设为北京,默认:“全国”
  132. });
  133. let markernum = 0
  134. for (let i = 0; i < targetpoint.length; i++) {
  135. geocoder.getLocation(targetpoint[i], (status, result) => {
  136. if (status === "complete"&& result.geocodes.length) {
  137. let dis = AMap.GeometryUtil.distance(Centralpoint,result.geocodes[0].location);
  138. // 绘制点标记显示内容,HTML要素字符串
  139. if(dis <= that.distance * 1000) {
  140. markernum++;
  141. let markerContent = "" +
  142. "
    " +
  143. " " +
  144. "
    "+ markernum +"
    "
    +
  145. "";
  146. this.$ajax.get(this.HOST + "/redesign/home/findCompanyAll").then( (res) => {
  147. this.matchingenterpriseList.push(res.data.resultObject[i])
  148. })
  149. let marker = new AMap.Marker({
  150. content: markerContent,
  151. position: result.geocodes[0].location,
  152. offset: new AMap.Pixel(-13, -30),
  153. extData:{
  154. id: markernum
  155. }
  156. });
  157. that.markers.push(marker);
  158. marker.setMap(this.map);
  159. }
  160. }
  161. })
  162. }
  163. that.matchingenterpriseState = true;
  164. }, e => {
  165. console.log("地图加载失败" ,e)
  166. })
  167. },

左侧企业列表

</>复制代码

  1. // 企业鼠标经过
  2. matchingenterprisemouseenter(item,index) {
  3. this.activemetchmouseenter = index;
  4. let targetMarker = {};
  5. for (let i = 0; i < this.markers.length; i++) {
  6. let id = this.markers[i].getExtData().id;
  7. if(id == index){
  8. targetMarker = this.markers[i];
  9. break;
  10. }
  11. }
  12. // 重新替换样式
  13. let markerContent = "" +
  14. "
    " +
  15. " " +
  16. "
    "+ index +"
    "
    +
  17. "";
  18. // targetMarker.setIcon("//a.amap.com/jsapi_demos/static/demo-center/icons/poi-marker-default.png");
  19. targetMarker.setContent(markerContent)
  20. let position = targetMarker.getPosition();
  21. this.infoWindow = new AMap.InfoWindow({
  22. position: position,
  23. offset: new AMap.Pixel(0, -35),
  24. content: item.companyName
  25. });
  26. this.infoWindow.open(this.map);
  27. },

</>复制代码

  1. // 企业鼠标离开
  2. matchingenterprisemouseleave(item,index) {
  3. this.activemetchmouseenter = "";
  4. let targetMarker = {};
  5. for (let i = 0; i < this.markers.length; i++) {
  6. let id = this.markers[i].getExtData().id;
  7. if(id == index){
  8. targetMarker = this.markers[i];
  9. break;
  10. }
  11. }
  12. let markerContent = "" +
  13. "
    " +
  14. " " +
  15. "
    "+ index +"
    "
    +
  16. "";
  17. // targetMarker.setIcon("//a.amap.com/jsapi_demos/static/demo-center/icons/poi-marker-default.png");
  18. targetMarker.setContent(markerContent)
  19. this.map.remove(this.infoWindow);
  20. },

</>复制代码

  1. // 企业点击
  2. matchingenterpriseclick(item,index) {
  3. this.activeState3 = true;
  4. this.activeState1 = false;
  5. this.activeState = false;
  6. this.itemList = item;
  7. // this.map.remove(overlayGroups);
  8. let geocoder = new AMap.Geocoder({
  9. city: "全国", //城市设为北京,默认:“全国”
  10. });
  11. geocoder.getLocation(item.address, (status, result) => {
  12. this.map.setCenter(result.geocodes[0].location); //设置地图中心点
  13. this.map.setZoom(30); // 缩放级别
  14. })
  15. },

具体代码

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/7867.html

相关文章

  • vuevuex,echarts,地图,ueditor的使用

    摘要:获取富文本内容地图我是使用高德地图在全局导入为我申请的也可以自己去申请高德地图官网案例 前言 今天是个好日子,大家六一快乐;vue-cli生成的template还需要配置axios,vuex,element等插件,该项目中将这些常用插件进行了配置;项目开发中template可以快速复用,也是可以快速上手vue的一个demo; 1.动态效果图 showImg(https://segmen...

    Cheriselalala 评论0 收藏0
  • echarts结合高德API进行地图下钻

    摘要:广告开始最近做了表格数据转成图表展示的一个组件,地址如下整合地图的时候发现针对地级市的一些文件太多了,全部引入后有将近,所以就用了下高德老爷给的组件和接口,然后弄完在这记录一下虽然这种例子在社区里有很多这个组件文件放在了中,样式奇丑,请原谅 /——————广告开始——————/ 最近做了表格数据转成图表展示的一个react组件,地址如下: https://github.com/Lyla...

    happen 评论0 收藏0
  • vue地图可视化 ArcGIS篇(3)

    摘要:不建议底图选择中存在两种不同坐标体系,如下图坐标存在明显的偏差,火星坐标在采用坐标系的地图上位置偏上彩色中国天地图全球卫星地图例如我们使用的类进行查找,返回的数据都是国际坐标,因此必须进行偏差纠正。 ArcGIS for javascript开发心得 本次实例中采用ArcGIS for javascript3.24版本,由于版本3与4在API等存在较大区别,就不一一列举,详细区别看官方...

    oogh 评论0 收藏0
  • 开发一个基于 Vue2.0 的个人天气预报

    摘要:本场是一个基于的个人天气预报项目,就是兴趣所致,做来玩玩。本项目会采用高德地图,可视化库和相关的技术来开发本项目。本场你将学到如下内容学会制作自己的天气预报学会使用部分高德地图的学会使用的部分相关的一些技术。有兴趣的请到查看效果图如下 本场 Chat 是一个基于 Vue 的个人天气预报项目,就是兴趣所致,做来玩玩。顺便扩展一下知识面。 本项目会采用高德地图 API,Echarts 可视...

    mushang 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<