-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathshared.js
More file actions
66 lines (62 loc) · 2.48 KB
/
shared.js
File metadata and controls
66 lines (62 loc) · 2.48 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
// shared.js
// Loads the SDF map pin image into a Mapbox map instance.
// Call loadPinImage(map, callback) after map 'load', before adding any symbol layers.
//
// The pin is loaded as an SDF (Signed Distance Field), which means its color
// can be controlled dynamically at render time via the 'icon-color' paint property
// on any symbol layer that uses it. This makes it easy to change pin colors
// based on lien type, bond company, criminal charges, or any other data-driven
// property without needing separate image assets per color.
//
// Usage in each city's map.js:
// map.on('load', function() {
// loadPinImage(map, function() {
// // add your symbol layers here
// });
// });
//
// When adding a symbol layer, use:
// 'type': 'symbol'
// 'layout': {
// 'icon-image': 'bail-pin',
// 'icon-size': 0.5,
// 'icon-anchor': 'bottom',
// 'icon-allow-overlap': true
// }
// 'paint': {
// 'icon-color': '#2b2b2b', // change this to any color or expression
// 'icon-opacity': 0.9
// }
function loadPinImage(map, callback) {
const pinPath = getRelativePinPath('pin.png');
const overlayPath = getRelativePinPath('pin_overlay.png');
console.log('[shared.js] Loading pin from:', pinPath);
map.loadImage(pinPath, function(error, image) {
if (error) {
console.error('[shared.js] Failed to load pin image:', error);
return;
}
console.log('[shared.js] Pin image loaded successfully');
if (!map.hasImage('bail-pin')) {
map.addImage('bail-pin', image, { sdf: true });
}
// Load the static white overlay (not SDF — always white)
map.loadImage(overlayPath, function(error2, overlayImage) {
if (error2) {
console.error('[shared.js] Failed to load pin overlay image:', error2);
return;
}
if (!map.hasImage('bail-pin-overlay')) {
map.addImage('bail-pin-overlay', overlayImage, { sdf: false });
}
if (callback) callback();
});
});
}
// Determines the correct relative path to the pin image based on the current page's location.
// City pages are one level deep (e.g. Newark&Jersey_City/), so they need '../images/pin.png'.
// Root-level pages use 'images/pin.png'.
function getRelativePinPath(filename) {
const depth = window.location.pathname.split('/').filter(Boolean).length;
return (depth >= 2 ? '../images/' : 'images/') + filename;
}