-
Notifications
You must be signed in to change notification settings - Fork 1
/
map.js
176 lines (130 loc) · 3.94 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
d3.select(window).on("resize", throttle);
// var zoom = d3.behavior.zoom()
// .scaleExtent([1, 9])
// .on("zoom", move);
var width = document.getElementById('location__map').offsetWidth;
var height = width/2;
var topo,projection,path,svg,g;
var graticule = d3.geo.graticule();
var tooltip = d3.select("#location__map").append("div").attr("class", "tooltip hidden");
setup(width,height);
function setup(width,height){
projection = d3.geo.mercator()
.translate([(width/2), (height/1.4)])
.scale( width / 2.5/ Math.PI);
path = d3.geo.path().projection(projection);
svg = d3.select("#location__map").append("svg")
.attr("width", width)
.attr("height", height + 8)
// .call(zoom)
.on("click", click)
.append("g");
g = svg.append("g");
}
d3.json("https://api.github.com/gists/9398333", function(error, root) {
var world = root.files['world.json'].content
world = JSON.parse(world)
var countries = topojson.feature(world, world.objects.countries).features;
topo = countries;
draw(topo);
});
function draw(topo) {
/*
svg.append("path")
.datum(graticule)
.attr("class", "graticule")
.attr("d", path);
*/
/*
g.append("path")
.datum({type: "LineString", coordinates: [[-180, 0], [-90, 0], [0, 0], [90, 0], [180, 0]]})
.attr("class", "equator")
.attr("d", path);
*/
var country = g.selectAll(".country").data(topo);
country.enter().insert("path")
.attr("class", "country")
.attr("d", path)
.attr("id", function(d,i) { return d.id; })
.attr("title", function(d,i) { return d.properties.name; })
.style("fill", function(d, i) { return "#BBDEFB";return d.properties.color; });
//offsets for tooltips
var offsetL = document.getElementById('location__map').offsetLeft+20;
var offsetT = document.getElementById('location__map').offsetTop+10;
//tooltips
country
.on("mousemove", function(d,i) {
var mouse = d3.mouse(svg.node()).map( function(d) { return parseInt(d); } );
tooltip.classed("hidden", false)
.attr("style", "left:"+(mouse[0]+offsetL)+"px;top:"+(mouse[1]+offsetT)+"px")
.html(d.properties.name);
})
.on("mouseout", function(d,i) {
tooltip.classed("hidden", true);
});
$.getJSON( "http://smart-ip.net/geoip-json?callback=?",
function(data){
console.log( data);
addpoint(data.longitude, data.latitude, data.city);
$("span.city").html(data.city);
}
);
}
function redraw() {
width = document.getElementById('location__map').offsetWidth;
height = width / 2;
d3.select('svg').remove();
setup(width,height);
draw(topo);
}
function move() {
var t = d3.event.translate;
var s = d3.event.scale;
zscale = s;
var h = height/4;
t[0] = Math.min(
(width/height) * (s - 1),
Math.max( width * (1 - s), t[0] )
);
t[1] = Math.min(
h * (s - 1) + h * s,
Math.max(height * (1 - s) - h * s, t[1])
);
// zoom.translate(t);
g.attr("transform", "translate(" + t + ")scale(" + s + ")");
//adjust the country hover stroke width based on zoom level
d3.selectAll(".country").style("stroke-width", 1.5 / s);
}
var throttleTimer;
function throttle() {
window.clearTimeout(throttleTimer);
throttleTimer = window.setTimeout(function() {
redraw();
}, 200);
}
//geo translation on mouse click in map
function click() {
var latlon = projection.invert(d3.mouse(this));
console.log(latlon);
}
//function to add points and text to the map (used in plotting capitals)
function addpoint(longitude, latitude, text) {
var gpoint = g.append("g").attr("class", "gpoint");
var x = projection([longitude, latitude])[0];
var y = projection([longitude, latitude])[1];
gpoint.append("svg:circle")
.attr("cx", x)
.attr("cy", y)
.attr("class","point")
.attr("r", 2)
.style("fill", "#fff");
//conditional in case a point has no associated text
if(text.length>0){
gpoint.append("text")
.attr("x", x+2)
.attr("y", y+2)
.attr("class","text")
.text(text)
.style("fill", "#fff");
}
}