Skip to content

Commit 3398c8c

Browse files
authored
Add location properties to mapbox reverse geocode (#286)
* add location properties to mapbox reverse geocode * fix formatting
1 parent e1efe48 commit 3398c8c

File tree

1 file changed

+21
-17
lines changed

1 file changed

+21
-17
lines changed

src/geocoders/mapbox.ts

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,24 @@ export class Mapbox implements GeocoderAPI {
2121
this.options.reverseQueryParams.access_token = accessToken;
2222
}
2323

24+
_getProperties(loc) {
25+
const properties = {
26+
text: loc.text,
27+
address: loc.address
28+
};
29+
30+
for (let j = 0; j < (loc.context || []).length; j++) {
31+
const id = loc.context[j].id.split('.')[0];
32+
properties[id] = loc.context[j].text;
33+
34+
// Get country code when available
35+
if (loc.context[j].short_code) {
36+
properties['countryShortCode'] = loc.context[j].short_code;
37+
}
38+
}
39+
return properties;
40+
}
41+
2442
geocode(query: string, cb: GeocodingCallback, context?: any): void {
2543
const params = this.options.geocodingQueryParams;
2644
if (
@@ -46,26 +64,11 @@ export class Mapbox implements GeocoderAPI {
4664
bbox = L.latLngBounds(center, center);
4765
}
4866

49-
const properties = {
50-
text: loc.text,
51-
address: loc.address
52-
};
53-
54-
for (let j = 0; j < (loc.context || []).length; j++) {
55-
const id = loc.context[j].id.split('.')[0];
56-
properties[id] = loc.context[j].text;
57-
58-
// Get country code when available
59-
if (loc.context[j].short_code) {
60-
properties['countryShortCode'] = loc.context[j].short_code;
61-
}
62-
}
63-
6467
results[i] = {
6568
name: loc.place_name,
6669
bbox: bbox,
6770
center: center,
68-
properties: properties
71+
properties: this._getProperties(loc)
6972
};
7073
}
7174
}
@@ -109,7 +112,8 @@ export class Mapbox implements GeocoderAPI {
109112
results[i] = {
110113
name: loc.place_name,
111114
bbox: bbox,
112-
center: center
115+
center: center,
116+
properties: this._getProperties(loc)
113117
};
114118
}
115119
}

0 commit comments

Comments
 (0)