api.ts 2.74 KB
Newer Older
JOE XMG's avatar
update  
JOE XMG committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import * as L from 'leaflet';

/**
 * An object that represents a result from a geocoding query
 */
export interface GeocodingResult {
  /**
   * Name of found location
   */
  name: string;
  /**
   * The bounds of the location
   */
  bbox: L.LatLngBounds;
  /**
   * The center coordinate of the location
   */
  center: L.LatLng;
  /**
   * URL for icon representing result; optional
   */
  icon?: string;
  /**
   * HTML formatted representation of the name
   */
  html?: string;
  /**
   * Additional properties returned by the geocoder
   */
  properties?: any;
}

/**
 * A callback function used in {@link IGeocoder.geocode} and {@link IGeocoder.suggest} and {@link IGeocoder.reverse}
 */
export type GeocodingCallback = (result: GeocodingResult[]) => void;

/**
 * An interface implemented to respond to geocoding queries
 */
export interface IGeocoder {
  /**
   * Performs a geocoding query and returns the results to the callback in the provided context
   * @param query the query
   * @param cb the callback function
   * @param context the `this` context in the callback
   */
  geocode(query: string, cb: GeocodingCallback, context?: any): void;
  /**
   * Performs a geocoding query suggestion (this happens while typing) and returns the results to the callback in the provided context
   * @param query the query
   * @param cb the callback function
   * @param context the `this` context in the callback
   */
  suggest?(query: string, cb: GeocodingCallback, context?: any): void;
  /**
   * Performs a reverse geocoding query and returns the results to the callback in the provided context
   * @param location the coordinate to reverse geocode
   * @param scale the map scale possibly used for reverse geocoding
   * @param cb the callback function
   * @param context the `this` context in the callback
   */
  reverse?(location: L.LatLngLiteral, scale: number, cb: GeocodingCallback, context?: any): void;
}

export interface GeocoderOptions {
  /**
   * URL of the service
   */
  serviceUrl: string;
  /**
   * Additional URL parameters (strings) that will be added to geocoding requests
   */
  geocodingQueryParams?: Record<string, unknown>;
  /**
   * Additional URL parameters (strings) that will be added to reverse geocoding requests
   */
  reverseQueryParams?: Record<string, unknown>;
  /**
   * API key to use this service
   */
  apiKey?: string;
}

/**
 * @internal
 */
export function geocodingParams(
  options: GeocoderOptions,
  params: Record<string, unknown>
): Record<string, unknown> {
  return L.Util.extend(params, options.geocodingQueryParams);
}

/**
 * @internal
 */
export function reverseParams(
  options: GeocoderOptions,
  params: Record<string, unknown>
): Record<string, unknown> {
  return L.Util.extend(params, options.reverseQueryParams);
}