-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpcars_map_gpssensor.js
180 lines (142 loc) · 6.86 KB
/
pcars_map_gpssensor.js
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
function GPSSensor(initData) {
// constructor(initData) {
//state information
var _div = null;
var _data = initData;
var _projection = null;
// this._div = null;
// this._data = initData;
// this._projection = null;
// return this;
// }
function transform(d) {
// transform(d) {
var padding = 10;
d = new google.maps.LatLng(d.Lat, d.Long);
d = _projection.fromLatLngToDivPixel(d);
return d3.select(this)
.style("left", (d.x - padding) + "px")
.style("top", (d.y - padding) + "px");
}
function transformWithEase(d) {
// transformWithEase(d) {
var padding = 10;
d = new google.maps.LatLng(d.Lat, d.Long);
d = _projection.fromLatLngToDivPixel(d);
return d3.select(this)
.transition().duration(DisplayDuration)
.style("left", (d.x - padding) + "px")
.style("top", (d.y - padding) + "px");
}
//superclass methods for google maps
this.onAdd = function() {
// onAdd (){
_div = d3.select(this.getPanes().overlayLayer)
.append("div")
.attr("class", "stations");
}
this.draw = function () {
// draw(){
var padding = 10;
_projection = this.getProjection();
// the draw function is triggered by Google Maps events like zoom_changed, dragend, etc
// in this case marker must be updated extra, independent from the normal worker run
if(aSensorData) {this.update(aSensorData);}
}
this.onRemove = function () {
// onRemove () {
_div.remove();
}
// todo: known issue: Beim Wechsel der Position, bleibt eine "Leiche" auf der Karte uebrig, weil Key "Pos - Name" zusammensetzt
// easyt solution: deleting leading position number
this.update = function (data) {
// update (data) {
if (!_div){ return 1;} // prevent situation where _div is undefined
var marker;
var padding = 10;
//update internal data which drive redrawing on zoom_changed
if(log >= 3){console.log("TODO GPSSensor; data: ", data);}
_data = data.slice();
//Detect time between StopTransitionDelay set to "true" and now
// disabled because of a complete login overhaul - Issue #164
//var CurrentTime = Date.now();
//var StopTransitionDelay_TimeRun = CurrentTime - StopTransitionDelay_StartTime;
if(log >= 3){console.log("StopTransitionDelay: ",StopTransitionDelay, " / dsdata_workerrun_counter: ", dsdata_workerrun_counter);}
if( StopTransitionDelay == "true"){
marker = _div.selectAll(".stations, svg")
.data(_data, function (d) { return d.Key; })
.each(transform) // update existing markers
.enter().append("svg:svg")
.each(transform)
.attr("class", function (d){ return d.CSSTextClasses });
//reset to default after zoom event finished.
//A zoom change in Google Maps takes some time. If the worker runs have a short interval you need a minimum time where the transformWithEase have to be interrupted
//if (StopTransitionDelay_TimeRun > StopTransitionDelay_minTimeRun) {StopTransitionDelay = "false";} // disabled because of a complete login overhaul - Issue #164
}else{
marker = _div.selectAll(".stations, svg")
.data(_data, function (d) { return d.Key; })
.each(transformWithEase) // update existing markers
.enter().append("svg:svg")
.each(transform)
.attr("class", function (d){ return d.CSSTextClasses });
//reset to default after zoom event finished
//StopTransitionDelay = "false";
}
// Add a circle.
marker.append("svg:circle")
.attr("r", 4.5)
.attr("cx", padding)
.attr("cy", padding)
.attr("class", function (d){ return d.CSSCircleClasses } );
// Add a label.
marker.append("svg:text")
.attr("x", padding + 7)
.attr("y", padding)
.attr("dy", ".31em")
//todo !!!!!!!!!!!!!!!!!!!! very uggly implementation !!! only temporary workaround
// it seem that the function within .text(...) will called only once at the beginning
// question: is it possible to call _div.selectAll("svg") and set text style direct in loop() ??
.text(function (d) {
//if(log >= 4){console.log("+++++ d: " , d );}
for (var i = 0; i < aSensorData.length; i++){
if (aSensorData[i].Key == d.Key){
//if(log >= 3){console.log("Match found ---- Update MarkerLabel: " , aSensorData[i].MarkerLabel);}
return aSensorData[i].MarkerLabel;
}
}
return d.MarkerLabel;
})
// Update CSS classes new
var svgs = _div.selectAll("circle")
.data(_data, function (d){ return d.Key; })
.attr("class" ,(function(d){ return d.CSSCircleClasses; }));
// Update labels new
var svgs = _div.selectAll("text")
.data(_data, function (d){ return d.Key; })
.text(function(d){ return d.MarkerLabel; });
// delete unneeded svg objects from dom tree
this.CleanupDriverObjects(_data);
} //end update()
this.CleanupDriverObjects = function(data) {
// CleanupDriverObjects (data) {
// remapping of array of drivers to array of key
var aTmp = {};
for ( i = 0; i < data.length; i++) {
aTmp[data[i].Key] = "";
}
d3.selectAll("svg, .driverlabel")
.filter(".driverlabel") // needed to prevent action to other svg in this project like LapChart
.each(function( d ) {
if ( aTmp[d.Key] == undefined ) {
this.remove(); // delete svg object / ghots cars
}
});
} // end CleanupDriverObjects()
this.interruptTransition = function (){
// interruptTransition (){
// interrupt transition while zoom event
if(_div != null){
_div.selectAll("svg, .driverlabel").interrupt();
}
} // end interruptTransition()
}; // end GPSSensor(initData)