Skip to content

Commit

Permalink
[#49] Create fnMarker & handle MarkerIcon props
Browse files Browse the repository at this point in the history
  • Loading branch information
ifirmawan committed Nov 19, 2024
1 parent 4780c5a commit ae5d0bb
Showing 1 changed file with 31 additions and 9 deletions.
40 changes: 31 additions & 9 deletions src/components/Map/Utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,23 @@ import L from 'leaflet';
import mIcon from 'leaflet/dist/images/marker-icon.png';
import mShadow from 'leaflet/dist/images/marker-shadow.png';
import * as topojson from 'topojson-client';
import { renderReactToDiv } from '../../utils/mapHelper';

export const MarkerIcon = () => {
export const MarkerIcon = (props = {}) => {
const defaultIcon = typeof mIcon === 'object' ? mIcon?.src : mIcon;
const defaultShadow = typeof mShadow === 'object' ? mShadow?.src : mShadow;

const Icon = L.icon({
iconUrl: defaultIcon,
shadowUrl: defaultShadow,
iconSize: [25, 41],
iconAnchor: [12, 41],
popupAnchor: [1, -34],
shadowSize: [41, 41]
});
const Icon = props?.html
? L.divIcon(props)
: L.icon({
iconUrl: defaultIcon,
shadowUrl: defaultShadow,
iconSize: [25, 41],
iconAnchor: [12, 41],
popupAnchor: [1, -34],
shadowSize: [41, 41],
...props
});

return Icon;
};
Expand All @@ -33,3 +37,21 @@ export const getGeoJSONList = (d) => {
}
return [d];
};

export const fnMarker = (
latlng,
{ children = null, label = null, popUp = {}, icon = {}, ...props }
) => {
const m = L.marker(latlng, {
icon: MarkerIcon(icon),
...props
});
if (label) {
m.bindPopup(label, popUp || {});
}
if (children) {
const content = renderReactToDiv(children);
m.bindPopup(content, popUp || {});
}
return m;
};

0 comments on commit ae5d0bb

Please sign in to comment.