-
Notifications
You must be signed in to change notification settings - Fork 1
/
map2.js
358 lines (269 loc) · 7.87 KB
/
map2.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
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
var map_config = {
path : '/tiles/s2b/',
fileExt : 'png',
tileSize : 256,
defaultZoom : 3,
maxZoom : 3,
cacheMinutes : 0,
debug : false
};
var weblog_counts = {};
var accept_station_clicks = true;
var g_this_station = null;
var g_marker_image = null;
//
// a very simple projection
//
function LBMapProjection(){}
LBMapProjection.prototype.fromLatLngToPoint = function(latLng){
var x = latLng.lng() * map_config.tileSize;
var y = latLng.lat() * map_config.tileSize;
return new google.maps.Point(x, y);
};
LBMapProjection.prototype.fromPointToLatLng = function(point){
var lng = point.x * (1.0 / map_config.tileSize);
var lat = point.y * (1.0 / map_config.tileSize);
return new google.maps.LatLng(lat, lng);
};
function LatLngToPixels(latLng){
var pnt = LBMapProjection.prototype.fromLatLngToPoint(latLng);
return [pnt.x * 8, pnt.y * 8];
}
function PixelsToLatLng(pxs){
var pnt = {x: pxs[0] / 8, y: pxs[1] / 8};
return LBMapProjection.prototype.fromPointToLatLng(pnt);
}
//
// the class for our map layer
//
//Zoom map
//0 -> 4
//1 -> 3
//2 -> 2
//3 -> 1
var LBMapOptions = {
getTileUrl: function(tile, zoom) {
if (tile.x < 0 || tile.y < 0) return null;
if ((zoom == 3) && (tile.x > 15 || tile.y > 12)) return null;
if ((zoom == 2) && (tile.x > 7 || tile.y > 6)) return null;
if ((zoom == 1) && (tile.x > 3 || tile.y > 3)) return null;
if ((zoom == 0) && (tile.x > 1 || tile.y > 1)) return null;
var tx = ""+tile.x;
var ty = ""+tile.y;
while (tx.length < 3) tx = "0"+tx;
while (ty.length < 3) ty = "0"+ty;
var url = map_config.path+"tile_"+zoom+"_"+tx+"_"+ty+".jpg";
return url;
},
tileSize: new google.maps.Size(map_config.tileSize, map_config.tileSize),
maxZoom: map_config.maxZoom,
minZoom: 0,
isPng: false
};
var LBMapType = new google.maps.ImageMapType(LBMapOptions);
LBMapType.name = "LB Map";
LBMapType.alt = "London Bloggers Map";
LBMapType.projection = new LBMapProjection();
//
// the debug overlay
//
function CoordMapType(){
}
function CoordMapType(tileSize){
this.tileSize = tileSize;
}
CoordMapType.prototype.getTile = function(coord, zoom, ownerDocument) {
var div = ownerDocument.createElement('DIV');
div.innerHTML = "(" + coord.x + ", " + coord.y + ", " + zoom + ")";
div.innerHTML += "<br />";
div.innerHTML += LBMapOptions.getTileUrl(coord, zoom);
div.style.width = this.tileSize.width + 'px';
div.style.height = this.tileSize.height + 'px';
div.style.fontSize = '10';
div.style.borderStyle = 'solid';
div.style.borderWidth = '1px';
div.style.borderColor = '#AAAAAA';
return div;
};
//
// startup code
//
var map;
var info_window;
var markers = [];
function initialize() {
var mapOptions = {
backgroundColor: '#eee',
zoom: map_config.defaultZoom,
center: new google.maps.LatLng(0.25, 0.25),
navigationControl: true,
navigationControlOptions: { style: google.maps.NavigationControlStyle.SMALL }, // still no way to get rid of the dude
scaleControl: false,
mapTypeControl: false,
streetViewControl: false,
mapTypeId: 'LBmap'
};
map = new google.maps.Map(document.getElementById("map"), mapOptions);
if(map_config.debug) {
map.overlayMapTypes.insertAt(0, new CoordMapType(new google.maps.Size(map_config.tileSize, map_config.tileSize)));
google.maps.event.addListener(map, 'click', function(event) {
console.log("latLng; " + event.latLng.lat() + ", " + event.latLng.lng());
var pnt = map.getProjection().fromLatLngToPoint(event.latLng);
console.log("point: ", pnt);
var pxx = (pnt.x * 8) - 256;
var pxy = (pnt.y * 8) - 256;
console.log("pixel: " + pxx + ", " + pxy);
});
}
// Now attach the coordinate map type to the map's registry
map.mapTypes.set('LBmap', LBMapType);
// We can now set the map to use the 'coordinate' map type
map.setMapTypeId('LBmap');
// we'll use this info window later
info_window = new google.maps.InfoWindow();
// click hook
google.maps.event.addListener(map, 'click', function(event){
var p = LatLngToPixels(event.latLng);
process_click(p[0], p[1]);
});
}
function process_click(x, y){
if (!accept_station_clicks) return;
// find any matching boxes
var matches = [];
for (var i=0; i<g_click_boxes.length; i++){
var box = g_click_boxes[i];
if (x >= box[0] && y >= box[1] && x <= box[2] && y <= box[3]){
matches.push(box);
}
}
if (!matches.length) return;
//
// find shortest distance to each station
//
var distances = {};
for (var i=0; i<matches.length; i++){
var cx = matches[i][0] + ((matches[i][2] - matches[i][0]) / 2);
var cy = matches[i][1] + ((matches[i][3] - matches[i][1]) / 2);
var dx = Math.abs(x - cx);
var dy = Math.abs(y - cy);
var d = Math.sqrt(dx * dx + dy * dy);
if (!distances[matches[i][4]] || d < distances[matches[i][4]]){
distances[matches[i][4]] = d;
}
}
//
// pick the closest station
//
var best_d = 1000;
var best_id = null;
for (var i in distances){
if (distances[i] < best_d){
best_d = distances[i];
best_id = i;
}
}
if (!best_id) return;
select_station(best_id, true);
}
function select_station(id){
g_selected_station = id;
var station = g_stations[id];
//
// open window
//
var ll = PixelsToLatLng([station.x, station.y]);
map.panTo(ll);
info_window.close();
info_window.setPosition(ll);
set_station_html(id);
info_window.open(map);
if (!weblog_counts[id]){
api_call('weblog_count', { 'station' : id }, function(o){
weblog_counts[o.station] = o.count == 0 ? 'z' : o.count;
if (o.station == g_selected_station){
set_station_html(g_selected_station);
}
});
}
}
function set_station_html(id){
var station = g_stations[id];
if (weblog_counts[id]){
var count = weblog_counts[id] == 'z' ? 0 : weblog_counts[id];
var weblogs = count==1 ? 'weblog' : 'weblogs';
if (g_this_station == id){
if (weblog_counts[id] > 1){
info_window.setContent("<b>"+station.name+"</b><br /><br />View <a href=\"/stations/"+id+"/all/\">all "+count+" "+weblogs+"</a>");
}else{
info_window.setContent("<b>"+station.name+"</b><br /><br />Be the first to <a href=\"/stations/"+id+"/add/\">add your weblog</a>");
}
}else{
info_window.setContent("<b>"+station.name+"</b><br /><br />Home to <a href=\"/stations/"+id+"/\">"+count+" "+weblogs+"</a>");
}
}else{
info_window.setContent("<b>"+station.name+"</b><br /><br />...");
}
}
function get_station_info(id){
return g_stations[id];
}
function clear_markers(){
for (var i=0; i<markers.length; i++){
markers[i].setMap(null);
}
markers = [];
}
function create_markers(stations){
clear_markers();
var xs = [];
var ys = [];
var marker_img = new google.maps.MarkerImage(g_marker_image);
var shadow_img = new google.maps.MarkerImage('http://chart.apis.google.com/chart?chst=d_map_pin_shadow');
for (var i=0; i<stations.length; i++){
var id = stations[i];
var station = get_station_info(id);
xs.push(parseInt(station.x));
ys.push(parseInt(station.y));
var marker = new google.maps.Marker({
position: PixelsToLatLng([station.x, station.y]),
map: map,
title: station.name
});
if (g_marker_image){
marker.setIcon(marker_img);
marker.setShadow(shadow_img);
}
google.maps.event.addListener(marker, 'click', function(){
var cid = id;
var cmarker = marker;
return function(event){
marker_click(cid, cmarker);
};
}());
markers.push(marker);
}
xs.sort(function(a,b){return a-b});
ys.sort(function(a,b){return a-b});
var lo_x = xs[0];
var lo_y = ys[0];
var hi_x = xs.pop();
var hi_y = ys.pop();
//
// for large marker sets (lines), we will pull the edges in to avoid fitToBounds()
// picking a zoom level too far out. or google could just fix this...
//
if (hi_x - lo_x > 1000){
lo_x += 200;
hi_x -= 200;
}
if (hi_y - lo_y > 1000){
lo_y += 200;
hi_y -= 200;
}
var bounds = new google.maps.LatLngBounds(PixelsToLatLng([lo_x, hi_y]), PixelsToLatLng([hi_x, lo_y]));
map.fitBounds(bounds);
}
function marker_click(id, marker){
select_station(id);
}