Skip to content

Commit

Permalink
fix potential problem with address strings for v5 and v6
Browse files Browse the repository at this point in the history
  • Loading branch information
VladislavMedved committed May 10, 2023
1 parent f9a6990 commit 64f3267
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 9 deletions.
67 changes: 65 additions & 2 deletions debug/mock-api.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,66 @@
[
{"id":"place.7673410831246050","type":"Feature","place_type":["place"],"relevance":1,"properties":{"wikidata":"Q61"},"text_en-US":"Washington","language_en-US":"en","place_name_en-US":"Washington, District of Columbia, United States of America","text":"Washington","language":"en","place_name":"SERVER: Washington, District of Columbia, United States of America","matching_place_name":"Washington, DC, United States of America","bbox":[-77.1197609567342,38.79155738,-76.909391,38.99555093],"center":[-77.0366,38.895],"geometry":{"type":"Point","coordinates":[-77.0366,38.895]},"context":[{"id":"region.14064402149979320","short_code":"US-DC","wikidata":"Q3551781","text_en-US":"District of Columbia","language_en-US":"en","text":"District of Columbia","language":"en"},{"id":"country.19678805456372290","wikidata":"Q30","short_code":"us","text_en-US":"United States of America","language_en-US":"en","text":"United States of America","language":"en"}]}
]
{
"type": "Feature",
"id": "address.7840146147075390",
"geometry": {
"type": "Point",
"coordinates": [22.476772, -33.991656]
},
"properties": {
"mapbox_id": "address.7840146147075390",
"feature_type": "address",
"name": "12 Main Street",
"coordinates": {
"longitude": 22.476772,
"latitude": -33.991656,
"accuracy": "proximate"
},
"place_formatted": "George, Western Cape 6529, South Africa",
"match_code": {
"address_number": "matched",
"street": "unmatched",
"postcode": "unmatched",
"place": "unmatched",
"region": "unmatched",
"locality": "not_applicable",
"country": "inferred",
"confidence": "low"
},
"context": {
"address": {
"id": "address.7840146147075390",
"street": "Main Street",
"address_number": "12",
"name": "12 Main Street"
},
"neighborhood": {
"id": "neighborhood.1846526",
"name": "Ballotsview"
},
"postcode": {
"id": "postcode.24039166",
"name": "6529"
},
"place": {
"id": "place.24643838",
"name": "George",
"wikidata_id": "Q370456"
},
"region": {
"id": "region.9470",
"name": "Western Cape",
"wikidata_id": "Q127167",
"region_code": "WC",
"region_code_full": "ZA-WC"
},
"country": {
"id": "country.8958",
"name": "South Africa",
"wikidata_id": "Q258",
"country_code": "ZA",
"country_code_alpha_3": "ZAF"
}
}
}
}
]
6 changes: 5 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,11 @@ MapboxGeocoder.prototype = {
const feature = resp.body.features[0];

if (feature) {
const locationText = utils.transformFeatureToGeolocationText(feature, this.options.addressAccuracy);
const locationText = utils.transformFeatureToGeolocationText(
feature,
this.options.addressAccuracy,
this.options.version
);
this._setInputValue(locationText);

feature.user_coordinates = geojson.geometry.coordinates;
Expand Down
12 changes: 6 additions & 6 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
* This function transforms the feature from reverse geocoding to plain text with specified accuracy
* @param {object} feature
* @param {string} accuracy
* @param {'v5'|'v6'} version
* @returns
*/
function transformFeatureToGeolocationText(feature, accuracy) {
const addrInfo = getAddressInfo(feature);
function transformFeatureToGeolocationText(feature, accuracy, version) {
const addrInfo = getAddressInfo(feature, version);

const addressAccuracy = ['address', 'street', 'place', 'country'];
var currentAccuracy;
Expand Down Expand Up @@ -35,15 +36,14 @@ function transformFeatureToGeolocationText(feature, accuracy) {
}, '');
}

const isV5Feature = (feature) => "id" in feature;

/**
* This function transforms the feature from reverse geocoding to AddressInfo object
* @param {object} feature
* @param {'v5'|'v6'} version
* @returns {object}
*/
function getAddressInfo(feature) {
if(!isV5Feature(feature)) {
function getAddressInfo(feature, version) {
if(version === 'v6') {
return getV6AddressInfo(feature);
}
const houseNumber = feature.address || '';
Expand Down

0 comments on commit 64f3267

Please sign in to comment.