-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmap.js
More file actions
292 lines (238 loc) · 8.51 KB
/
map.js
File metadata and controls
292 lines (238 loc) · 8.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
import './map.html'
import './map.css'
import { Template } from 'meteor/templating'
import { Tracker } from 'meteor/tracker';
import * as L from 'leaflet'
import * as d3 from 'd3'
import { $ } from 'meteor/jquery'
import 'leaflet/dist/leaflet.css' // BUG: had to import leaflet with npm meteor install
import { Nodes } from '../../../api/collections.js'
var map, points = []
Template.filterByCategory.onRendered( function() {
console.log("map template rendered")
// init map style / tiles
L.Icon.Default.imagePath = 'packages/bevanhunt_leaflet/images'
var url = 'http://tile.stamen.com/toner/{z}/{x}/{y}.png'
// var url = 'http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png'
var attrib = "Map data © <a href='http://openstreetmap.org'>OpenStreetMap</a> contributors"
console.log(L);
var layer = new L.TileLayer(url, {
minZoom: 1,
maxZoom: 16,
attribution: attrib
})
// create map div
map = L.map('map').setView([35.0, 50.0], 2)
map.addLayer(layer)
// add d3 map overlay
let mapReady = false
function addMapOverlay() {
let mapOverlay = L.d3SvgOverlay(function(selection,projection){
console.log("overlay pl");
selection
.attr("id", "circles")
.selectAll("circle")
.data(points)
.enter()
.append("circle")
.attr("id", function(d) {
return d.id
})
.attr("cx", function(d){ return projection.latLngToLayerPoint(d.latLng).x })
.attr("cy", function(d){ return projection.latLngToLayerPoint(d.latLng).y })
.style("fill", "red")
.attr("r", 10)
console.log(circles);
});
mapOverlay.addTo(map)
mapReady = true
}
self = this
self.autorun(function(auto) {
let network = Template.instance().data.network.get()
// get nodes
var nodes = Nodes.find().fetch()
console.log(nodes.length, 'nodes')
// create map points
points = nodes
.filter(function(node){ // filter out points that does not have lat/lng
return node.data.lat && node.data.lng
})
.map(function(d) {
return {
latLng: new L.LatLng(d.data.lat, d.data.lng),
id: d.data.id
}
})
console.log(points.length, "points on the map")
if(points.length && !mapReady) {
addMapOverlay()
mapReady = true
}
console.log(network);
if(points.length && network) {
// disable network mouse events and zoom
$("#network").css({ "pointer-events" : "none" })
network.zoomingEnabled( false )
map.on("moveend", function(e){
updateNetwork(network)
})
// (init) place correctly on map
updateNetwork(network)
// bind events
// network.on("pan", function(e){
// console.log("pan");
// updateMap()
// })
}
});
})
function updateMap() {
// console.log('update map');
// fitBounds
}
function getCoordinates() {
let pos = {}
let circles = d3.select("#circles")
.selectAll('circle')
circles[0]
.forEach(function(d){
if (d) {
let box = d.getBoundingClientRect()
pos[d.id] = {
x : box.left + box.width/2,
y : box.top + box.height/2
}
}
})
return pos
}
function updateNetwork(network) {
var pos = getCoordinates()
network.nodes().positions(function(i, node){
return pos[i]
})
}
/**
* Copyright 2015 Teralytics AG
*
* @author Kirill Zhuravlev <[email protected]>
*
*/
L.D3SvgOverlay = (L.version < "1.0" ? L.Class : L.Layer).extend({
includes: (L.version < "1.0" ? L.Mixin.Events : []),
_undef: function(a){ return typeof a == "undefined" },
_options: function (options) {
if (this._undef(options)) {
return this.options;
}
options.zoomHide = this._undef(options.zoomHide) ? false : options.zoomHide;
options.zoomDraw = this._undef(options.zoomDraw) ? true : options.zoomDraw;
return this.options = options;
},
_disableLeafletRounding: function(){
this._leaflet_round = L.Point.prototype._round;
L.Point.prototype._round = function(){ return this; };
},
_enableLeafletRounding: function(){
L.Point.prototype._round = this._leaflet_round;
},
draw: function () {
this._disableLeafletRounding();
this._drawCallback(this.selection, this.projection, this.map.getZoom());
this._enableLeafletRounding();
},
initialize: function (drawCallback, options) { // (Function(selection, projection)), (Object)options
this._options(options || {});
this._drawCallback = drawCallback;
},
// Handler for "viewreset"-like events, updates scale and shift after the animation
_zoomChange: function (evt) {
this._disableLeafletRounding();
var newZoom = this._undef(evt.zoom) ? this.map._zoom : evt.zoom; // "viewreset" event in Leaflet has not zoom/center parameters like zoomanim
this._zoomDiff = newZoom - this._zoom;
this._scale = Math.pow(2, this._zoomDiff);
this.projection.scale = this._scale;
this._shift = this.map.latLngToLayerPoint(this._wgsOrigin)
._subtract(this._wgsInitialShift.multiplyBy(this._scale));
var shift = ["translate(", this._shift.x, ",", this._shift.y, ") "];
var scale = ["scale(", this._scale, ",", this._scale,") "];
this._rootGroup.attr("transform", shift.concat(scale).join(""));
if (this.options.zoomDraw) { this.draw() }
this._enableLeafletRounding();
},
onAdd: function (map) {
this.map = map;
var _layer = this;
// SVG element
if (L.version < "1.0") {
map._initPathRoot();
this._svg = d3.select(map._panes.overlayPane)
.select("svg");
this._rootGroup = this._svg.append("g");
} else {
this._svg = L.svg();
map.addLayer(this._svg);
this._rootGroup = d3.select(this._svg._rootGroup).classed("d3-overlay", true);
}
this._rootGroup.classed("leaflet-zoom-hide", this.options.zoomHide);
this.selection = this._rootGroup;
// Init shift/scale invariance helper values
this._pixelOrigin = map.getPixelOrigin();
this._wgsOrigin = L.latLng([0, 0]);
this._wgsInitialShift = this.map.latLngToLayerPoint(this._wgsOrigin);
this._zoom = this.map.getZoom();
this._shift = L.point(0, 0);
this._scale = 1;
// Create projection object
this.projection = {
latLngToLayerPoint: function (latLng, zoom) {
zoom = _layer._undef(zoom) ? _layer._zoom : zoom;
var projectedPoint = _layer.map.project(L.latLng(latLng), zoom)._round();
return projectedPoint._subtract(_layer._pixelOrigin);
},
layerPointToLatLng: function (point, zoom) {
zoom = _layer._undef(zoom) ? _layer._zoom : zoom;
var projectedPoint = L.point(point).add(_layer._pixelOrigin);
return _layer.map.unproject(projectedPoint, zoom);
},
unitsPerMeter: 256 * Math.pow(2, _layer._zoom) / 40075017,
map: _layer.map,
layer: _layer,
scale: 1
};
this.projection._projectPoint = function(x, y) {
var point = _layer.projection.latLngToLayerPoint(new L.LatLng(y, x));
this.stream.point(point.x, point.y);
};
this.projection.pathFromGeojson =
d3.geo.path().projection(d3.geo.transform({point: this.projection._projectPoint}));
// Compatibility with v.1
this.projection.latLngToLayerFloatPoint = this.projection.latLngToLayerPoint;
this.projection.getZoom = this.map.getZoom.bind(this.map);
this.projection.getBounds = this.map.getBounds.bind(this.map);
this.selection = this._rootGroup;
if (L.version < "1.0") map.on("viewreset", this._zoomChange, this);
// Initial draw
this.draw();
},
// Leaflet 1.0
getEvents: function() { return {zoomend: this._zoomChange}; },
onRemove: function (map) {
if (L.version < "1.0") {
map.off("viewreset", this._zoomChange, this);
this._rootGroup.remove();
} else {
this._svg.remove();
}
},
addTo: function (map) {
map.addLayer(this);
return this;
}
});
L.D3SvgOverlay.version = "2.2";
// Factory method
L.d3SvgOverlay = function (drawCallback, options) {
return new L.D3SvgOverlay(drawCallback, options);
};