Skip to content
This repository was archived by the owner on Jun 9, 2020. It is now read-only.

Commit 5e88f3b

Browse files
committed
Add Canada to map
1 parent e5fafc7 commit 5e88f3b

File tree

3 files changed

+57
-112
lines changed

3 files changed

+57
-112
lines changed

public/assets/topo/ca.json

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.

public/assets/topo/us-ca.json

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.

resources/views/global.twig

Lines changed: 55 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -74,42 +74,10 @@
7474
</article>
7575
{% endblock %}
7676
{% block scripts %}
77-
<script src="/assets/js/d3.v3.min.js"></script>
78-
<script src="/assets/js/topojson.v1.min.js"></script>
77+
<script src="http://d3js.org/d3.v3.min.js"></script>
78+
<script src="http://d3js.org/topojson.v1.min.js"></script>
7979
<script src="/assets/js/d3.tip.js"></script>
8080
<script>
81-
82-
// Location
83-
var LocationTools = {
84-
degToRad: function(deg){
85-
return deg * Math.PI / 180;
86-
},
87-
pythagorasEquirectangular: function(lat, long, lat2, long2){
88-
lat = LocationTools.degToRad(lat);
89-
lat2 = LocationTools.degToRad(lat2);
90-
long = LocationTools.degToRad(long);
91-
long2 = LocationTools.degToRad(long2);
92-
var R = 6371; // km
93-
var x = (long2-long) * Math.cos((lat+lat2)/2);
94-
var y = (lat2-lat);
95-
var d = Math.sqrt(x*x + y*y) * R;
96-
return d;
97-
},
98-
nearestRegion: function(cities, lat, long){
99-
var mindif = 99999;
100-
var closest;
101-
102-
for (index = 0; index < cities.length; ++index) {
103-
var dif = LocationTools.pythagorasEquirectangular(lat, long, cities[index].lat, cities[index].lng);
104-
if (dif < mindif){
105-
closest = index;
106-
mindif = dif;
107-
}
108-
}
109-
return cities[closest];
110-
}
111-
}
112-
11381
batch_regions = [
11482
{% set is_first = true %}
11583
{% for region in batch_regions %}
@@ -124,49 +92,46 @@
12492
{% endfor %}
12593
];
12694
127-
usCached = null;
128-
129-
var updateMap = function(us){
130-
var path = d3.geo.path();
131-
132-
var map = $('#map').html('<svg></svg>');
133-
var width = map.width();
134-
var height = map.width() * 0.5;
135-
136-
var projection = d3.geo.albersUsa()
137-
.scale((width))
138-
.translate([width / 2, height / 2]);
139-
140-
var path = d3.geo.path()
141-
.projection(projection);
142-
143-
var svg = d3.select("#map svg")
144-
.attr("width", width)
145-
.attr("height", height);
146-
147-
var tip = d3.tip()
148-
.attr('class', 'd3-tip')
149-
.offset([-10, 0])
150-
.html(function(d) {
151-
return d.name;
152-
});
153-
svg.call(tip);
154-
155-
svg.insert("path", ".graticule")
156-
.datum(topojson.feature(us, us.objects.land))
157-
.attr("class", "land")
158-
.attr("d", path);
159-
160-
svg.insert("clipPath")
161-
.attr("id", "landclip")
162-
.append("path", ".graticule")
163-
.datum(topojson.feature(us, us.objects.land))
164-
.attr("d", path);
165-
166-
svg.insert("path", ".graticule")
167-
.datum(topojson.mesh(us, us.objects.states, function(a, b) { return a !== b; }))
168-
.attr("class", "state-boundary")
169-
.attr("d", path);
95+
var redraw = function(geodata) {
96+
var map = $('#map').html('<svg></svg>');
97+
var width = map.width();
98+
var height = map.width() * 0.55 / (width/630);
99+
100+
//Map projection
101+
var projection = d3.geo.mercator()
102+
.scale(530 * (width/630))
103+
.center([-95,40]) //projection center
104+
.translate([width/2,height/2]) //translate to center the map in view
105+
106+
//Generate paths based on projection
107+
var path = d3.geo.path()
108+
.projection(projection);
109+
110+
//Create an SVG
111+
var svg = d3.select("#map svg")
112+
.attr("width", width)
113+
.attr("height", height);
114+
115+
//Group for the map features
116+
var features = svg.append("g")
117+
.attr("class","features");
118+
119+
120+
var tip = d3.tip()
121+
.attr('class', 'd3-tip')
122+
.offset([-10, 0])
123+
.html(function(d) {
124+
return d.name;
125+
});
126+
svg.call(tip);
127+
128+
129+
features.selectAll("path")
130+
.data(topojson.feature(geodata,geodata.objects.collection).features)
131+
.enter()
132+
.append("path")
133+
.attr("class", "land")
134+
.attr("d",path);
170135
171136
svg.selectAll("circle.footprint")
172137
.data(batch_regions)
@@ -184,8 +149,8 @@
184149
return "city footprint";
185150
}
186151
});
187-
188-
svg.selectAll("circle.impact")
152+
153+
svg.selectAll("circle.impact")
189154
.data(batch_regions)
190155
.enter()
191156
.append("circle")
@@ -201,7 +166,6 @@
201166
return "city impact";
202167
}
203168
});
204-
205169
svg.selectAll("circle.outline")
206170
.data(batch_regions)
207171
.enter()
@@ -218,7 +182,6 @@
218182
return "city outline";
219183
}
220184
});
221-
222185
svg.selectAll("circle.handle")
223186
.data(batch_regions)
224187
.enter()
@@ -238,44 +201,24 @@
238201
.on('click', function(d){
239202
window.location.href = '/'+ d.id;
240203
});
241-
242204
d3.select(self.frameElement).style("height", height + "px");
243205
map.css('margin-top', '-'+(height/1.5)+'px');
244-
};
206+
}
245207
208+
var _geodata = null;
246209
var update = function() {
247-
// Caching the map really makes the load time quicker.
248-
if(!usCached){
249-
d3.json("/assets/topo/us.json", function(error, us) {
250-
usCached = us;
251-
updateMap(us);
252-
});
253-
}else{
254-
updateMap(usCached);
210+
if (_geodata == null) {
211+
d3.json("/assets/topo/us-ca.json",function(error,geodata) {
212+
if (error) return console.log(error);
213+
_geodata = geodata;
214+
redraw(_geodata);
215+
});
216+
} else {
217+
redraw(_geodata);
255218
}
256-
};
219+
}
257220
258221
update();
259222
$(window).on('resize', update);
260-
261-
// $(".wrap-inner").on("mouseenter", function(){
262-
// $("#lead-text").animate({
263-
// "margin-bottom": "220px"
264-
// });
265-
//
266-
// $(".lead").animate({
267-
// "margin-top": "0px"
268-
// });
269-
// });
270-
//
271-
// $(".wrap-inner").on("mouseleave", function(){
272-
// $("#lead-text").animate({
273-
// "margin-bottom": "0px"
274-
// });
275-
//
276-
// $(".lead").animate({
277-
// "margin-top": "5rem"
278-
// });
279-
// });
280223
</script>
281224
{% endblock %}

0 commit comments

Comments
 (0)