Map.vue 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <script>
  2. import L from 'leaflet'
  3. import 'leaflet/dist/leaflet.css'
  4. import axios from'axios'
  5. import mapUrl from '@/assets/images/layout_cont.png'
  6. export default{
  7. mounted(){
  8. this.$nextTick(()=>{
  9. this.init()
  10. })
  11. },
  12. methods:{
  13. init(){
  14. // let w = 2420,
  15. // h = 1868;
  16. const map = L.map('map', {
  17. minZoom: 1,
  18. maxZoom: 3,
  19. center: [0, 0],
  20. zoom: 1,
  21. crs: L.CRS.Simple
  22. });
  23. // const southWest = map.unproject([0, h], map.getMaxZoom() - 1);
  24. // const northEast = map.unproject([w, 0], map.getMaxZoom() - 1);
  25. // const bounds = new L.LatLngBounds(southWest, northEast);
  26. var yx = L.latLng
  27. var xy = function xy(x, y) {
  28. if (L.Util.isArray(x)) {
  29. return yx(x[1], x[0])
  30. }
  31. return yx(y, x)
  32. }
  33. var bounds = [xy(-200, -200), xy(300, 185)]
  34. /**添加图层 */
  35. L.imageOverlay(mapUrl, bounds).addTo(map);
  36. map.setMaxBounds(bounds);
  37. /**添加标记点 */
  38. const docWidth = document.documentElement.clientWidth;
  39. const markers=L.layerGroup()
  40. let coordinates=[-100, 100];
  41. let options={id:1};
  42. let icon="project-12";
  43. let tip="盲板抽堵作业";
  44. let iconSize = [27*docWidth/1920, 33*docWidth/1920];
  45. function drawMark(option={tip,icon,options,coordinates}){
  46. console.log({option})
  47. let myIcon = L.divIcon({
  48. className: option.icon,
  49. iconSize: iconSize,
  50. });
  51. let marker = L.marker(option.coordinates, {...option.options, icon : myIcon}).bindPopup(option.tip).addTo(markers);
  52. marker.on('click', function(ev) {
  53. let id=ev.target.options?.id
  54. console.log({ev,id})
  55. })
  56. }
  57. markers.addTo(map)
  58. this.getLayers((res)=>{
  59. if(res&&res.length>0){
  60. for(let i=0;i<res.length;i++){
  61. drawMark({
  62. tip:`${res[i].jobactivityTitle}`,
  63. icon:`project-${res[i].typeId}`,
  64. coordinates:[res[i].jobactQywzwd,res[i].jobactQywzjd],
  65. options:{id:14}
  66. })
  67. }
  68. }
  69. })
  70. },
  71. getLayers(callback){
  72. axios.request({
  73. url:'http://192.168.3.5:8080/JobActivitys/vedit/list',
  74. headers:{
  75. Authorization:'Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ7XCJvY0lkXCI6NDA2MjgzLFwib2NUeXBlSWRcIjoyLFwidXNlcklkXCI6NDA2MjgzLFwidXNlck5hbWVcIjpcImFkbWluMDAzXCIsXCJ1c2VyUmVhbE5hbWVcIjpcIueuoeeQhuWRmDNcIn0iLCJpYXQiOjE2ODA1OTY4MTksImV4cCI6MTY4MDYzMjgxOX0.dht0awVtwmjzzwpN-kbLo-jf1hXEb5w-3fYcJ4Tgp64'
  76. }
  77. }).then((res)=>{
  78. if(res.data.code===0){
  79. callback(res.data.data)
  80. }else{
  81. alert('接口异常')
  82. }
  83. })
  84. }
  85. }
  86. }
  87. </script>
  88. <template>
  89. <div id="map"></div>
  90. </template>
  91. <style lang="scss" scoped>
  92. #map {
  93. height: 100%;
  94. width: 100%;
  95. background-color: inherit;
  96. ::v-deep .leaflet-control{
  97. display: none;
  98. // &.leaflet-control-attribution {
  99. // display: none;
  100. // }
  101. }
  102. }
  103. </style>