Map.vue 3.7 KB

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