-
Notifications
You must be signed in to change notification settings - Fork 126
/
Copy pathreverse.ts
88 lines (79 loc) · 2.57 KB
/
reverse.ts
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
/* Copyright (c) 2017-2018 Environmental Systems Research Institute, Inc.
* Apache-2.0 */
import {
request,
cleanUrl,
IPoint,
ILocation
} from "@esri/arcgis-rest-request";
import { ARCGIS_ONLINE_GEOCODING_URL, IEndpointOptions } from "./helpers.js";
export interface IReverseGeocodeResponse {
address: {
[key: string]: any;
};
location: IPoint;
}
function isLocationArray(
coords: ILocation | IPoint | [number, number] | [number, number, number]
): coords is [number, number] | [number, number, number] {
return (
(coords as [number, number]).length === 2 ||
(coords as [number, number, number]).length === 3
);
}
function isLocation(
coords: ILocation | IPoint | [number, number] | [number, number, number]
): coords is ILocation {
return (
(coords as ILocation).latitude !== undefined ||
(coords as ILocation).lat !== undefined
);
}
/**
* Used to determine the address of a [location](https://developers.arcgis.com/rest/geocode/api-reference/geocoding-reverse-geocode.htm).
*
* ```js
* import { reverseGeocode } from '@esri/arcgis-rest-geocoding';
* //
* reverseGeocode([-118.409,33.943 ]) // long, lat
* .then((response) => {
* response.address.PlaceName; // => "LA Airport"
* });
* // or
* reverseGeocode({ long: -118.409, lat: 33.943 })
* reverseGeocode({ latitude: 33.943, latitude: -118.409 })
* reverseGeocode({ x: -118.409, y: 33.9425 }) // wgs84 is assumed
* reverseGeocode({ x: -13181226, y: 4021085, spatialReference: { wkid: 3857 })
* ```
*
* @param coordinates - the location you'd like to associate an address with.
* @param requestOptions - Additional options for the request including authentication.
* @returns A Promise that will resolve with the data from the response.
*/
export function reverseGeocode(
coords: IPoint | ILocation | [number, number],
requestOptions?: IEndpointOptions
): Promise<IReverseGeocodeResponse> {
const options: IEndpointOptions = {
endpoint: ARCGIS_ONLINE_GEOCODING_URL,
params: {},
...requestOptions
};
if (isLocationArray(coords)) {
options.params.location = coords.join();
} else if (isLocation(coords)) {
if (coords.lat) {
options.params.location = coords.long + "," + coords.lat;
}
if (coords.latitude) {
options.params.location = coords.longitude + "," + coords.latitude;
}
} else {
// if input is a point, we can pass it straight through, with or without a spatial reference
options.params.location = coords;
}
return request(`${cleanUrl(options.endpoint)}/reverseGeocode`, options);
}
export default {
reverseGeocode
};