-
Notifications
You must be signed in to change notification settings - Fork 119
/
geocode.ts
125 lines (115 loc) · 3.49 KB
/
geocode.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/* Copyright (c) 2017-2018 Environmental Systems Research Institute, Inc.
* Apache-2.0 */
import {
request,
appendCustomParams,
cleanUrl
} from "@esri/arcgis-rest-request";
import { IExtent, ISpatialReference, IPoint } from "@esri/arcgis-rest-common";
import { worldGeocoder, IEndpointRequestOptions } from "./helpers";
export interface IGeocodeRequestOptions extends IEndpointRequestOptions {
/**
* use this if all your address info is contained in a single string.
*/
singleLine?: string;
address?: string;
address2?: string;
address3?: string;
neighborhood?: string;
city?: string;
subregion?: string;
/**
* The World Geocoding Service expects US states to be passed in as a 'region'.
*/
region?: string;
postal?: number;
postalExt?: number;
countryCode?: string;
/**
* You can create an autocomplete experience by making a call to suggest with partial text and then passing through the magicKey and complete address that are returned to geocode.
* ```js
* import { suggest, geocode } from '@esri/arcgis-rest-geocoder';
* suggest("LAX")
* .then((response) => {
* geocode({
* singleLine: response.suggestions[1].text,
* magicKey: response.suggestions[0].magicKey
* })
* })
* ```
*/
magicKey?: string;
}
export interface IGeocodeResponse {
spatialReference: ISpatialReference;
candidates: Array<{
address: string;
location: IPoint;
extent?: IExtent;
score: number;
attributes: object;
}>;
}
/**
* ```js
* import { geocode } from '@esri/arcgis-rest-geocoder';
* //
* geocode("LAX")
* .then((response) => {
* response.candidates[0].location; // => { x: -118.409, y: 33.943, spatialReference: ... }
* });
* //
* geocode({
* address: "1600 Pennsylvania Ave",
* postal: 20500,
* countryCode: "USA"
* })
* .then((response) => {
* response.candidates[1].location; // => { x: -77.036533, y: 38.898719, spatialReference: ... }
* });
* ```
* Used to determine the location of a single address or point of interest. See the [REST Documentation](https://developers.arcgis.com/rest/geocode/api-reference/geocoding-find-address-candidates.htm) for more information.
* @param address String representing the address or point of interest or RequestOptions to pass to the endpoint.
* @returns A Promise that will resolve with address candidates for the request. The spatial reference will be added to candidate locations and extents unless `rawResponse: true` was passed.
*/
export function geocode(
address: string | IGeocodeRequestOptions
): Promise<IGeocodeResponse> {
let options: IGeocodeRequestOptions = {
endpoint: worldGeocoder,
params: {}
};
if (typeof address === "string") {
options.params.singleLine = address;
} else {
options.endpoint = address.endpoint || worldGeocoder;
options = {
...options,
...address
};
appendCustomParams(address, options);
}
// add spatialReference property to individual matches
return request(
`${cleanUrl(options.endpoint)}/findAddressCandidates`,
options
).then(response => {
if (options.rawResponse) {
return response;
}
const sr = response.spatialReference;
response.candidates.forEach(function(candidate: {
location: IPoint;
extent?: IExtent;
}) {
candidate.location.spatialReference = sr;
if (candidate.extent) {
candidate.extent.spatialReference = sr;
}
});
return response;
});
}
export default {
geocode
};