Skip to content

Commit 81785a4

Browse files
committed
add markerIconOptions
1 parent 707f578 commit 81785a4

File tree

4 files changed

+23
-6
lines changed

4 files changed

+23
-6
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ Parameters that can be used (can be passed to the app server as GET query params
3131
| oxipng | enable losslsess compression with [oxipng](https://github.com/shssoichiro/oxipng) | `false` |
3232
| arrows | render arrows to show the direction of linestrings | `false` |
3333
| scale | enable render a scale ruler (boolean or [a json options object](https://leafletjs.com/reference-1.6.0.html#control-scale-option)) | `false` |
34+
| markerIconOptions | set marker icon options ([a json options object](https://leafletjs.com/reference-1.6.0.html#icon-option)) *see note | `undefined` (leaflet's default marker) |
35+
36+
* Note on markerIconOptions: it's also accepted a markerIconOptions attribute in the geojson feature, for example `{"type":"Point","coordinates":[-105.01621,39.57422],"markerIconOptions":{"iconUrl":"https://leafletjs.com/examples/custom-icons/leaf-red.png"}}`
3437

3538
## How to use
3639

lib/lib.d.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Control } from "leaflet";
1+
import { Control, IconOptions } from "leaflet";
22

33
export = _default;
44

@@ -104,6 +104,12 @@ interface OsmStaticMapsOptions {
104104
* @defaultValue `false`
105105
*/
106106
scale?: boolean | Control.ScaleOptions;
107+
108+
/**
109+
* set marker icon options ([a json options object](https://leafletjs.com/reference-1.6.0.html#icon-option))
110+
* @defaultValue `undefined`
111+
*/
112+
markerIconOptions?: IconOptions;
107113
}
108114

109115
/** Renders a map controlled by the options passed and returns an image */

lib/lib.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ module.exports = function(options) {
4141
options.oxipng = options.oxipng || false;
4242
options.arrows = options.arrows || false;
4343
options.scale = (options.scale && (typeof options.scale === 'string' ? options.scale : JSON.stringify(options.scale))) || false;
44+
options.markerIconOptions = (options.markerIconOptions && (typeof options.markerIconOptions === 'string' ? options.markerIconOptions : JSON.stringify(options.markerIconOptions))) || false;
4445

4546
const html = replacefiles(template(options));
4647

lib/template.html

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,20 @@
6868
{{/if}}
6969

7070
{{#if geojson }}
71-
var myIcon = L.icon({
72-
iconUrl: "data:image/png;base64,//markericonpng//",
73-
iconSize: [25, 41],
74-
iconAnchor: [15, 41]
75-
});
71+
{{#if markerIconOptions}}
72+
var myIcon = L.icon({{{markerIconOptions}}});
73+
{{else}}
74+
var myIcon = L.icon({
75+
iconUrl: "data:image/png;base64,//markericonpng//",
76+
iconSize: [25, 41],
77+
iconAnchor: [15, 41]
78+
});
79+
{{/if}}
7680
var geojsonlayer = L.geoJson({{{ geojson }}}, {
7781
pointToLayer: function (feature, latlng) {
82+
if (feature.markerIconOptions) {
83+
return L.marker(latlng, {icon: L.icon(feature.markerIconOptions)})
84+
}
7885
return L.marker(latlng, {icon: myIcon});
7986
},
8087
onEachFeature: function (feature, layer) {

0 commit comments

Comments
 (0)