From 3398c8c9ce651068557ff483f861e97b3c0a80a2 Mon Sep 17 00:00:00 2001 From: dfliess Date: Sun, 22 Nov 2020 17:45:33 -0300 Subject: [PATCH] Add location properties to mapbox reverse geocode (#286) * add location properties to mapbox reverse geocode * fix formatting --- src/geocoders/mapbox.ts | 38 +++++++++++++++++++++----------------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/src/geocoders/mapbox.ts b/src/geocoders/mapbox.ts index e6492474..eb3a5e0f 100644 --- a/src/geocoders/mapbox.ts +++ b/src/geocoders/mapbox.ts @@ -21,6 +21,24 @@ export class Mapbox implements GeocoderAPI { this.options.reverseQueryParams.access_token = accessToken; } + _getProperties(loc) { + const properties = { + text: loc.text, + address: loc.address + }; + + for (let j = 0; j < (loc.context || []).length; j++) { + const id = loc.context[j].id.split('.')[0]; + properties[id] = loc.context[j].text; + + // Get country code when available + if (loc.context[j].short_code) { + properties['countryShortCode'] = loc.context[j].short_code; + } + } + return properties; + } + geocode(query: string, cb: GeocodingCallback, context?: any): void { const params = this.options.geocodingQueryParams; if ( @@ -46,26 +64,11 @@ export class Mapbox implements GeocoderAPI { bbox = L.latLngBounds(center, center); } - const properties = { - text: loc.text, - address: loc.address - }; - - for (let j = 0; j < (loc.context || []).length; j++) { - const id = loc.context[j].id.split('.')[0]; - properties[id] = loc.context[j].text; - - // Get country code when available - if (loc.context[j].short_code) { - properties['countryShortCode'] = loc.context[j].short_code; - } - } - results[i] = { name: loc.place_name, bbox: bbox, center: center, - properties: properties + properties: this._getProperties(loc) }; } } @@ -109,7 +112,8 @@ export class Mapbox implements GeocoderAPI { results[i] = { name: loc.place_name, bbox: bbox, - center: center + center: center, + properties: this._getProperties(loc) }; } }