-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbase.coffee
149 lines (123 loc) · 4.3 KB
/
base.coffee
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
---
---
proximityFromZoom = (z) ->
zoom = parseInt(z.replace('#',''),10)
miles = [
10000, 8000, 7000, 6000, 5000,
3000, 1000, 500, 300, 150,
150, 80, 30, 20, 10,
10, 5, 3, 1, 1,
0.5, 0.5, 0.5, 0.5
]
"&proximity_square=#{miles[zoom]}"
setFeatures = (geojson, clear) ->
# Filter out elements that already have been added
# By checking to see if their ids are keys in the existing_features obj
geojson.features = geojson.features.filter (x) ->
!window.existing_features[x.properties.id]?
# Then add the ids to a hash (since objects have unique keys)
ids = geojson.features.map (f) -> f.properties.id
window.existing_features[id] = id for id in ids
# Check how many keys there are!
# console.log(Object.keys(window.existing_features).length)
# Add the deduped features to the map!
window.markers.push L.mapbox.featureLayer(geojson).addTo map
getUpdatedData = (url=window.location.href) ->
l = url.split('/')
lng = l.pop()
lat = l.pop()
zoom = l.pop()
url = "#{window.base_url}proximity=#{lat},#{lng}#{proximityFromZoom(zoom)}"
url += "&limit=#{window.limit}" if window.limit?
url += "&query=#{window.query}" if window.query?
url += "&occurred_after=#{window.occurred_after}" if window.occurred_after?
unless zoom.match('#')
lng = start_us[1]
lat = start_us[0]
zoom = "#{start_zoom}"
$.ajax
dataType: 'json'
url: url
success: (geojson) ->
setFeatures(geojson)
clearMap = ->
m.clearLayers() for m in window.markers
window.markers = []
window.existing_features = {}
addLegendActions = ->
map.getContainer().querySelector('.toggle_legend').onclick = ->
if @className == 'active'
map.legendControl.removeLegend(legend);
map.legendControl.addLegend(hidden_legend)
else
map.legendControl.removeLegend(hidden_legend);
map.legendControl.addLegend(legend)
addLegendActions()
# This doesn't work since the 2020-10-15 update. Oh well!
# map.getContainer().querySelector('.searchbtn').onclick = ->
# clearMap()
# window.query = $('.map-legend .thefts-location').val()
# getUpdatedData()
map.getContainer().querySelector('.clear_map').onclick = ->
clearMap()
map.getContainer().querySelector('.show_more').onclick = ->
if @className == 'active'
@innerHTML = 'show 500 (slower)'
window.limit = 100
@className = ''
else
window.limit = 500
@className = 'active'
@innerHTML = 'show 100 (default)'
getUpdatedData()
for t in [1..6]
map.getContainer().querySelector("#time_#{t}").onclick = ->
clearMap()
int = parseInt(@id.match(/\d+/)[0], 10)
if @className == 'active'
@className = ''
for below in [int+1..6]
$(".map-legend #time_#{below}").removeClass('active')
else
@className = 'active'
for above in [int-1..1]
$(".map-legend #time_#{above}").addClass('active')
unless int == 6
for below in [int+1..6]
$(".map-legend #time_#{below}").removeClass('active')
$(".map-legend #time_1").addClass('active')
for i in [6..1]
if $(".map-legend #time_#{i}").hasClass('active')
setTimeFrame(i)
break
setTimeFrame = (int) ->
now = Math.floor(Date.now() / 1000)
tframe = [
now - 86400, # 24 hours ago
now - 604800, # 1 week ago
now - 2592000, # 1 month ago
now - 15552000, # 6 months ago
now - 31557600, # 1 year ago
now - 157788000, # 5 years ago
]
if int == 6
window.occurred_after = null
else
window.occurred_after = tframe[int-1]
getUpdatedData()
window.base_url = "https://bikewise.org/api/v2/locations/markers?incident_type=theft&"
start_us = [40.814, -94.702]
start_zoom = 5
window.existing_features = {}
window.markers = []
L.mapbox.accessToken = "pk.eyJ1IjoiYmlrZWluZGV4IiwiYSI6ImNrZzc1OGUzYjAzeDUycW15MnJrcjJ3cjcifQ.ke32Jq5-9LuO2_7nONIK0w"
map = L.mapbox.map("map", "mapbox.light", zoomControl: false).setView(start_us, start_zoom)
hash = new (L.Hash)(map)
hidden_legend = document.getElementById("hidden_legend").innerHTML
legend = document.getElementById("legend").innerHTML
map.legendControl.addLegend(legend)
addLegendActions()
new (L.Control.Zoom)(position: "topright").addTo map
getUpdatedData()
$(window).on "hashchange", (e) ->
getUpdatedData(e.originalEvent.newURL)