Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add lat/lon coordinates to map labels #354

Merged
merged 1 commit into from
Dec 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use client'

import { IncidentFormFooter } from '@/app/[locale]/incident/components/IncidentFormFooter'
import { useTranslations } from 'next-intl'
import { useLocale, useTranslations } from 'next-intl'
import { Divider } from '@/components/ui/Divider'
import React, { ReactNode, useEffect, useState } from 'react'
import { signalsClient } from '@/services/client/api-client'
Expand All @@ -19,6 +19,10 @@ import { getCurrentStep } from '@/lib/utils/stepper'
import { getAttachments } from '@/lib/utils/attachments'
import { useConfig } from '@/hooks/useConfig'
import { ParagraphOrList } from '@/components/ui/ParagraphOrList'
import {
coords2degreeMinuteSecondsFlat,
isCoordinatesTuple,
} from '@/lib/utils/format-coordinates'

const IncidentSummaryForm = () => {
const t = useTranslations('describe_summary')
Expand All @@ -33,6 +37,7 @@ const IncidentSummaryForm = () => {
const pathname = usePathname()
const step = getCurrentStep(pathname)
const { config } = useConfig()
const locale = useLocale()

useEffect(() => {
router.prefetch('/incident/thankyou')
Expand Down Expand Up @@ -117,6 +122,12 @@ const IncidentSummaryForm = () => {
}
}

const numberFormat = new Intl.NumberFormat(locale)
const unitDisplay: 'short' | 'long' = 'long'
const coordinates = isCoordinatesTuple(formState.coordinates)
? coords2degreeMinuteSecondsFlat(formState.coordinates)
: null

return (
<div className="flex flex-col gap-8">
<Paragraph appearance="lead">{t('description')}</Paragraph>
Expand Down Expand Up @@ -163,9 +174,62 @@ const IncidentSummaryForm = () => {
value={formState.address?.weergave_naam}
>
<div
style={{ minHeight: 200, height: 200 }}
className="signalen-map-img"
role="img"
aria-label=""
inert
data-lat={formState.coordinates && formState.coordinates[0]}
data-lon={formState.coordinates && formState.coordinates[1]}
aria-label={
coordinates
? t('map_coords', {
coords: t('coords', {
latitudeDegree: numberFormat.format(
coordinates.latitudeDegree
),
latitudeMinute: numberFormat.format(
coordinates.latitudeMinute
),
latitudeSecond: numberFormat.format(
coordinates.latitudeSecond
),
longitudeDegree: numberFormat.format(
coordinates.longitudeDegree
),
longitudeMinute: numberFormat.format(
coordinates.longitudeMinute
),
longitudeSecond: numberFormat.format(
coordinates.longitudeSecond
),
latitudeDescription: coordinates.north
? t('north')
: coordinates.south
? t('south')
: '',
longitudeDescription: coordinates.east
? t('east')
: coordinates.west
? t('west')
: '',
degreeSuffix:
// @ts-ignore
unitDisplay === 'short'
? t('degree_short')
: t('degree_long'),
minuteSuffix:
// @ts-ignore
unitDisplay === 'short'
? t('minute_short')
: t('minute_long'),
secondSuffix:
// @ts-ignore
unitDisplay === 'short'
? t('second_short')
: t('second_long'),
}),
})
: undefined
}
>
<LocationMap />
</div>
Expand Down
89 changes: 89 additions & 0 deletions src/lib/utils/format-coordinates.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
type CoordinatesTuple = [number, number]

export interface DegreeMinuteSecond {
degree: number
minute: number
second: number
}

export interface DegreeMinuteSecondCoordinates {
latitude: DegreeMinuteSecond & {
north: boolean
south: boolean
}
longitude: DegreeMinuteSecond & {
east: boolean
west: boolean
}
}

/**
* From: https://stackoverflow.com/posts/5786281
*/
export const degreeMinuteSecond = (d: number): DegreeMinuteSecond => {
let degree = Math.abs(Math.floor(d)),
minute = Math.abs(Math.floor(((d + 1e-9) % 1) * 60)),
second = Math.abs(Math.floor(((((d + 1e-9) * 60) % 1) * 6000) / 100))

return {
degree,
minute,
second,
}
}

export const coords2degreeMinuteSeconds = ([latitude, longitude]: [
number,
number,
]) => {
return {
latitude: {
...degreeMinuteSecond(latitude),
north: latitude >= 0,
south: latitude < 0,
},
longitude: {
...degreeMinuteSecond(longitude),
east: longitude >= 0,
west: longitude < 0,
},
}
}

export const coords2degreeMinuteSecondsFlat = (coords: [number, number]) => {
const {
latitude: {
degree: latitudeDegree,
minute: latitudeMinute,
second: latitudeSecond,
south,
north,
},
longitude: {
degree: longitudeDegree,
minute: longitudeMinute,
second: longitudeSecond,
east,
west,
},
} = coords2degreeMinuteSeconds(coords)

return {
latitudeDegree,
latitudeMinute,
latitudeSecond,
longitudeDegree,
longitudeMinute,
longitudeSecond,
north,
south,
east,
west,
}
}

export const isCoordinatesTuple = (arg: any): arg is CoordinatesTuple =>
Array.isArray(arg) &&
arg.length === 2 &&
typeof arg[0] === 'number' &&
typeof arg[1] === 'number'
14 changes: 13 additions & 1 deletion translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,19 @@
"heading": "Processing",
"description": "Your report is being processed. You will automatically go to the next page once the report is received.\n\nPlease wait a moment...."
}
}
},
"map_coords": "map zoomed into {coords}",
"coords": "{latitudeDegrees} {latitudeMinutes} {latitudeDescription}, {longitudeDegrees} {longitudeMinutes} {longitudeDescription}",
"north": "North",
"south": "South",
"east": "East",
"west": "West",
"degree_long": " degrees",
"minute_long": " minutes",
"second_long": " seconds",
"degree_short": "°",
"minute_short": "′",
"second_short": "″"
},
"describe_thankyou": {
"heading": "Report submitted",
Expand Down
14 changes: 13 additions & 1 deletion translations/nl.json
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,19 @@
"heading": "Bezig met afhandelen",
"description": "Uw melding wordt afgehandeld. U gaat automatisch naar de volgende pagina zodra de melding is ontvangen.\n\nEen ogenblik geduld alstublieft..."
}
}
},
"coords": "{latitudeDegree}{degreeSuffix} {latitudeMinute}{minuteSuffix} {latitudeDescription}, {longitudeDegree}{degreeSuffix} {longitudeMinute}{minuteSuffix} {longitudeDescription}",
"map_coords": "kaart ingezoomd op {coords}",
"north": "noorderbreedte",
"south": "zuiderbreedte",
"east": "oosterlengte",
"west": "westerlengte",
"degree_long": " graden",
"minute_long": " minuten",
"second_long": " seconden",
"degree_short": "°",
"minute_short": "′",
"second_short": "″"
},
"describe_thankyou": {
"heading": "Melding verzonden",
Expand Down
Loading