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 | import theConfig from '../../data/Config.js'; |
26 | |
27 | import { ZERO, ONE, HTTP_STATUS_OK } from '../../main/Constants.js'; |
28 | |
29 | |
30 | |
31 | |
32 | |
33 | |
34 | |
35 | class NominatimDataLoader { |
36 | |
37 | |
38 | |
39 | |
40 | |
41 | #nominatimStatusOk = false; |
42 | |
43 | |
44 | |
45 | |
46 | |
47 | |
48 | #name = ''; |
49 | |
50 | |
51 | |
52 | |
53 | |
54 | |
55 | #street = ''; |
56 | |
57 | |
58 | |
59 | |
60 | |
61 | |
62 | #city = ''; |
63 | |
64 | |
65 | |
66 | |
67 | |
68 | |
69 | #country = ''; |
70 | |
71 | |
72 | |
73 | |
74 | |
75 | |
76 | |
77 | #parseNominatimData ( nominatimDataZoom10, nominatimDataZoom18 ) { |
78 | if ( nominatimDataZoom10.error || nominatimDataZoom18.error ) { |
79 | this.#nominatimStatusOk = false; |
80 | } |
81 | else { |
82 | this.#name = nominatimDataZoom18.namedetails.name; |
83 | if ( nominatimDataZoom18.address.house_number ) { |
84 | this.#street = nominatimDataZoom18.address.house_number + ' '; |
85 | } |
86 | if ( nominatimDataZoom18.address.road ) { |
87 | this.#street += nominatimDataZoom18.address.road + ' '; |
88 | } |
89 | else if ( nominatimDataZoom18.address.pedestrian ) { |
90 | this.#street += nominatimDataZoom18.address.pedestrian + ' '; |
91 | } |
92 | this.#city = |
93 | nominatimDataZoom10.address.city |
94 | || |
95 | nominatimDataZoom10.address.town |
96 | || |
97 | nominatimDataZoom10.address.municipality |
98 | || |
99 | nominatimDataZoom10.address.village |
100 | || |
101 | ''; |
102 | this.#country = nominatimDataZoom18.address.country; |
103 | this.#nominatimStatusOk = true; |
104 | } |
105 | } |
106 | |
107 | |
108 | |
109 | |
110 | |
111 | constructor ( ) { |
112 | Object.freeze ( this ); |
113 | } |
114 | |
115 | |
116 | |
117 | |
118 | |
119 | |
120 | async loadData ( latLng ) { |
121 | |
122 | this.#nominatimStatusOk = false; |
123 | |
124 | |
125 | const nominatimUrl = |
126 | theConfig.nominatim.url + 'reverse?format=json&lat=' + |
127 | latLng [ ZERO ] + '&lon=' + latLng [ ONE ]; |
128 | |
129 | |
130 | let nominatimUrlZoom10 = |
131 | nominatimUrl + '&zoom=10&addressdetails=1&namedetails=0'; |
132 | |
133 | |
134 | let nominatimUrlZoom18 = |
135 | nominatimUrl + '&zoom=18&addressdetails=1&namedetails=1'; |
136 | |
137 | |
138 | const nominatimLanguage = theConfig.nominatim.language; |
139 | if ( nominatimLanguage && '*' !== nominatimLanguage ) { |
140 | nominatimUrlZoom10 += '&accept-language=' + nominatimLanguage; |
141 | nominatimUrlZoom18 += '&accept-language=' + nominatimLanguage; |
142 | } |
143 | |
144 | const nominatimHeaders = new Headers ( ); |
145 | if ( nominatimLanguage && '*' === nominatimLanguage ) { |
146 | nominatimHeaders.append ( 'accept-language', '' ); |
147 | } |
148 | |
149 | |
150 | const nominatimResponseZoom10 = await fetch ( nominatimUrlZoom10, { headers : nominatimHeaders } ); |
151 | const nominatimResponseZoom18 = await fetch ( nominatimUrlZoom18, { headers : nominatimHeaders } ); |
152 | if ( |
153 | HTTP_STATUS_OK === nominatimResponseZoom10.status |
154 | && |
155 | nominatimResponseZoom10.ok |
156 | && |
157 | HTTP_STATUS_OK === nominatimResponseZoom18.status |
158 | && |
159 | nominatimResponseZoom18.ok |
160 | ) { |
161 | this.#parseNominatimData ( |
162 | await nominatimResponseZoom10.json ( ), |
163 | await nominatimResponseZoom18.json ( ) |
164 | ); |
165 | } |
166 | else { |
167 | this.#nominatimStatusOk = false; |
168 | } |
169 | } |
170 | |
171 | |
172 | |
173 | |
174 | |
175 | |
176 | get name ( ) { return this.#nominatimStatusOk ? this.#name : null; } |
177 | |
178 | |
179 | |
180 | |
181 | |
182 | |
183 | get street ( ) { return this.#nominatimStatusOk ? this.#street : null; } |
184 | |
185 | |
186 | |
187 | |
188 | |
189 | |
190 | get city ( ) { return this.#nominatimStatusOk ? this.#city : null; } |
191 | |
192 | |
193 | |
194 | |
195 | |
196 | |
197 | get country ( ) { return this.#nominatimStatusOk ? this.#country : null; } |
198 | |
199 | } |
200 | |
201 | export default NominatimDataLoader; |
202 | |
203 | |
204 | |