Skip to content

Commit d8cbaa7

Browse files
author
swiftcomplete-public
committed
Added extra examples to readme documentation
1 parent 7f7258c commit d8cbaa7

File tree

2 files changed

+120
-13
lines changed

2 files changed

+120
-13
lines changed

README.md

Lines changed: 80 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# reverse-geocode
1+
# Swiftcomplete Reverse Geocoding
22
## Description
33
This package provides reverse geocoding using the **[Swiftcomplete Places API](https://www.swiftcomplete.com/places/address-autocomplete/)**.
44

@@ -21,22 +21,97 @@ const swiftcompleteReverseGeocoder = require('@swiftcomplete/reverse-geocode');
2121

2222
swiftcompleteReverseGeocoder.setAPIKey('INSERT-KEY-HERE');
2323

24-
swiftcompleteReverseGeocoder.reverseGeocode('52.955771,-1.142881').then(function(results) {
24+
swiftcompleteReverseGeocoder.reverseGeocode('51.499403,-0.127362').then(function(results) {
25+
console.log(results);
26+
});
27+
```
28+
29+
## Response
30+
```json
31+
[
32+
{
33+
"primary":{
34+
"text":"Westminster Abbey",
35+
"highlights":[]
36+
},
37+
"secondary":{
38+
"text":"London",
39+
"highlights":[]
40+
},
41+
"type":"address.residential.building.data.emptyroad",
42+
"isContainer":false,
43+
"geometry":{
44+
"centre":{
45+
"lat":51.499462,
46+
"lon":-0.127448,
47+
"type":"address"
48+
}
49+
},
50+
"distance":{
51+
"units":"m",
52+
"measurement":9,
53+
"type":"biasTowards",
54+
"geometry":{
55+
"centre":{
56+
"lat":51.499403,
57+
"lon":-0.127362
58+
}
59+
}
60+
},
61+
"populatedRecord":{
62+
"lines":[
63+
"Westminster Abbey",
64+
"",
65+
"London",
66+
"SW1P 3PA",
67+
"United Kingdom"
68+
],
69+
"label":"Westminster Abbey\nLondon\nSW1P 3PA\nUnited Kingdom"
70+
}
71+
}
72+
]
73+
```
74+
75+
## Response field descriptions
76+
77+
- primary.text - A simple description of the address, usually the building & street
78+
- secondary.text - A simple description of the location, usually the city
79+
- geometry.centre - The coordinates of the address
80+
- geometry.centre.type - The accuracy of the coordinates, either "address", "street" or "postcode"
81+
- distance - How far the address is from your coordinates
82+
- distance.geometry.centre - Your original coordinates
83+
- populatedRecord.lines - The fully formatted postal address, line by line
84+
- populatedRecord.label - The fully formatted postal address, in a simple label format
85+
86+
## Customising the response
87+
88+
It's possible to pass in an optional options object to customise the response. Each field is optional within the object:
89+
90+
- maxResults - Max number of results to return (up to 5, default: 1). Note that you are billed per result, so 4 results = 4 charges.
91+
- distanceUnits - Unit of measurement to display how far away the address is from your coordinate ("metric", "imperial", "m", "km", "ft", "mi", default: "metric")
92+
93+
```js
94+
const swiftcompleteReverseGeocoder = require('@swiftcomplete/reverse-geocode');
95+
96+
swiftcompleteReverseGeocoder.setAPIKey('INSERT-KEY-HERE');
97+
98+
swiftcompleteReverseGeocoder.reverseGeocode('51.499403,-0.127362', {
99+
maxResults: 5,
100+
distanceUnits: "ft"
101+
}).then(function(results) {
25102
console.log(results);
26103
});
27104
```
28105

29106
## Data coverage
30107

31-
Swiftcomplete reverse geocoding is currently available in the following countries:
108+
Swiftcomplete reverse geocoding is currently available in the United Kingdom. We'll be adding the following countries shortly:
32109

33110
- Denmark
34111
- France
35112
- Liechtenstein
36113
- Luxembourg
37114
- Norway
38-
- United Kingdom
39-
40115

41116
We regularly update and expand our data coverage - **[contact us](https://www.swiftcomplete.com/contact-us/)** if there's a country or dataset that isn't listed and we'll let you know where it is on our priority list.
42117

index.js

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,34 @@ async function makeRequest(url) {
2525
});
2626
}
2727

28+
function parseDistanceUnits(str) {
29+
let result = '';
30+
let parsedStr = str.toLowerCase().trim();
31+
32+
switch (parsedStr) {
33+
case 'metric':
34+
result = 'metric';
35+
break;
36+
case 'imperial':
37+
result = 'imperial';
38+
break;
39+
case 'km':
40+
result = 'km';
41+
break;
42+
case 'm':
43+
result = 'm';
44+
break;
45+
case 'mi':
46+
result = 'mi';
47+
break;
48+
case 'ft':
49+
result = 'ft';
50+
break;
51+
}
52+
53+
return result;
54+
}
55+
2856
/**
2957
* Sets the API key to authenticate your request to the Swiftcomplete API. You can obtain an API key at https://www.swiftcomplete.com
3058
* @param key
@@ -39,29 +67,33 @@ exports.setAPIKey = function (key) {
3967

4068
/**
4169
* Returns the nearest address, street or places to a coordinate
42-
* @param {String} coordinate A WGS84 coordinate, in the format "latitude,longitude"
70+
* @param {string} coordinate A WGS84 coordinate, in the format "latitude,longitude"
4371
* @param {Object} options Optional options object
44-
* @param {Integer} options.maxResults Max number of results to return (up to 5). Note that you are billed per result, so 4 results = 4 charges.
72+
* @param {("metric"|"imperial"|"m"|"km"|"ft"|"mi")} [options.distanceUnits="metric"] Unit of measurement to display how far away the address is from your coordinate
73+
* @param {number} [options.maxResults=1] Max number of results to return (up to 5). Note that you are billed per result, so 4 results = 4 charges.
4574
* @returns {Promise} Array of matching results
4675
*/
4776
exports.reverseGeocode = function (coordinate, options) {
4877
if (apiKey.length == 0)
4978
throw 'You must set your API key first by calling setAPIKey(key)';
5079

51-
let requestOptions = {
52-
maxResults: 1
53-
};
80+
let url = `https://api.swiftcomplete.com/v1/places/?key=${apiKey}&searchMode=reverseGeocode&biasTowards=${coordinate}`;
5481

5582
if (options) {
83+
if ('distanceUnits' in options) {
84+
let parsedDistanceUnits = parseDistanceUnits(options.distanceUnits);
85+
86+
if (parsedDistanceUnits.length > 0)
87+
url += `&distanceUnits=${parsedDistanceUnits}`;
88+
}
89+
5690
if ('maxResults' in options) {
5791
let parsedMaxResults = parseInt(options.maxResults);
5892

5993
if (!isNaN(parsedMaxResults) && parsedMaxResults >= 1 && parsedMaxResults <= 5)
60-
requestOptions.maxResults = options.maxResults;
94+
url += `&maxResults=${parsedMaxResults}`;
6195
}
6296
}
6397

64-
let url = `https://api.swiftcomplete.com/v1/places/?key=${apiKey}&biasTowards=${coordinate}&maxResults=${requestOptions.maxResults}&populateIndex=0`;
65-
6698
return makeRequest(url);
6799
}

0 commit comments

Comments
 (0)