|
45 | 45 | "../assets/data/netjsonmap-indoormap.json", |
46 | 46 | { |
47 | 47 | render: "map", |
48 | | - crs: L.CRS.Simple, |
49 | 48 | // set map initial state. |
50 | 49 | mapOptions: { |
51 | | - center: [48.577, 18.539], |
| 50 | + center: [0, 0], |
52 | 51 | zoom: 0, |
53 | | - zoomSnap: 0.3, |
54 | | - minZoom: -1, |
55 | | - maxZoom: 2, |
| 52 | + minZoom: 6, |
| 53 | + maxZoom: 10, |
| 54 | + zoomSnap: 0.5, |
| 55 | + zoomDelta: 0.5, |
56 | 56 | nodeConfig: { |
57 | 57 | label: { |
58 | 58 | show: false, |
59 | 59 | }, |
60 | 60 | animation: false, |
61 | 61 | }, |
62 | | - linkConfig: { |
63 | | - linkStyle: { |
64 | | - width: 4, |
65 | | - }, |
66 | | - animation: false, |
67 | | - }, |
68 | | - baseOptions: { |
69 | | - media: [ |
70 | | - { |
71 | | - query: { |
72 | | - minWidth: 320, |
73 | | - maxWidth: 850, |
74 | | - }, |
75 | | - option: { |
76 | | - tooltip: { |
77 | | - show: false, |
78 | | - }, |
79 | | - }, |
80 | | - }, |
81 | | - { |
82 | | - query: { |
83 | | - minWidth: 851, |
84 | | - }, |
85 | | - option: { |
86 | | - tooltip: { |
87 | | - show: true, |
88 | | - }, |
89 | | - }, |
90 | | - }, |
91 | | - { |
92 | | - query: { |
93 | | - minWidth: 320, |
94 | | - maxWidth: 400, |
95 | | - }, |
96 | | - option: { |
97 | | - series: [ |
98 | | - { |
99 | | - label: { |
100 | | - fontSize: "12px", |
101 | | - }, |
102 | | - }, |
103 | | - ], |
104 | | - }, |
105 | | - }, |
106 | | - ], |
107 | | - }, |
| 62 | + baseOptions: {media: [{option: {tooltip: {show: true}}}]}, |
108 | 63 | }, |
109 | 64 | bookmarkableActions: { |
110 | 65 | enabled: true, |
111 | | - id: "indoorMap" |
| 66 | + id: "indoorMap", |
112 | 67 | }, |
113 | | - |
114 | 68 | // Convert to internal json format |
115 | 69 | prepareData: function (data) { |
116 | 70 | data.nodes.map((node) => { |
|
121 | 75 | }); |
122 | 76 | }, |
123 | 77 |
|
124 | | - onReady: async function presentIndoormap() { |
125 | | - const netjsonmap = this.leaflet; |
126 | | - const image = new Image(); |
127 | | - const imageUrl = "../assets/images/floorplan.png"; |
128 | | - image.src = imageUrl; |
129 | | - await image.decode() |
130 | | - const aspectRatio = image.width / image.height; |
131 | | - const h = 700; |
132 | | - const w = aspectRatio * h; |
133 | | - const zoom = netjsonmap.getMaxZoom() - 1; |
134 | | - const bottomLeft = netjsonmap.unproject([0, h * 2], zoom); |
135 | | - const upperRight = netjsonmap.unproject([w * 2, 0], zoom); |
136 | | - const bounds = new L.LatLngBounds(bottomLeft, upperRight); |
| 78 | + onReady: async function () { |
| 79 | + const map = this.leaflet; |
| 80 | + // Remove default tile layers |
| 81 | + map.eachLayer((layer) => layer._url && map.removeLayer(layer)); |
137 | 82 |
|
138 | | - L.imageOverlay(imageUrl, bounds).addTo(netjsonmap); |
139 | | - netjsonmap.fitBounds(bounds); |
140 | | - netjsonmap.setMaxBounds(bounds); |
| 83 | + const img = new Image(); |
| 84 | + imageUrl = "../assets/images/floorplan.png"; |
| 85 | + img.src = imageUrl; |
| 86 | + await img.decode(); |
141 | 87 |
|
142 | | - // Remove any default tile layers and show only the floorplan image. |
143 | | - netjsonmap.eachLayer((layer) => { |
144 | | - if (layer._url) { |
145 | | - netjsonmap.removeLayer(layer); |
146 | | - } |
147 | | - }); |
148 | | - }, |
149 | | - }, |
150 | | - ); |
| 88 | + const aspectRatio = img.width / img.height; |
| 89 | + const h = 700; |
| 90 | + const w = h * aspectRatio; |
| 91 | + |
| 92 | + const zoom = map.getMaxZoom() - 1; |
| 93 | + |
| 94 | + // Workaround for https://github.com/openwisp/netjsongraph.js/issues/397 |
| 95 | + // To make the image center in the map at (0,0) coordinates |
| 96 | + const anchorLatLng = L.latLng(0, 0); |
| 97 | + const anchorPoint = map.project(anchorLatLng, zoom); |
151 | 98 |
|
| 99 | + // Calculate the bounds of the image, with respect to the anchor point (0, 0) |
| 100 | + // Leaflet's pixel coordinates increase to the right and downwards |
| 101 | + // Unlike cartesian system where y increases upwards |
| 102 | + // So top-left will have negative y and bottom-right will have positive y |
| 103 | + // Similarly left will have negative x and right will have positive x |
| 104 | + const topLeft = L.point(anchorPoint.x - w / 2, anchorPoint.y - h / 2); |
| 105 | + const bottomRight = L.point(anchorPoint.x + w / 2, anchorPoint.y + h / 2); |
| 106 | + |
| 107 | + // Update node coordinates to fit the image overlay |
| 108 | + // We get the node coordinates from the API in the format for L.CRS.Simple |
| 109 | + // So the coordinates is in for cartesian system with origin at top left corner |
| 110 | + // Rendering image in the third quadrant with topLeft as (0,0) and bottomRight as (w,-h) |
| 111 | + // So we convert py to positive and then project the point to get the corresponding topLeft |
| 112 | + // Then unproject the point to get the corresponding latlng on the map |
| 113 | + const mapOptions = this.echarts.getOption(); |
| 114 | + mapOptions.series[0].data.forEach((data) => { |
| 115 | + const node = data.node; |
| 116 | + const px = Number(node.location.lng); |
| 117 | + const py = -Number(node.location.lat); |
| 118 | + const nodeProjected = L.point(topLeft.x + px, topLeft.y + py); |
| 119 | + const nodeLatLng = map.unproject(nodeProjected, zoom); |
| 120 | + |
| 121 | + node.location = nodeLatLng |
| 122 | + node.properties.location = nodeLatLng; |
| 123 | + data.value = [nodeLatLng.lng, nodeLatLng.lat]; |
| 124 | + }); |
| 125 | + // Similary, updating the coordinates for the links |
| 126 | + mapOptions.series[1].data.forEach((data) => { |
| 127 | + const soruce = data.coords[0]; |
| 128 | + const sourcePx = Number(soruce[0]); |
| 129 | + const sourcePy = -Number(soruce[1]); |
| 130 | + const sourceNodeProjected = L.point(topLeft.x + sourcePx, topLeft.y + sourcePy); |
| 131 | + const sourceNodeLatLng = map.unproject(sourceNodeProjected, zoom); |
| 132 | + |
| 133 | + const target = data.coords[1]; |
| 134 | + const targetPx = Number(target[0]); |
| 135 | + const targetPy = -Number(target[1]); |
| 136 | + const targetNodeProjected = L.point(topLeft.x + targetPx, topLeft.y + targetPy); |
| 137 | + const targetNodeLatLng = map.unproject(targetNodeProjected, zoom); |
| 138 | + |
| 139 | + console.log(soruce, sourceNodeLatLng) |
| 140 | + console.log(target, targetNodeLatLng) |
| 141 | + |
| 142 | + data.coords[0] = [sourceNodeLatLng.lng, sourceNodeLatLng.lat]; |
| 143 | + data.coords[1] = [targetNodeLatLng.lng, targetNodeLatLng.lat]; |
| 144 | + }); |
| 145 | + this.echarts.setOption(mapOptions); |
| 146 | + // Unproject the topLeft and bottomRight points to get northWest and southEast latlngs |
| 147 | + const nw = map.unproject(topLeft, zoom); |
| 148 | + const se = map.unproject(bottomRight, zoom); |
| 149 | + const bnds = L.latLngBounds(nw, se); |
| 150 | + |
| 151 | + L.imageOverlay(imageUrl, bnds).addTo(map); |
| 152 | + map.fitBounds(bnds); |
| 153 | + map.setMaxBounds(bnds); |
| 154 | + }, |
| 155 | + }, |
| 156 | + ); |
152 | 157 | graph.render(); |
153 | 158 | </script> |
154 | 159 | </body> |
|
0 commit comments