Skip to content

Commit

Permalink
Feature/rotation (#5)
Browse files Browse the repository at this point in the history
* Add rotation to vehicles

* Add option to use external icon urls
  • Loading branch information
r00tat authored Apr 17, 2023
1 parent 762fcf3 commit 288ac2f
Show file tree
Hide file tree
Showing 11 changed files with 83 additions and 7 deletions.
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"geofire-common": "^6.0.0",
"googleapis": "^117.0.0",
"leaflet": "^1.7.1",
"leaflet-rotatedmarker": "^0.2.0",
"leaflet.markercluster": "^1.5.3",
"moment": "^2.29.1",
"moment-timezone": "^0.5.34",
Expand Down
4 changes: 3 additions & 1 deletion src/components/FirecallItems/FirecallItemDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,9 @@ export default function FirecallItemDialog({
id={key}
key={key}
label={label}
type="text"
type={
(itemInfo.fieldTypes && itemInfo.fieldTypes[key]) || 'text'
}
fullWidth
variant="standard"
onChange={onChange(key)}
Expand Down
11 changes: 10 additions & 1 deletion src/components/FirecallItems/infos/marker.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { FirecallItem, FirecallItemMarker } from '../../firebase/firestore';
import { markerIcon } from '../icons';
import { FirecallItemInfo } from './types';
import L from 'leaflet';

export const markerInfo: FirecallItemInfo<FirecallItemMarker> = {
name: 'Marker',
Expand All @@ -11,6 +12,7 @@ export const markerInfo: FirecallItemInfo<FirecallItemMarker> = {
fields: {
name: 'Bezeichnung',
beschreibung: 'Beschreibung',
icon: 'Icon URL',
},
dateFields: [],
factory: () => ({
Expand All @@ -19,6 +21,7 @@ export const markerInfo: FirecallItemInfo<FirecallItemMarker> = {
beschreibung: '',
datum: '',
}),
// fieldTypes: { beschreibung: 'multiline' },
dialogText: (item) => `Markierung`,
popupFn: (item: FirecallItem) => {
return (
Expand All @@ -30,7 +33,13 @@ export const markerInfo: FirecallItemInfo<FirecallItemMarker> = {
);
},
titleFn: (item: FirecallItem) => `${item.name}\n${item.beschreibung || ''}`,
icon: (gisObject: FirecallItem) => {
icon: (m: FirecallItem) => {
if ((m as any)?.icon) {
return L.icon({
iconUrl: (m as any)?.icon,
iconSize: [24, 24],
});
}
return markerIcon;
},
};
2 changes: 2 additions & 0 deletions src/components/FirecallItems/infos/rohr.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ export const rohrItemInfo: FirecallItemInfo<Rohr> = {
art: 'Art (C/B oder Wasserwerfer)',
name: 'Bezeichnung',
durchfluss: 'Durchfluss (l/min)',
rotation: 'Drehung in Grad',
},
dateFields: ['datum'],
fieldTypes: { rotation: 'number', durchfluss: 'number' },
factory: () =>
({
art: 'C',
Expand Down
2 changes: 2 additions & 0 deletions src/components/FirecallItems/infos/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ export interface FirecallItemInfo<T = FirecallItem> {
};

dateFields: string[];

fieldTypes?: { [fieldName: string]: string };
/**
* render popup html
*/
Expand Down
4 changes: 4 additions & 0 deletions src/components/FirecallItems/infos/vehicle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ export const vehicleItemInfo: FirecallItemInfo<Fzg> = {
alarmierung: 'Alarmierung',
eintreffen: 'Eintreffen',
abruecken: 'Abrücken',
rotation: 'Drehung in Grad',
},
dateFields: ['alarmierung', 'eintreffen', 'abruecken'],
fieldTypes: { rotation: 'number', ats: 'number' },
factory: () =>
({
name: '',
Expand All @@ -33,6 +35,8 @@ export const vehicleItemInfo: FirecallItemInfo<Fzg> = {
type: 'vehicle',
alarmierung: moment().toISOString(),
eintreffen: moment().toISOString(),
ats: 0,
rotation: '0',
} as Fzg),
dialogText: (item) => `Einsatzfahrzeug`,
icon: (gisObj: FirecallItem) =>
Expand Down
12 changes: 10 additions & 2 deletions src/components/Map/markers/FirecallItemMarker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { firestore } from '../../firebase/firebase';
import { Connection, FirecallItem } from '../../firebase/firestore';
import { firecallItemInfo } from '../../FirecallItems/infos/firecallitems';
import ConnectionMarker from './ConnectionMarker';
import { RotatedMarker } from './RotatedMarker';

export interface FirecallItemMarkerProps {
record: FirecallItem;
Expand Down Expand Up @@ -76,7 +77,7 @@ export function FirecallItemMarkerDefault({

return (
<>
<Marker
<RotatedMarker
position={startPos}
title={itemInfo.titleFn(record)}
icon={icon}
Expand All @@ -88,6 +89,13 @@ export function FirecallItemMarkerDefault({
updateFircallItemPos(firecallId, event, record);
},
}}
rotationAngle={
record?.rotation &&
!Number.isNaN(Number.parseInt(record?.rotation, 10))
? Number.parseInt(record?.rotation, 10) % 360
: 0
}
rotationOrigin="center"
>
<Popup>
<IconButton
Expand All @@ -98,7 +106,7 @@ export function FirecallItemMarkerDefault({
</IconButton>
{itemInfo.popupFn(record)}
</Popup>
</Marker>
</RotatedMarker>
</>
);
}
38 changes: 38 additions & 0 deletions src/components/Map/markers/RotatedMarker.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import 'leaflet-rotatedmarker';
import { forwardRef, useEffect, useRef } from 'react';
import { Marker, MarkerProps } from 'react-leaflet';
export interface RotatedMarkerProps extends MarkerProps {
rotationAngle?: number;
rotationOrigin?: string;
}
export const RotatedMarker = forwardRef<L.Marker, RotatedMarkerProps>(
({ children, rotationAngle, rotationOrigin, ...props }, forwardRef) => {
const markerRef = useRef<L.Marker>();

useEffect(() => {
const marker = markerRef.current;
if (marker) {
(marker as any).setRotationAngle(rotationAngle);
(marker as any).setRotationOrigin(rotationOrigin);
}
}, [rotationAngle, rotationOrigin]);

return (
<Marker
ref={(ref) => {
if (ref) {
markerRef.current = ref;
// if (forwardRef) {
// forwardRef.current = ref;
// }
}
}}
{...props}
>
{children}
</Marker>
);
}
);

RotatedMarker.displayName = 'RotatedMarker';
1 change: 1 addition & 0 deletions src/components/firebase/firestore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export interface FirecallItem {
datum?: string;
editable?: boolean;
original?: FirecallItem;
rotation?: string;
}

export interface FirecallItemMarker extends FirecallItem {
Expand Down
9 changes: 6 additions & 3 deletions src/pages/api/fzg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ export default function handler(
req: NextApiRequest,
res: NextApiResponse<string>
) {
const { name = '', fw = '' } = req.query;
const { name = '', fw = '', rotate = '0' } = req.query;
const rotateInt = !Number.isNaN(Number.parseInt('' + rotate, 10))
? Number.parseInt('' + rotate, 10) % 360
: 0;
res.setHeader(`Content-Type`, 'image/svg+xml').send(`
<svg xmlns="http://www.w3.org/2000/svg" width="45px" height="20" fill="#ff0000">
<rect width="45" height="20" x="0" y="0" style="fill:#ff0000"></rect>
<rect width="45" height="20" x="0" y="0" style="fill:#ff0000" transform="rotate(${rotateInt})"></rect>
<circle cx="4" cy="6" r="1.5" fill="blue" />
<circle cx="4" cy="14" r="1.5" fill="blue" />
<line x1="8" y1="0" x2="8" y2="20" style="stroke:rgb(255,255,255);stroke-width:2" />
<line x1="8" y1="0" x2="8" y2="20" style="stroke:rgb(255,255,255);stroke-width:2" transform="rotate(${rotateInt})" />
<text x="12" y="9" fill="#ffffff" font-family="Arial, Helvetica, sans-serif" font-size="8">
${name}
<tspan x="12" y="17">${fw}</tspan>
Expand Down

0 comments on commit 288ac2f

Please sign in to comment.