Skip to content

Commit 64f3267

Browse files
fix potential problem with address strings for v5 and v6
1 parent f9a6990 commit 64f3267

File tree

3 files changed

+76
-9
lines changed

3 files changed

+76
-9
lines changed

debug/mock-api.json

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,66 @@
11
[
2-
{"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"}]}
3-
]
2+
{
3+
"type": "Feature",
4+
"id": "address.7840146147075390",
5+
"geometry": {
6+
"type": "Point",
7+
"coordinates": [22.476772, -33.991656]
8+
},
9+
"properties": {
10+
"mapbox_id": "address.7840146147075390",
11+
"feature_type": "address",
12+
"name": "12 Main Street",
13+
"coordinates": {
14+
"longitude": 22.476772,
15+
"latitude": -33.991656,
16+
"accuracy": "proximate"
17+
},
18+
"place_formatted": "George, Western Cape 6529, South Africa",
19+
"match_code": {
20+
"address_number": "matched",
21+
"street": "unmatched",
22+
"postcode": "unmatched",
23+
"place": "unmatched",
24+
"region": "unmatched",
25+
"locality": "not_applicable",
26+
"country": "inferred",
27+
"confidence": "low"
28+
},
29+
"context": {
30+
"address": {
31+
"id": "address.7840146147075390",
32+
"street": "Main Street",
33+
"address_number": "12",
34+
"name": "12 Main Street"
35+
},
36+
"neighborhood": {
37+
"id": "neighborhood.1846526",
38+
"name": "Ballotsview"
39+
},
40+
"postcode": {
41+
"id": "postcode.24039166",
42+
"name": "6529"
43+
},
44+
"place": {
45+
"id": "place.24643838",
46+
"name": "George",
47+
"wikidata_id": "Q370456"
48+
},
49+
"region": {
50+
"id": "region.9470",
51+
"name": "Western Cape",
52+
"wikidata_id": "Q127167",
53+
"region_code": "WC",
54+
"region_code_full": "ZA-WC"
55+
},
56+
"country": {
57+
"id": "country.8958",
58+
"name": "South Africa",
59+
"wikidata_id": "Q258",
60+
"country_code": "ZA",
61+
"country_code_alpha_3": "ZAF"
62+
}
63+
}
64+
}
65+
}
66+
]

lib/index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,11 @@ MapboxGeocoder.prototype = {
393393
const feature = resp.body.features[0];
394394

395395
if (feature) {
396-
const locationText = utils.transformFeatureToGeolocationText(feature, this.options.addressAccuracy);
396+
const locationText = utils.transformFeatureToGeolocationText(
397+
feature,
398+
this.options.addressAccuracy,
399+
this.options.version
400+
);
397401
this._setInputValue(locationText);
398402

399403
feature.user_coordinates = geojson.geometry.coordinates;

lib/utils.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22
* This function transforms the feature from reverse geocoding to plain text with specified accuracy
33
* @param {object} feature
44
* @param {string} accuracy
5+
* @param {'v5'|'v6'} version
56
* @returns
67
*/
7-
function transformFeatureToGeolocationText(feature, accuracy) {
8-
const addrInfo = getAddressInfo(feature);
8+
function transformFeatureToGeolocationText(feature, accuracy, version) {
9+
const addrInfo = getAddressInfo(feature, version);
910

1011
const addressAccuracy = ['address', 'street', 'place', 'country'];
1112
var currentAccuracy;
@@ -35,15 +36,14 @@ function transformFeatureToGeolocationText(feature, accuracy) {
3536
}, '');
3637
}
3738

38-
const isV5Feature = (feature) => "id" in feature;
39-
4039
/**
4140
* This function transforms the feature from reverse geocoding to AddressInfo object
4241
* @param {object} feature
42+
* @param {'v5'|'v6'} version
4343
* @returns {object}
4444
*/
45-
function getAddressInfo(feature) {
46-
if(!isV5Feature(feature)) {
45+
function getAddressInfo(feature, version) {
46+
if(version === 'v6') {
4747
return getV6AddressInfo(feature);
4848
}
4949
const houseNumber = feature.address || '';

0 commit comments

Comments
 (0)