Repository navigation
Expand file tree
/
Copy pathexample10.html
More file actions
169 lines (145 loc) · 5.29 KB
/
Copy pathexample10.html
File metadata and controls
169 lines (145 loc) · 5.29 KB
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
159
160
161
162
163
164
165
166
167
168
169
<!doctype HTML>
<meta charset = 'utf-8'>
<html>
<head>
<link rel='stylesheet' href='/Library/Frameworks/R.framework/Versions/3.0/Resources/library/rCharts/libraries/leaflet/external/leaflet.css'>
<link rel='stylesheet' href='/Library/Frameworks/R.framework/Versions/3.0/Resources/library/rCharts/libraries/leaflet/external/leaflet-rCharts.css'>
<link rel='stylesheet' href='/Library/Frameworks/R.framework/Versions/3.0/Resources/library/rCharts/libraries/leaflet/external/legend.css'>
<script src='/Library/Frameworks/R.framework/Versions/3.0/Resources/library/rCharts/libraries/leaflet/external/leaflet.js' type='text/javascript'></script>
<script src='/Library/Frameworks/R.framework/Versions/3.0/Resources/library/rCharts/libraries/leaflet/external/leaflet-providers.js' type='text/javascript'></script>
<script src='/Library/Frameworks/R.framework/Versions/3.0/Resources/library/rCharts/libraries/leaflet/external/Control.FullScreen.js' type='text/javascript'></script>
<script src='regions.js' type='text/javascript'></script>
<style>
.rChart {
display: block;
margin-left: auto;
margin-right: auto;
width: 800px;
height: 400px;
}
</style>
</head>
<body>
<div id='chart9ce53fa5634' class='rChart leaflet'></div>
<script>
var spec = {
"dom": "chart9ce53fa5634",
"width": 800,
"height": 400,
"urlTemplate": "http://{s}.tile.osm.org/{z}/{x}/{y}.png",
"layerOpts": {
"attribution": "Map data<a href=\"http://openstreetmap.org\">OpenStreetMap</a>\n contributors, Imagery<a href=\"http://mapbox.com\">MapBox</a>"
},
"provider": "Stamen.TonerLite",
"center": [ -37, 145 ],
"zoom": 6,
"features": regions,
"legend": {
"position": "bottomright",
"colors": [ "red", "blue", "green" ],
"labels": [ "high", "medium", "low" ]
},
"id": "chart9ce53fa5634"
}
var map = L.map(spec.dom)
.setView(spec.center, spec.zoom);
if (spec.provider){
L.tileLayer.provider(spec.provider).addTo(map)
} else {
L.tileLayer(spec.urlTemplate, spec.layerOpts).addTo(map)
}
if (spec.circle2){
for (var c in spec.circle2){
var circle = L.circle(c.center, c.radius, c.opts)
.addTo(map);
}
}
var geojsonLayer = L.geoJson(spec.features
,
{
"style": function(feature) {
var rgn2col = {1:'red',2:'blue',4:'green'};
return {
color: rgn2col[feature.properties['region_id']],
strokeWidth: '1px',
strokeOpacity: 0.5,
fillOpacity: 0.2
}; } ,
"onEachFeature": function (feature, layer) {
// info rollover
if (document.getElementsByClassName('info leaflet-control').length == 0 ){
info = L.control({position: 'topright'}); // NOTE: made global b/c not ideal place to put this function
info.onAdd = function (map) {
this._div = L.DomUtil.create('div', 'info');
this.update();
return this._div;
};
info.update = function (props) {
this._div.innerHTML = '<h4>Field Name</h4>' + (props ?
props['region_id'] + ': <b> + props[fld] + </b>'
: 'Hover over a region');
};
info.addTo(map);
};
// mouse events
layer.on({
// mouseover to highlightFeature
mouseover: function (e) {
var layer = e.target;
layer.setStyle({
strokeWidth: '3px',
strokeOpacity: 0.7,
fillOpacity: 0.5
});
if (!L.Browser.ie && !L.Browser.opera) {
layer.bringToFront();
}
info.update(layer.feature.properties);
},
// mouseout to resetHighlight
mouseout: function (e) {
geojsonLayer.resetStyle(e.target);
info.update();
},
// click to zoom
click: function (e) {
var layer = e.target;
if ( feature.geometry.type === 'MultiPolygon' ) {
// for multipolygons get true extent
var bounds = layer.getBounds(); // get the bounds for the first polygon that makes up the multipolygon
// loop through coordinates array, skip first element as the bounds var represents the bounds for that element
for ( var i = 1, il = feature.geometry.coordinates[0].length; i < il; i++ ) {
var ring = feature.geometry.coordinates[0][i];
var latLngs = ring.map(function(pair) {
return new L.LatLng(pair[1], pair[0]);
});
var nextBounds = new L.LatLngBounds(latLngs);
bounds.extend(nextBounds);
}
map.fitBounds(bounds);
} else {
// otherwise use native target bounds
map.fitBounds(e.target.getBounds());
}
}
});
}
}
).addTo(map)
var legend = L.control({position: spec.legend.position});
legend.onAdd = function (map) {
var div = L.DomUtil.create('div', 'info legend'),
labels = spec.legend.labels,
colors = spec.legend.colors
// loop through our density intervals and generate a
// label with a colored square for each interval
for (var i = 0; i < labels.length; i++) {
div.innerHTML += '<i style="background:' + colors[i] + '"></i>' +
labels[i] + "<br>"
}
return div;
};
legend.addTo(map);
</script>
</body>
</html>