Skip to content

Commit

Permalink
render map polyline with events
Browse files Browse the repository at this point in the history
Signed-off-by: Aleem Isiaka <[email protected]>
  • Loading branch information
limistah committed Dec 30, 2023
1 parent 6b7196c commit 927919c
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/components/Map/Map.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ export type IHMapPropsRequired = Omit<
'loadingEl' | 'style' | 'children' | 'options' | 'ref'
>;

export interface IHMapOptions extends H.Map.Options {
export type IHMapOptions = H.Map.Options & {
mapType?: MAP_TYPES;
}
};

export type IHMapOptionsMerged = IHMapPropsRequired & {
container: React.RefObject<HTMLDivElement> | null;
Expand Down
15 changes: 10 additions & 5 deletions src/components/Map/objects/Polyline/index.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { useContext, useEffect } from 'react';
import { MapContext } from '../../../../contexts/map';
import { initMapObjectEvents } from '../../../libs/initMapObjectEvents';
import { mapEvents } from '../../../libs/defaults';

export interface IHMapPolylineProps {
points: H.geo.IPoint[];
options?: H.map.Polyline.Options;
setViewBounds: boolean;
events: typeof mapEvents;
}

export const HMapPolyline = (props: IHMapPolylineProps) => {
Expand All @@ -19,7 +22,8 @@ export const HMapPolyline = (props: IHMapPolylineProps) => {
);
}
useEffect(() => {
const { points, options } = props;
console.log(props);
const { points, options, events } = props;
// Initialize a LineString and add all the points to it:
const lineString = new H.geo.LineString();
points.forEach(function(point) {
Expand All @@ -33,11 +37,12 @@ export const HMapPolyline = (props: IHMapPolylineProps) => {
});
mapContext.map?.setZoom(4);
// Add event listener to the object if intention of using the object is defined
initMapObjectEvents(polyLine, objectEvents, __options);
const { useEvents, interactive } = mapContext.options || {};
initMapObjectEvents(polyLine, events, {
interactive: Boolean(interactive),
useEvents: Boolean(useEvents),
});
mapContext.map?.addObject(polyLine);

// Add the polyLine to the map:
// console.log(polyLine.getBoundingBox());
}, [props.points]);

return null;
Expand Down
20 changes: 20 additions & 0 deletions src/components/libs/initMapObjectEvents.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { DefaultOptionsType, mapEventTypes, mapEvents } from './defaults';

export const initMapObjectEvents = (
mapObject: H.map.Object,
objectEvents: typeof mapEvents,
platformOptions: Pick<DefaultOptionsType, 'useEvents' | 'interactive'>
) => {
const { useEvents, interactive } = platformOptions;
console.log(objectEvents);
if (useEvents && interactive && objectEvents) {
for (const type in objectEvents) {
if (objectEvents.hasOwnProperty(type)) {
const callback = objectEvents[type as mapEventTypes];
if (callback && typeof callback === 'function') {
mapObject.addEventListener(type, callback);
}
}
}
}
};
12 changes: 12 additions & 0 deletions stories/Polyline.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ const points = [
{ lat: 52.5166, lng: 13.3833 },
];

function logEvent(evt) {
const evtLog = ['event "', evt.type, '" @ ' + evt.target.getData()].join('');
console.log(evtLog);
}

const meta: Meta = {
title: 'HMapPolyline',
component: HMap,
Expand All @@ -19,12 +24,19 @@ const meta: Meta = {
points,
setVisibility: true,
options: { style: { lineWidth: 4 } },
events: {
pointerdown: logEvent,
pointerenter: logEvent,
pointerleave: logEvent,
pointermove: logEvent,
},
},
};

export default meta;

const Template: Story<IHMapPolylineProps> = args => {
console.log(args);
const renderHMapComponents = useHPlatform(
{
appId,
Expand Down

0 comments on commit 927919c

Please sign in to comment.