-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
158 lines (133 loc) · 4.13 KB
/
index.html
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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Valentine's Day Map: Where is your nearest love?</title>
<meta name="description" content="map of love locations">
<meta name="author" content="@rgdonohue">
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.6.0/dist/leaflet.css"
integrity="sha512-xwE/Az9zrjBIphAcBb3F6JVqxf46+CDLwfLMHloNu6KEQCAWi6HcDUbeOfBIptF7tcCzusKFjFw2yuvEpDL9wQ=="
crossorigin="" />
<link href="https://fonts.googleapis.com/css?family=Merriweather:300,400&display=swap" rel="stylesheet">
<style>
body {
margin: 0;
padding: 0;
background: #090909;
font-family: 'Merriweather', serif;
}
header {
height: 10vh;
}
h1,
h2 {
padding: 0 0 0 .5em;
margin: .6em 0;
color: red;
font-weight: 300;
font-size: 1.8em;
display: inline-block;
}
h2 {
font-size: .9em;
padding-right: 3em;
margin: 2em 0 0;
color: white;
float: right;
}
h2 a {
color: white;
text-decoration: underline;
}
#map {
top: 10vh;
bottom: 0;
width: 100%;
position: absolute;
}
</style>
</head>
<header>
<h1>💘 Where's your nearest love?</h1>
<h2>A quick prototype map for <a href='https://github.com/maptimelex/nearest-love/'>demonstration purposes and
maptimeLEX</a></h2>
</header>
<div id='map'></div>
<body>
<script src="https://d3js.org/d3-fetch.v1.min.js"></script>
<script src="https://unpkg.com/leaflet@1.6.0/dist/leaflet.js"
integrity="sha512-gZwIG9x3wUXg2hdXF6+rVkLF/0Vi9U8D2Ntg4Ga5I5BZpVkVxlJWbSQtXPSiUTtC0TjtGOmxa1AJPuV0CPthew=="
crossorigin=""></script>
<script src='https://npmcdn.com/@turf/turf/turf.min.js'></script>
<script>
const map = L.map('map', {
center: [38, -97],
zoom: 4,
zoomSnap: .1
})
L.tileLayer('https://{s}.basemaps.cartocdn.com/rastertiles/dark_all/{z}/{x}/{y}.png', {
attribution: 'CARTO'
}).addTo(map);
// request data files
const pointsData = d3.json('data/love_points.json');
const voronoiData = d3.json('data/voronoi.json');
// when all data is successfully loaded, send to mapData function
Promise.all([pointsData, voronoiData]).then(mapData);
function mapData(data) {
// function accepts datasets contained within an Array,
// converts GeoJSON data into Leaflet geoJson objects and
// adds them to the map, providing basic styling, interaction,
// and a point in polygon operation
// pull datasets out of array and assign to variables
const pointsJson = data[0];
const voronoiJson = data[1];
// create polygons and add to Leaflet map
const voronoi = L.geoJson(voronoiJson, {
style: function () {
return {
opacity: 0,
fillOpacity: 0,
weight: 1,
fillColor: 'red'
}
}
}).addTo(map);
// create points and add to Leaflet map
const points = L.geoJson(pointsJson, {
pointToLayer: function (geoJsonPoint, latlng) {
return L.circle(latlng, {
color: 'red',
radius: 3
})
},
onEachFeature: function (feature, layer) {
layer.bindTooltip(layer.feature.properties['FEATURE_NAME'])
}
}).addTo(map);
// when mousing over any given polygon, find the nearest
// point within it and open a tooltip with the point's name
voronoi.eachLayer(function (layer) {
layer.on('mouseover', function () {
layer.setStyle({
fillOpacity: .3
});
// loop through all points and perform a point in polygon test
points.eachLayer(function (point) {
if (turf.booleanPointInPolygon(point.toGeoJSON(), layer.toGeoJSON())) {
point.openTooltip();
} else {
point.closeTooltip();
}
})
});
// reset the point's tooltip to close
layer.on('mouseout', function () {
layer.setStyle({
fillOpacity: 0
})
})
})
}
</script>
</body>
</html>