-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmap.js
112 lines (95 loc) · 3.36 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
var mapLocationInUrl = location.hash != "";
var po = org.polymaps;
var loadedMaps = [];
var map = null;
var compass = null;
var graticules = null;
function openmaps() {
addBasemaps(["migurskiFull", "migurskiBg", "mapboxWarden", "openCyclemap", "topOSM"]);
}
function othermaps() {
addBasemaps(["googleTerrain", "bingTerrainLabels", "bing838", "googleStreet", "googleSatellite"]);
}
// Utility function for adding a list of base maps to the map
function addBasemaps(basemaps) {
for (var i = 0; i < basemaps.length; i++) {
var bm = po.basemaps[basemaps[i]];
if (bm && bm.map) { map.add(po.basemaps[basemaps[i]]); }
loadedMaps.push(basemaps[i]);
}
// Add the decorations after all the base maps, so they show on top
map.add(po.graticules());
map.add(po.compass());
// Add the HTML buttons to switch layers
addButtons();
}
// Allow graticules to be hidden
graticulesHidden = false;
function toggleGraticules() {
graticulesHidden = !graticulesHidden;
d3.selectAll(".graticules").style("display", graticulesHidden ? "none" : null);
}
// Utility function to add HTML buttons to map
function addButtons() {
var buttonData = [];
for (var i = 0; i < loadedMaps.length; i++) {
var map = po.basemaps[loadedMaps[i]];
buttonData.push({ t: map.source.desc,
f: map.switchTo });
}
buttonData.push({ t: " ",
c: "spacer" });
buttonData.push({ t: "Toggle graticules",
f: toggleGraticules });
// Create some clickable buttons for the baselayers
d3.selectAll("#buttonbox").selectAll("div").remove();
d3.selectAll("#buttonbox")
.selectAll("div")
.data(buttonData, function(d) { return d.t; })
.enter()
.append("div")
.html(function(d) { return d.t })
.attr("class", function(d) { if (d.c) { return d.c; } })
.on("click", function(d) { if (d.f) { return d.f(); } });
// Allow the button box to be hidden
var buttonBoxHidden = false;
d3.select("#button-control").on("click", function(d) {
buttonBoxHidden = !buttonBoxHidden;
d3.select("#buttonbox").style("display", buttonBoxHidden ? "none" : null); });
}
// Some key bindings for advanced users
// 1-9 switches map layer. g toggles graticules
function keyboardHandler(e) {
if (e.keyCode == 71) {
toggleGraticules();
} else if (e.keyCode >= 49 && e.keyCode <= 57) {
var selectedMap = e.keyCode - 49;
if (selectedMap < loadedMaps.length) {
po.basemaps[loadedMaps[selectedMap]].switchTo();
}
}
}
// Top level entry: make the map
function makeMap() {
// Construct our map object
map = po.map()
.container(document.getElementById("map").appendChild(po.svg("svg")))
.add(po.dblclick())
.add(po.drag())
.add(po.arrow())
.add(po.wheel().smooth(false))
.add(po.hash())
.add(po.touch().rotate(false));
// Center the map on the SF Bay Area if a location isn't specified
if (!mapLocationInUrl) {
map.center({lon: -122.2, lat: 37.8});
map.zoom(10);
}
// Add some base maps
openmaps();
othermaps();
// Choose which map is visible first
po.basemaps.migurskiFull.switchTo();
// Bind some keyboard events
window.addEventListener("keydown", keyboardHandler, true);
}