MapHolder.vue 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <template>
  2. <div id="map2DViewer"></div>
  3. </template>
  4. <script>
  5. import publicFun from "@/utils/publicFunction.js";
  6. import { get } from "@/utils/request";
  7. export default {
  8. name: "MapHolder",
  9. data() {
  10. return {
  11. town: "祝桥镇,南汇新城镇,川沙新镇,老港镇,惠南镇,航头镇,泥城镇,书院镇,新场镇,大团镇,唐镇,曹路镇,宣桥镇,张江镇,合庆镇,周浦镇,康桥镇,三林镇,高桥镇,高东镇,金桥镇,北蔡镇,万祥镇,高行镇",
  12. };
  13. },
  14. mounted() {
  15. var that = this;
  16. //地图初始化
  17. that.mapInit();
  18. that.getJSonData();
  19. },
  20. methods: {
  21. mapInit: function () {
  22. map2DViewer.map = L.map("map2DViewer", {
  23. attributionControl: false,
  24. zoomControl: false,
  25. minZoom: 10,
  26. maxZoom: 15,
  27. }).setView(systemConfig.mapViewer.center, systemConfig.mapViewer.zoom);
  28. //添加默认图层
  29. var guid = publicFun.buildGuid("baseLayer");
  30. var layer = L.esri
  31. .tiledMapLayer({
  32. url:
  33. // "http://aimap.pudong.sh:5236/maps/rest/services/basemap-shanghai-gem-blue-sh2000/mapserver/"
  34. systemConfig.mapService +
  35. "?servertype=Street_Purplish_Blue&token=" +
  36. systemConfig.token,
  37. })
  38. .addTo(map2DViewer.map);
  39. layer.guid = guid;
  40. map2DViewer.layers[guid] = layer;
  41. },
  42. // 读取浦东新区GeoJSON格式的区域数据
  43. getJSonData() {
  44. if (!map2DViewer.map) {
  45. this.mapInit();
  46. } else {
  47. get("./static/json/pdgeojson.json", "").then((geoJson) => {
  48. geoJson.features.map((feature) => {
  49. if (
  50. this.town.indexOf(feature.properties.NAME.replace("镇", "")) > -1
  51. ) {
  52. let center = turf.center(feature.geometry);
  53. this.renderPolygon(feature);
  54. }
  55. });
  56. });
  57. }
  58. },
  59. setView: function (coord, zoom) {
  60. console.log(coord, "位置");
  61. let center = L.latLng(coord[0], coord[1]);
  62. map2DViewer.map.setView(center, zoom);
  63. },
  64. renderPolygon: function (feature) {
  65. let center = turf.center(feature.geometry);
  66. let itemvalue = JSON.parse((Math.random() * 1000).toFixed(0));
  67. let polygonData = JSON.parse(JSON.stringify(feature));
  68. let coordinates = polygonData.geometry.coordinates[0];
  69. let polygon = L.polygon(this.latLngsToReverse(coordinates), {
  70. color: "#87d8fd",
  71. weight: 3,
  72. fillColor: this.getColor(itemvalue),
  73. opacity: 1,
  74. fillOpacity: 0.4,
  75. }).addTo(map2DViewer.map);
  76. center = JSON.parse(JSON.stringify(center)).geometry.coordinates;
  77. center.reverse();
  78. let wmarker = L.circleMarker(center, {
  79. radius: 10,
  80. weight: 1,
  81. fillOpacity: 0,
  82. color: "#e6d273",
  83. });
  84. wmarker.bindLabel(feature.properties.NAME, {
  85. noHide: true,
  86. clickable: true,
  87. offset: [-25, 10],
  88. });
  89. wmarker.addTo(map2DViewer.map);
  90. L.circleMarker(center, {
  91. radius: 8,
  92. weight: 1,
  93. fillOpacity: 0,
  94. color: "#e6d273",
  95. }).addTo(map2DViewer.map);
  96. L.circleMarker(center, {
  97. radius: 5,
  98. weight: 1,
  99. fillOpacity: 1,
  100. color: "#e6d273",
  101. }).addTo(map2DViewer.map);
  102. },
  103. latLngsToReverse: function (latlngsAry) {
  104. var tempLatlngsAry = JSON.parse(JSON.stringify(latlngsAry));
  105. if (typeof tempLatlngsAry[0] != "object") {
  106. return tempLatlngsAry.reverse();
  107. } else {
  108. for (var i = 0, l = tempLatlngsAry.length; i < l; i++) {
  109. tempLatlngsAry[i] = this.latLngsToReverse(tempLatlngsAry[i]);
  110. }
  111. }
  112. return tempLatlngsAry;
  113. },
  114. getColor: function (val) {
  115. var that = this;
  116. if (val < 350) {
  117. return "#e565ff";
  118. } else if (val < 700 && val >= 350) {
  119. return "#0055ff";
  120. } else {
  121. return "#00ffd5";
  122. }
  123. },
  124. },
  125. };
  126. </script>
  127. <style scoped>
  128. #map2DViewer {
  129. position: absolute;
  130. width: 100%;
  131. height: calc(100% - 60px);
  132. z-index: 1;
  133. top: 60px;
  134. left: 0px;
  135. }
  136. </style>