Skip to content

Commit

Permalink
Replace geocoder address search with servicemap
Browse files Browse the repository at this point in the history
  • Loading branch information
henrinie-nc committed Aug 1, 2024
1 parent 9f164d0 commit 8957be6
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 18 deletions.
31 changes: 14 additions & 17 deletions src/components/map/HelsinkiProvider.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { AddressResult, ParseArguments, ServiceMapResponse } from "./types"
export default class Provider {
options: Record<string, any>;

Expand All @@ -11,43 +12,39 @@ export default class Provider {

async search({
query
}: Record<string, any>) {
// 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
});
}

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<AddressResult> {
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 ?? "",
};
});
}

}
50 changes: 49 additions & 1 deletion src/components/map/types.ts
Original file line number Diff line number Diff line change
@@ -1 +1,49 @@
export type ControlPosition = "topleft" | "topright" | "bottomleft" | "bottomright";
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<ServiceMapAddress>;
};

export interface ParseArguments {
data: ServiceMapResponse;
};

export interface AddressResult {
x: number;
y: number;
label: string;
};

0 comments on commit 8957be6

Please sign in to comment.