aboutsummaryrefslogtreecommitdiff
path: root/resources/map.js
blob: 938e08aaaa526c7d415275f39c5fbf45d7e4998a (plain)
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
$(document).ready(function() {

	var self = this;

	self.popupTemplate = _.template('<dl> <% _.each(properties, function(val, key) { %> \
	 <% if (/\:/.exec(key)) { %> \
	 	<dl> \
	 		<dt><%= key.split(":")[1] %> </dt> \
			<dd><%= val %> </dd> \
		</dl> \
	 <% } else {%> \
	 <dt><%= key %> </dt> \
	 <dd><%= val %> </dd> \
	 <% } %> \
	 <% }); %> \
	 </dl>');

	// to filter them later
	self.hospitalAttributes = [];
	self.currentFilter = [];

	fetchLayers();
	var map = L.map( 'map', {
		zoom: 12,
		layers: [self.tileLayer],
	});

	function addHospitalLayer(){
		L.control.layers(null, {
			"Hospitals" : self.hospitalLayer
		}).addTo(map);
		// display layer
		$('.leaflet-control-layers-selector').first().trigger('click')
	}

	map.on('hospitalsfetched', addHospitalLayer);
	map.on('hospitalsfetched', initFilters);
	map.on('locationfound', onLocationFound);
	map.on('locationerror', onLocationError);

	map.on('moveend', function(e) {
		var zoom = map.getZoom();
		if (zoom < 10) {
			return;
		}
		if (!self.hospitalLayer)
			return;
		var bounds = map.getBounds();
		var sw = bounds.getSouthWest();
		var ne = bounds.getNorthEast();
		bbox = [sw.lat, sw.lng, ne.lat, ne.lng].join(',');
		var url = "http://overpass-api.de/api/interpreter?data=[out:json];node[amenity=hospital](" + bbox + ");out;";
		converter = new op2geojson();
		converter.fetch(url, function(data) {
			layer = buildLayer(data)
			self.hospitalLayer.addData(data);
		});
	})

	map.locate({setView: true, maxZoom: 12});

	function buildLayer(data) {
		return L.geoJson(data, {
				onEachFeature: function(feature, layer) {
					var isHierarchical = new RegExp('\:');
					_.each(feature.properties, function(val, key) {
						if (isHierarchical.exec(isHierarchical)) {
							key = key.split(':')[1];
							if (_.contains(self.hospitalAttributes), key) {
								self.hospitalAttributes.push(key);
							}
						}
					});
					layer.bindPopup(self.popupTemplate({ properties: feature.properties }));
				}
		});
	}

	function geojsonLayer() {
        url = "http://overpass-api.de/api/interpreter?data=[out:json];node[amenity=hospital](52.34,13.3,52.52,13.6);out;";
		converter = new op2geojson();
		converter.fetch(url, function(data) {
			layer = buildLayer(data);
			self.hospitalLayer =  layer;
			map.fireEvent('hospitalsfetched');
		});
	}

	function initFilters() {
		var FilterControl = L.Control.extend({
		    options: {
		        position: 'topright'
		    },
		    onAdd: function (map) {
		        // create the control container with a particular class name
		        var container = L.DomUtil.create('div', 'filter-box');
		        return container;
		    }
		});
		var template = _.template('<div><form> <% _.each(attributes, function (attr) { %> \
			<input type="checkbox" name=<%=attr %> value=<%= attr %>> <%= attr %> \
			<% }); %> \
			</form></div>');
		var t = template({ attributes: self.hospitalAttributes });

		map.addControl(new FilterControl());
		$('.filter-box').html(t);
		_.each($('.filter-box input'), function(el) {
			//first check them all
			el.click();
			$(el).click(checkedFilterBox);
		});
	}

	function checkedFilterBox(event) {
		_(self.currentFilter).without(event.currentTarget.value);
		var prev = self.hospitalLayer;
		// L.control.layers(null, {
		// 	"Hospitals" : TODO qgitnew Layer
		// }).addTo(map);
		// var filteredHospitals = L.layer
	}

	function fetchLayers() {
		geojsonLayer();
		self.tileLayer = L.tileLayer('http://{s}.www.toolserver.org/tiles/osm-no-labels/{z}/{x}/{y}.png', {
			maxZoom: 18,
			attribution: 'Map data &copy; <a href="http://openstreetmap.org">OpenStreetMap</a> contributors'
		});
	}

	function onLocationFound(e) {
		self.currentLocation = e.latlng;
		var radius = e.accuracy / 2;
		L.marker(e.latlng).addTo(map)
		.bindPopup("You are within " + radius + " meters from this point").openPopup();
	}

	function onLocationError(e) {
		alert(e.message);
	}

	// function renderHospitalIcon(feature, latlng) {
	// 	var hospitalIcon = L.icon({
	// 		iconUrl: 'resources/img/hospital.png'
	// 	});
	// 	debugger;
	// 	L.marker(latlng, {icon: hospitalIcon}).addTo(map)
	// }
});