forked from cheungkl04/leaflet-socrata
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex-control-layers.html
104 lines (89 loc) · 4.41 KB
/
index-control-layers.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
<html>
<head>
<meta charset=utf-8 />
<title>Leaflet with Socrata</title>
<meta name='viewport' content='initial-scale=1,maximum-scale=1,user-scalable=no' />
<!-- Load Leaflet, use newest version at http://leafletjs.com -->
<link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/leaflet.css" />
<script src="https://unpkg.com/[email protected]/dist/leaflet-src.js"></script>
<!-- load jQuery, use newest version at https://code.jquery.com -->
<script
src="https://code.jquery.com/jquery-3.1.1.min.js"
integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8="
crossorigin="anonymous"></script>
<!-- style the map -->
<style>
body { margin:0; padding:0; }
#map { position: absolute; top:0; bottom:0; right:0; left:0; }
</style>
</head>
<body>
<!-- insert the map -->
<div id="map"></div>
<script>
// set the map center and zoom level
var map = L.map('map').setView([41.76, -72.69], 9);
// add link to view code on GitHub
map.attributionControl
.setPrefix('View <a href="http://github.com/jackdougherty/leaflet-socrata">code on GitHub</a> with <a href="http://leafletjs.com" title="A JS library for interactive maps">Leaflet</a>');
// add legend control layers - global variable with (null, null) allows indiv basemaps and overlays to be added inside functions below
var controlLayers = L.control.layers( null, null, {
position: "topright",
collapsed: false // false = open by default
}).addTo(map);
/* BASELAYERS */
var lightAll = new L.tileLayer('https://cartodb-basemaps-{s}.global.ssl.fastly.net/light_all/{z}/{x}/{y}.png', {
attribution: '© <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors, © <a href="http://cartodb.com/attributions">CartoDB</a>'
}).addTo(map); // adds layer by default
controlLayers.addBaseLayer(lightAll, 'Gray streets (Carto)');
var Esri_WorldImagery = L.tileLayer('https://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/{z}/{y}/{x}', {
attribution: 'Tiles © Esri — Source: Esri, i-cubed, USDA, USGS, AEX, GeoEye, Getmapping, Aerogrid, IGN, IGP, UPR-EGP, and the GIS User Community'
});
controlLayers.addBaseLayer(Esri_WorldImagery, 'Satellite (Esri)');
/* OVERLAYS */
// load open data from Socrata endpoint in GeoJSON format
// with simple marker styling: blue circles
// register your own Socrata app token at https://dev.socrata.com/register
// Connecticut School Directory, CT Open Data, https://data.ct.gov/resource/v4tt-nt9n
$.getJSON("https://data.ct.gov/resource/v4tt-nt9n.geojson?&$$app_token=QVVY3I72SVPbxBYlTM8fA7eet", function (data){
var geoJsonLayer = L.geoJson(data, {
pointToLayer: function( feature, latlng) {
var circle = L.circleMarker(latlng, {
radius: 6,
fillColor: "blue",
color: "blue",
weight: 2,
opacity: 1,
fillOpacity: 0.7
});
circle.bindPopup(feature.properties.name + '<br>' + feature.properties.district_name); // replace last term with property data labels to display from GeoJSON file
return circle;
}
}).addTo(map); // insert ".addTo(map)" to display layer by default
controlLayers.addOverlay(geoJsonLayer, 'Public Schools (CT Open Data)');
});
// load open data from Socrata endpoint in GeoJSON format, with simple filter https://dev.socrata.com/docs/filtering.html
// with simple marker styling: orange circles
// register your own Socrata app token at https://dev.socrata.com/register
// Agricultural commodities grown by farmer, limited to fruit:peaches
// Connecticut Open Data https://data.ct.gov/resource/y6p2-px98
$.getJSON("https://data.ct.gov/resource/y6p2-px98.geojson?category=Fruit&item=Peaches&$$app_token=QVVY3I72SVPbxBYlTM8fA7eet", function (data){
var geoJsonLayer = L.geoJson(data, {
pointToLayer: function( feature, latlng) {
var circle = L.circleMarker(latlng, {
radius: 6,
fillColor: "orange",
color: "orange",
weight: 2,
opacity: 1,
fillOpacity: 0.7
});
circle.bindPopup(feature.properties.business + '<br>' + feature.properties.item); // replace last term with property data labels to display from GeoJSON file
return circle;
}
}); // insert ".addTo(map)" to display layer by default
controlLayers.addOverlay(geoJsonLayer, 'Peaches (CT Open Data)');
});
</script>
</body>
</html>