Skip to content

Commit 55a5d7b

Browse files
add useGeocoderV6 flag that allows to use v6 endpoint instead of v5 for geocoding
1 parent 5ac5826 commit 55a5d7b

File tree

7 files changed

+16363
-10459
lines changed

7 files changed

+16363
-10459
lines changed

debug/index.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ var geocoder = new MapboxGeocoder({
7575
accessToken: mapboxgl.accessToken,
7676
trackProximity: true,
7777
enableGeolocation: true,
78+
useGeocoderV6: true,
7879
localGeocoder: function(query) {
7980
return coordinatesGeocoder(query);
8081
},
@@ -85,7 +86,18 @@ var geocoder = new MapboxGeocoder({
8586
.then(response => response.json())
8687
}
8788
},
88-
mapboxgl: mapboxgl
89+
mapboxgl: mapboxgl,
90+
searchType: 'structured',
91+
queryCoordinates: {longitude: 40.76490756883251, latitude: -74.0437572064368},
92+
address_line1: 'address_line1',
93+
address_number: 'address_number',
94+
street: 'street',
95+
place: 'place',
96+
region: 'region',
97+
countries: 'US',
98+
postcode: 'postcode',
99+
locality: 'locality',
100+
permanent: true
89101
});
90102

91103
map.addControl(geocoder)

debug/mock-api.json

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,58 @@
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+
"geometry": {
5+
"type": "Point",
6+
"coordinates": [-97.61568, 51.084213]
7+
},
8+
"properties": {
9+
"mapbox_id": "address.5243356057056628",
10+
"feature_type": "address",
11+
"name": "12 Main Street",
12+
"coordinates": {
13+
"longitude": -97.61568,
14+
"latitude": 51.084213,
15+
"accuracy": "point"
16+
},
17+
"place_formatted": "Fisher Branch, Manitoba R0C 0Z0, Canada",
18+
"street": "Main Street",
19+
"address_number": "12",
20+
"match_code": {
21+
"exact_match": false,
22+
"house_number": "matched",
23+
"street": "unmatched",
24+
"postcode": "unmatched",
25+
"place": "unmatched",
26+
"region": "unmatched",
27+
"locality": "not_applicable",
28+
"country": "inferred",
29+
"confidence": "low"
30+
},
31+
"context": {
32+
"postcode": {
33+
"id": "postcode.5061127719",
34+
"name": "R0C 0Z0"
35+
},
36+
"place": {
37+
"id": "place.24258599",
38+
"name": "Fisher Branch",
39+
"wikidata_id": "Q5454766"
40+
},
41+
"region": {
42+
"id": "region.25639",
43+
"name": "Manitoba",
44+
"wikidata_id": "Q1948",
45+
"region_code": "MB",
46+
"region_code_full": "CA-MB"
47+
},
48+
"country": {
49+
"id": "country.8743",
50+
"name": "Canada",
51+
"wikidata_id": "Q16",
52+
"country_code": "CA",
53+
"country_code_alpha_3": "CAN"
54+
}
55+
}
56+
}
57+
}
58+
]

lib/geolocation-v6.js

Lines changed: 1507 additions & 0 deletions
Large diffs are not rendered by default.

lib/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ var localization = require('./localization');
1212
var subtag = require('subtag');
1313
var Geolocation = require('./geolocation');
1414
var utils = require('./utils');
15+
var MapboxGeocoderV6 = require('./geolocation-v6.js');
1516

1617

1718
const GEOCODE_REQUEST_TYPE = {
@@ -75,6 +76,7 @@ function getFooterNode() {
7576
* @param {Boolean} [options.routing=false] Specify whether to request additional metadata about the recommended navigation destination corresponding to the feature or not. Only applicable for address features.
7677
* @param {String} [options.worldview="us"] Filter results to geographic features whose characteristics are defined differently by audiences belonging to various regional, cultural, or political groups.
7778
* @param {Boolean} [options.enableGeolocation=false] If `true` enable user geolocation feature.
79+
* @param {Boolean} [options.useGeocoderV6=false] If `true` use v6 endpoint instead of v5.
7880
* @param {('address'|'street'|'place'|'country')} [options.addressAccuracy="street"] The accuracy for the geolocation feature with which we define the address line to fill. The browser API returns the user's position with accuracy, and sometimes we can get the neighbor's address. To prevent receiving an incorrect address, you can reduce the accuracy of the definition.
7981
* @example
8082
* var geocoder = new MapboxGeocoder({ accessToken: mapboxgl.accessToken });
@@ -90,6 +92,7 @@ function MapboxGeocoder(options) {
9092
this.fresh = true;
9193
this.lastSelected = null;
9294
this.geolocation = new Geolocation();
95+
return this.options.useGeocoderV6 ? new MapboxGeocoderV6(options) : this;
9396
}
9497

9598
MapboxGeocoder.prototype = {

lib/utils.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,18 @@ function transformFeatureToGeolocationText(feature, accuracy) {
3434
return acc + addrInfo[name];
3535
}, '');
3636
}
37+
38+
const isV5Feature = (feature) => "id" in feature;
39+
3740
/**
3841
* This function transforms the feature from reverse geocoding to AddressInfo object
3942
* @param {object} feature
4043
* @returns {object}
4144
*/
4245
function getAddressInfo(feature) {
46+
if(!isV5Feature(feature)) {
47+
return getV6AddressInfo(feature);
48+
}
4349
const houseNumber = feature.address || '';
4450
const street = feature.text || '';
4551
const placeName = feature.place_name || '';
@@ -60,6 +66,34 @@ function getAddressInfo(feature) {
6066
return addrInfo;
6167
}
6268

69+
/**
70+
* This function transforms the v6 feature from reverse geocoding to AddressInfo object
71+
* @param {object} feature
72+
* @returns {object}
73+
*/
74+
function getV6AddressInfo(feature) {
75+
const houseNumber = feature.address_number || "";
76+
const street = feature.street || "";
77+
const placeName = feature.place_formatted || "";
78+
const address = placeName.split(",")[0];
79+
80+
const addrInfo = {
81+
address: address,
82+
houseNumber: houseNumber,
83+
street: street,
84+
placeName: placeName,
85+
};
86+
87+
for (const contextProp in feature.context) {
88+
if (contextProp) {
89+
const layer = contextProp.id.split(".")[0];
90+
addrInfo[layer] = contextProp.text;
91+
}
92+
}
93+
94+
return addrInfo;
95+
}
96+
6397
const REVERSE_GEOCODE_COORD_RGX = /^[ ]*(-?\d{1,3}(\.\d{0,256})?)[, ]+(-?\d{1,3}(\.\d{0,256})?)[ ]*$/;
6498

6599
module.exports = {

0 commit comments

Comments
 (0)