-
Notifications
You must be signed in to change notification settings - Fork 0
/
map.js
241 lines (207 loc) · 6.92 KB
/
map.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
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
MAP_COLORS = new Array(5)
BLACK = 'c0c0c080'
ROUTE_ERR_START = 100000000
var sourceCoords = [55.733, 37.587];
var routingMode = 'masstransit'
var myMap = null
var sourcePoint = null;
var routeTypeSelector = null;
var districts = null
var DurationHintLayout = null
function GetCSSBackgroundColor(elementId) {
var domElement = document.getElementById(elementId);
return window.getComputedStyle(domElement).getPropertyValue('background-color');
}
function LoadConfig(configData) {
var config = configData.configs[configData.current];
CITY_TL = [config.area[0], config.area[1]]
CITY_BR = [config.area[2], config.area[3]]
LAT_OFFSET = config.lat_offset
LONG_OFFSET = config.long_offset
LONG_COUNT = Math.floor((CITY_BR[1] - CITY_TL[1]) / LONG_OFFSET);
FILE_DIR = 'routes/' + configData.current;
DISTRICTS_JSON = 'routes/' + configData.current + '/ymaps.geojson'
}
function InitUIBindings() {
myMap.events.add('click', OnMapClick);
// TODO(kirr): work with lists right
var masstransitRouteItem = routeTypeSelector.get(0)
var autoRouteItem = routeTypeSelector.get(1)
autoRouteItem.events.add('click',
OnChangeRoutingMode.bind(null, 'auto', autoRouteItem));
masstransitRouteItem.events.add('click',
OnChangeRoutingMode.bind(null, 'masstransit', masstransitRouteItem));
}
function LoadJSON(url, success) {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState === XMLHttpRequest.DONE) {
if (xhr.status === 200) {
success(xhr.responseText)
} else {
console.log('Error in json reuest:' + xhr.statusText);
}
}
};
xhr.open("GET", url, true);
xhr.setRequestHeader('Cache-Control', 'no-cache');
xhr.send();
}
function AddPolygon(name, vertexes) {
var outer_coords = vertexes[0].map(
function(coords) { return [coords[1], coords[0]]; });
var inner_coords = []
if (vertexes[1]) {
inner_coords = vertexes[1].map(
function(coords) { return [coords[0], coords[1]]; });
}
var mapPoly = new ymaps.Polygon(
[outer_coords, inner_coords],
{hintContent: name},
{
fillColor: '#6699ff',
interactivityModel: 'default#transparent',
strokeWidth: 2,
opacity: 0.5
});
districts[name].geometry.push(mapPoly)
myMap.geoObjects.add(mapPoly);
}
function LoadDistricts(districts_json_text) {
json = JSON.parse(districts_json_text);
districts = {}
for (var i = 0; i < json.features.length; ++i) {
var district = json.features[i];
var districtName = district.properties.NAME;
districts[districtName] = {geometry:[], index:district.properties.index};
if (district.geometry.type == 'Polygon')
AddPolygon(districtName, district.geometry.coordinates);
else if (district.geometry.type == 'MultiPolygon') {
for (var j = 0; j < district.geometry.coordinates.length; ++j)
AddPolygon(districtName, district.geometry.coordinates[j]);
}
}
InitUIBindings();
UpdateAccessibilityMap();
}
function QuadCoordsById(id) {
var i = Math.floor(id / LONG_COUNT);
var j = id % LONG_COUNT;
var lat = CITY_BR[0] + i*LAT_OFFSET + 0.5 * LAT_OFFSET;
var long = CITY_TL[1] + j*LONG_OFFSET + 0.5 * LONG_OFFSET;
return [lat, long];
}
function QuadIdByCoords(coords) {
var latInd = Math.floor((coords[0] - CITY_BR[0])/LAT_OFFSET);
var longInd = Math.floor((coords[1] - CITY_TL[1])/LONG_OFFSET);
return longInd + LONG_COUNT * latInd;
}
function ColorForDuration(info) {
if (!info || !info.points_count)
return BLACK;
var d = info.sum / (info.points_count*60);
if (d < 15)
return MAP_COLORS[0];
else if (d < 30)
return MAP_COLORS[1];
else if (d < 45)
return MAP_COLORS[2];
else if (d < 60)
return MAP_COLORS[3];
return MAP_COLORS[4];
}
function UpdateAccessibilityMap() {
var sourceId = QuadIdByCoords(sourceCoords);
RequestRoutes(sourceId);
}
function RequestRoutes(sourceId) {
var routeFilePath =
FILE_DIR + '/' + routingMode + '/' + sourceId + '_time.json';
var req = new XMLHttpRequest();
req.open("GET", routeFilePath, true);
req.setRequestHeader('Cache-Control', 'no-cache');
req.onload = function (oEvent) {
if (req.status != 200) {
console.log(req.status + ': ' + req.statusText);
} else {
var durations = JSON.parse(req.responseText)
for (var d in districts) {
var district = districts[d];
var info = district.index.reduce(
function(pv, cv) {
var d = durations[cv];
if (d >= ROUTE_ERR_START)
return pv;
pv.max = Math.max(pv.max, d);
pv.min = Math.min(pv.min, d);
pv.sum = pv.sum + d;
++pv.points_count;
return pv;
}, {min:ROUTE_ERR_START, max:0, sum:0, points_count:0});
for (var i = 0; i < district.geometry.length; ++i) {
var poly = district.geometry[i];
poly.properties.set({
name:d,
duration_min:Math.floor(info.min/60),
duration_max:Math.floor(info.max/60)});
poly.options.set('fillColor', ColorForDuration(info));
poly.options.set('hintContentLayout', DurationHintLayout);
}
}
}
};
req.send(null);
}
function OnMapClick(e) {
sourceCoords = e.get('coords');
sourcePoint.geometry.setCoordinates(sourceCoords);
UpdateAccessibilityMap();
}
function OnChangeRoutingMode(newRoutingMode, selectedItem) {
if (newRoutingMode != routingMode) {
routingMode = newRoutingMode;
UpdateAccessibilityMap();
}
routeTypeSelector.data.set('content', selectedItem.data.get('content'));
routeTypeSelector.collapse();
}
ymaps.ready(function () {
myMap = new ymaps.Map('map', {
center: sourceCoords,
zoom: 11,
}, {
searchControlResults: 3,
searchControlNoCentering: true,
buttonMaxWidth: 150
});
routeTypeSelector = new ymaps.control.ListBox({
data: {
content: 'Общественный транспорт'
},
items: [
new ymaps.control.ListBoxItem('Общественный транспорт'),
new ymaps.control.ListBoxItem('Автомобиль')
],
options: {
itemSelectOnClick: false
}
});
myMap.controls.add(routeTypeSelector)
for (var i = 0; i < MAP_COLORS.length; ++i)
MAP_COLORS[i] = GetCSSBackgroundColor('map-color' + (i+1))
sourcePoint = new ymaps.Placemark(
sourceCoords,
{},
{iconLayout: 'default#image',
iconImageHref:'home.png',
iconImageSize:[60,70],
iconShape:{type:'Rectangle', coordinates:[[-15,-45], [15,0]]},
iconImageOffset:[-30, -45]});
myMap.geoObjects.add(sourcePoint);
DurationHintLayout = ymaps.templateLayoutFactory.createClass(
'<p><b>{{properties.name}}.</b></p><p>Время поездки: {{properties.duration_min}} - {{properties.duration_max}} минут.</p>');
LoadJSON('config.json', function(responseText){
LoadConfig(JSON.parse(responseText));
LoadJSON(DISTRICTS_JSON, LoadDistricts)
})
});