当前位置:   article > 正文

Vue+Openlayer动态加载geojson_vue baidu map加载geojson

vue baidu map加载geojson

加载1个或多个要素

 

  1. <template>
  2. <div id="map" style="width: 100vw; height: 100vh"></div>
  3. </template>
  4. <script>
  5. import "ol/ol.css";
  6. import TileLayer from "ol/layer/Tile";
  7. import VectorLayer from "ol/layer/Vector";
  8. import VectorSource from "ol/source/Vector";
  9. import XYZ from "ol/source/XYZ";
  10. import { Map, View, Feature, ol } from "ol";
  11. import { Style, Stroke, Fill } from "ol/style";
  12. import { Polygon, MultiPolygon } from "ol/geom";
  13. import areaGeo from "@/assets/chengdu.json";
  14. export default {
  15. data() {
  16. return {
  17. map: {},
  18. areaLayer: {},
  19. };
  20. },
  21. mounted() {
  22. this.initMap(); //初始化地图方法
  23. this.addArea(areaGeo); //添加区域图层方法
  24. this.pointMove();
  25. this.getFeatureByClick();
  26. },
  27. methods: {
  28. pointMove() {
  29. // 设置鼠标划过矢量要素的样式
  30. this.map.on("pointermove", (e) => {
  31. const isHover = this.map.hasFeatureAtPixel(e.pixel);
  32. this.map.getTargetElement().style.cursor = isHover ? "pointer" : "";
  33. });
  34. },
  35. getFeatureByClick() {
  36. this.map.on("click", (e) => {
  37. let features = this.map.getFeaturesAtPixel(e.pixel);
  38. this.map.getView().fit(features[0].getGeometry(), {
  39. duration: 1500,
  40. padding: [100, 100, 100, 100],
  41. });
  42. });
  43. },
  44. /**
  45. * 设置区域
  46. */
  47. addArea(geo = {}) {
  48. if (Object.keys(geo).length == 0 && geo.features.length == 0) return;
  49. // 设置图层
  50. this.areaLayer = new VectorLayer({
  51. source: new VectorSource({
  52. features: [],
  53. }),
  54. });
  55. // 添加图层
  56. this.map.addLayer(this.areaLayer);
  57. let features = geo.features;
  58. for (let i in features) {
  59. let areaFeature = {};
  60. if (features[i].geometry.type == "MultiPolygon") {
  61. areaFeature = new Feature({
  62. geometry: new MultiPolygon(features[i].geometry.coordinates),
  63. });
  64. } else if (features[i].geometry.type == "Polygon") {
  65. areaFeature = new Feature({
  66. geometry: new Polygon(features[i].geometry.coordinates),
  67. });
  68. }
  69. areaFeature.setStyle(
  70. new Style({
  71. fill: new Fill({ color: "#08305d5b" }),
  72. stroke: new Stroke({
  73. width: 3,
  74. color: [71, 137, 227, 1],
  75. }),
  76. })
  77. );
  78. areaFeature.setProperties(features[i].properties);
  79. this.areaLayer.getSource().addFeature(areaFeature);
  80. }
  81. },
  82. /**
  83. * 初始化地图
  84. */
  85. initMap() {
  86. this.map = new Map({
  87. target: "map",
  88. layers: [
  89. new TileLayer({
  90. source: new XYZ({
  91. url: "http://map.geoq.cn/ArcGIS/rest/services/ChinaOnlineStreetPurplishBlue/MapServer/tile/{z}/{y}/{x}",
  92. }),
  93. }),
  94. ],
  95. view: new View({
  96. projection: "EPSG:4326",
  97. center: [103, 31],
  98. zoom: 7,
  99. }),
  100. });
  101. },
  102. },
  103. };
  104. </script>

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

闽ICP备14008679号