/
error-handling-leaflet.html
190 lines (160 loc) · 6.03 KB
/
error-handling-leaflet.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<!DOCTYPE html>
<html>
<head>
<title>Error handling | CARTO</title>
<meta name="viewport" content="initial-scale=1.0">
<meta charset="utf-8">
<link href="https://fonts.googleapis.com/css?family=Montserrat:400,600,700|Open+Sans:300,400,600" rel="stylesheet">
<link href='https://fonts.googleapis.com/css?family=Open+Sans:300,400,600,700' rel='stylesheet' type='text/css'>
<!-- Include Leaflet -->
<script src="https://unpkg.com/leaflet@1.3.1/dist/leaflet.js"></script>
<link href="https://unpkg.com/leaflet@1.3.1/dist/leaflet.css" rel="stylesheet">
<!-- Include CARTO.js -->
<script src="../../../dist/public/carto.js"></script>
<link href="../style.css" rel="stylesheet">
</head>
<body>
<div id="map"></div>
<aside class="toolbox">
<div class="box">
<header>
<h1>Error handling</h1>
<button class="github-logo js-source-link"></button>
</header>
<section>
<p class="description open-sans">Handle common errors in maps.</p>
<div class="separator"></div>
<section class="usage">
<header>USAGE</header>
<p class="open-sans">In this example we handle the client error while calling the `addLayer` funciton.</p>
<p class="open-sans">See `refreshLayer` function in the source code for an example on how to handle Promise errors.</p>
</section>
<div id="controls">
<div id="info"><p class="mt-16 open-sans"></p></div>
</div>
</section>
<footer class="js-footer"></footer>
</div>
<div class="box widget">
<h2 class="h2">DATASET</h2>
<textarea id="dataset-value" rows=2></textarea>
<button id="dataset-button" class="button" onclick="updateDataset()">SET WRONG DATASET</button>
</div>
<div class="box widget">
<h2 class="h2">STYLE</h2>
<textarea id="style-value" rows="3"></textarea>
<button id="style-button" class="button" onclick="updateStyle()">UPDATE STYLE</button>
</div>
</aside>
<script>
const map = L.map('map').setView([30, 0], 3);
map.scrollWheelZoom.disable();
L.tileLayer('https://{s}.basemaps.cartocdn.com/rastertiles/voyager_nolabels/{z}/{x}/{y}.png', {
maxZoom: 18
}).addTo(map);
const client = new carto.Client({
apiKey: 'default_public',
username: 'cartojs-test'
});
// State
const state = {
isDatasetValid: true,
isStyleValid: true,
errorMessage: '',
updating: false
};
// Datasets
const validDataset = 'ne_10m_populated_places_simple';
const wrongDataset = 'wrong_dataset';
// Style
const validStyle = '#layer { marker-fill: red; }';
const wrongStyle = '#layer { marker-fill: wrong-color; }';
let source = new carto.source.Dataset(validDataset);
let style = new carto.style.CartoCSS(validStyle);
let layer = new carto.layer.Layer(source, style);
client.addLayer(layer);
client.getLeafletLayer().addTo(map);
updateUI();
function refreshLayer (layer) {
client.addLayer(layer)
.then(function () {
state.errorMessage = '';
state.updating = false;
updateUI();
})
.catch(function (error) {
state.errorMessage = error.message;
state.updating = false;
updateUI();
});
}
function updateDataset() {
client.removeLayer(layer);
state.isDatasetValid = !state.isDatasetValid;
state.updating = true;
const dataset = state.isDatasetValid
? validDataset
: wrongDataset;
updateUI();
source = new carto.source.Dataset(dataset);
layer = new carto.layer.Layer(source, style);
refreshLayer(layer);
}
function updateStyle() {
client.removeLayer(layer);
state.isStyleValid = !state.isStyleValid;
state.updating = true;
const styleContent = state.isStyleValid
? validStyle
: wrongStyle;
updateUI();
style = new carto.style.CartoCSS(styleContent);
layer = new carto.layer.Layer(source, style);
refreshLayer(layer);
}
function updateUI () {
const datasetValueEl = document.getElementById('dataset-value');
const styleValueEl = document.getElementById('style-value');
const datasetButtonEl = document.getElementById('dataset-button');
const styleButtonEl = document.getElementById('style-button');
const panelEl = document.querySelector('#info p');
datasetValueEl.value = state.isDatasetValid
? validDataset
: wrongDataset;
styleValueEl.value = state.isStyleValid
? validStyle
: wrongStyle;
datasetButtonEl.textContent = state.isDatasetValid
? 'Set wrong dataset'
: 'Revert to a valid dataset';
styleButtonEl.textContent = state.isStyleValid
? 'Set wrong style'
: 'Revert to a valid style';
panelEl.textContent = state.updating
? 'Updating...'
: state.errorMessage
? state.errorMessage
: 'Everything OK';
if (state.updating) {
panelEl.classList.remove('bg-red');
panelEl.classList.remove('bg-white');
panelEl.classList.add('bg-orange');
panelEl.classList.add('p-8');
panelEl.classList.add('text-white');
} else if (state.errorMessage) {
panelEl.classList.add('bg-red');
panelEl.classList.remove('bg-white');
panelEl.classList.remove('bg-orange');
panelEl.classList.add('p-8');
panelEl.classList.add('text-white');
} else {
panelEl.classList.add('bg-white');
panelEl.classList.remove('bg-red');
panelEl.classList.remove('bg-orange');
panelEl.classList.remove('p-8');
panelEl.classList.remove('text-white');
}
}
</script>
</body>
</html>