Map.vue 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <script>
  2. import L from 'leaflet'
  3. import 'leaflet/dist/leaflet.css'
  4. export default{
  5. mounted(){
  6. this.$nextTick(()=>{
  7. this.init()
  8. })
  9. },
  10. methods:{
  11. init(){
  12. let w = 2420,
  13. h = 1868,
  14. url='http://dev.abaoya.cn/layout_cont.png';
  15. const map = L.map('map', {
  16. minZoom: 1,
  17. maxZoom: 3,
  18. center: [0, 0],
  19. zoom: 1,
  20. crs: L.CRS.Simple
  21. });
  22. const southWest = map.unproject([0, h], map.getMaxZoom() - 1);
  23. const northEast = map.unproject([w, 0], map.getMaxZoom() - 1);
  24. const bounds = new L.LatLngBounds(southWest, northEast);
  25. /**添加图层 */
  26. L.imageOverlay(url, bounds).addTo(map);
  27. map.setMaxBounds(bounds);
  28. /**添加标记点 */
  29. const docWidth = document.documentElement.clientWidth;
  30. const markers=L.layerGroup()
  31. let coordinates=[-100, 100];
  32. let options={id:1};
  33. let icon="project-12";
  34. let tip="盲板抽堵作业";
  35. let iconSize = [40*docWidth/1920, 50*docWidth/1920];
  36. function drawMark(option={tip,icon,options,coordinates}){
  37. console.log({option})
  38. let myIcon = L.divIcon({
  39. className: option.icon,
  40. iconSize: iconSize,
  41. });
  42. let marker = L.marker(option.coordinates, {...option.options, icon : myIcon}).bindPopup(option.tip).addTo(markers);
  43. marker.on('click', function(ev) {
  44. let id=ev.target.options?.id
  45. console.log({ev,id})
  46. })
  47. }
  48. for(let i=1;i<13;i++){
  49. drawMark({
  50. tip:`第${i}个坐标点,<br />位置:${-20*i},${i*20+50}`,
  51. icon:`project-${i}`,
  52. coordinates:[-33*i,i*48],
  53. options:{id:i}
  54. })
  55. }
  56. markers.addTo(map)
  57. }
  58. }
  59. }
  60. </script>
  61. <template>
  62. <div id="map"></div>
  63. </template>
  64. <style lang="scss" scoped>
  65. #map {
  66. height: 100%;
  67. width: 100%;
  68. background-color: inherit;
  69. ::v-deep .leaflet-control{
  70. display: none;
  71. // &.leaflet-control-attribution {
  72. // display: none;
  73. // }
  74. }
  75. }
  76. </style>