Skip to content

Commit 797d704

Browse files
committed
custom marker icon and color in the map component
1 parent 4613615 commit 797d704

File tree

4 files changed

+21
-4
lines changed

4 files changed

+21
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
- Re-add the systematic `CAST(? AS TEXT)` around variables, which helps the database know which type it is dealing with in advance. This fixes a regression in 0.15 where some SQLite websites were broken because of missing affinity information. In SQLite `SELECT '1' = 1` returns `false` but `SELECT CAST('1' AS TEXT) = 1` returns `true`. This also fixes error messages like `could not determine data type of parameter $1` in PostgreSQL.
1212
- Fix a bug where [cookie](https://sql.ophir.dev/documentation.sql?component=cookie#component) removal set the cookie value to the empty string instead of removing the cookie completely.
1313
- Support form submission using the [button](https://sql.ophir.dev/documentation.sql?component=button#component) component using its new `form` property. This allows you to create a form with multiple submit buttons that submit the form to different targets.
14+
- Custom icons and colors for markers in the [map](https://sql.ophir.dev/documentation.sql?component=map#component) component.
1415

1516
## 0.15.0 (2023-10-29)
1617
- New function: [`sqlpage.path`](https://sql.ophir.dev/functions.sql?function=path#function) to get the path of the current page.

examples/official-site/sqlpage/migrations/10_map.sql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,8 @@ VALUES (
9999
JSON(
100100
'[
101101
{ "component": "map", "title": "Paris", "zoom": 11, "latitude": 48.85, "longitude": 2.34, "height": 400 },
102-
{ "title": "Notre Dame", "latitude": 48.8530, "longitude": 2.3498, "description_md": "A beautiful cathedral.", "link": "https://en.wikipedia.org/wiki/Notre-Dame_de_Paris" },
103-
{ "title": "Eiffel Tower", "latitude": 48.8584, "longitude": 2.2945, "description_md": "A tall tower. [Wikipedia](https://en.wikipedia.org/wiki/Eiffel_Tower)" }
102+
{ "title": "Notre Dame", "icon": "building-castle", "color": "indigo", "latitude": 48.8530, "longitude": 2.3498, "description_md": "A beautiful cathedral.", "link": "https://en.wikipedia.org/wiki/Notre-Dame_de_Paris" },
103+
{ "title": "Eiffel Tower", "icon": "tower", "color": "yellow", "latitude": 48.8584, "longitude": 2.2945, "description_md": "A tall tower. [Wikipedia](https://en.wikipedia.org/wiki/Eiffel_Tower)" }
104104
]'
105105
)
106106
);

sqlpage/sqlpage.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,20 @@ function sqlpage_map() {
5555
}
5656
function addMarker(marker_elem, map) {
5757
const coords = marker_elem.dataset.coords.split(",").map(c => parseFloat(c));
58-
const marker = L.marker(coords).addTo(map);
58+
const options = {
59+
title: marker_elem.getElementsByTagName("h3")[0].textContent.trim(),
60+
};
61+
const color = marker_elem.dataset.color;
62+
const icon_obj = marker_elem.getElementsByClassName("mapicon")[0];
63+
if (icon_obj) {
64+
options.icon = L.divIcon({
65+
html: icon_obj,
66+
className: `border-0 bg-${color || 'primary'} bg-gradient text-white rounded-circle p-2 shadow`,
67+
iconSize: [42, 42],
68+
iconAnchor: [21, 5],
69+
});
70+
}
71+
const marker = L.marker(coords, options).addTo(map);
5972
marker.bindPopup(marker_elem);
6073
}
6174
}

sqlpage/templates/map.handlebars

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,11 @@
1717
</div>
1818
<div class="d-none" hidden>
1919
{{~#each_row~}}
20-
<div class="marker" data-coords="{{latitude}},{{longitude}}">
20+
<div class="marker" data-coords="{{latitude}},{{longitude}}" {{#if color}}data-color="{{color}}"{{/if}}>
2121
<h3>
22+
{{~#if icon~}}
23+
<span class="mapicon">{{~icon_img icon~}}</span>
24+
{{~/if~}}
2225
{{~#if link~}}
2326
<a href="{{link}}">{{title}}</a>
2427
{{~else~}}

0 commit comments

Comments
 (0)