diff --git a/src/components/map/HelsinkiProvider.ts b/src/components/map/HelsinkiProvider.ts index f2ba2ff49..4d9686573 100644 --- a/src/components/map/HelsinkiProvider.ts +++ b/src/components/map/HelsinkiProvider.ts @@ -1,3 +1,4 @@ +import type { AddressResult, ParseArguments, ServiceMapResponse } from "./types" export default class Provider { options: Record; @@ -11,15 +12,12 @@ export default class Provider { async search({ query - }: Record) { - // eslint-disable-next-line no-bitwise - const protocol = ~location.protocol.indexOf('http') ? location.protocol : 'https:'; + }: { query: string }) { const url = this.endpoint({ - query, - protocol + query }); const request = await fetch(url); - const json = await request.json(); + const json = await request.json() as ServiceMapResponse; return this.parse({ data: json }); @@ -27,27 +25,26 @@ export default class Provider { endpoint({ query - }: any = {}) { + }: { query: string }) { const { params } = this.options; - const paramString = this.getParamString({ ...params, - name: query + const paramString = this.getParamString({ + ...params, + q: query }); - const proto = 'https:'; - return `${proto}//dev.hel.fi/geocoder/v1/address/?${paramString}&municipality=91`; + return `https://api.hel.fi/servicemap/v2/search/?${paramString}&type=address&&municipality=helsinki`; } parse({ data - }: any) { - return data.objects.map(r => { + }: ParseArguments): Array { + return data.results?.map(address => { return { - x: r.location.coordinates[0], - y: r.location.coordinates[1], - label: r.name + x: address.location?.coordinates[0] ?? 0, + y: address.location?.coordinates[1] ?? 0, + label: address.name?.fi ?? "", }; }); } - } \ No newline at end of file diff --git a/src/components/map/types.ts b/src/components/map/types.ts index ee96fc756..ed91892a5 100644 --- a/src/components/map/types.ts +++ b/src/components/map/types.ts @@ -1 +1,49 @@ -export type ControlPosition = "topleft" | "topright" | "bottomleft" | "bottomright"; \ No newline at end of file +export type ControlPosition = "topleft" | "topright" | "bottomleft" | "bottomright"; + +interface ServiceMapAddress { + object_type: "address"; + name: { + fi: string; + sv: string; + en: string; + }; + number: string; + number_end: string; + letter: string; + modified_at: string; + municipality: { + id: string; + name: { + fi: string; + sv: string; + }; + }; + street: { + name: { + fi: string; + sv?: string; + en?: string; + }; + }; + location: { + type: "Point"; + coordinates: [number, number]; + }; +}; + +export interface ServiceMapResponse { + count: number; + next: string | null; + previous: string | null; + results: Array; + }; + +export interface ParseArguments { + data: ServiceMapResponse; +}; + +export interface AddressResult { + x: number; + y: number; + label: string; +};