ola-map-sdk 1.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +137 -0
- package/package.json +21 -0
- package/src/client.js +25 -0
- package/src/endpoints/places.js +33 -0
- package/src/endpoints/routing.js +38 -0
- package/src/endpoints/tiles.js +55 -0
- package/src/index.js +5 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Kunal Kongkan Kashyap
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
# Ola Map SDK
|
|
2
|
+
Ola Map SDK is a Node.js package that provides an easy-to-use interface for interacting with the Ola Maps API. This SDK allows you to perform various operations like geocoding, reverse geocoding, fetching map tiles, and getting routing directions.
|
|
3
|
+
|
|
4
|
+
## Installation
|
|
5
|
+
Install the package using npm:
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install ola-map-sdk
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
First, you need to require the SDK and create an instance of the OlaMapsClient class with your API key.
|
|
13
|
+
|
|
14
|
+
```javascript
|
|
15
|
+
const OlaMapsClient = require('ola-map-sdk');
|
|
16
|
+
|
|
17
|
+
const apiKey = 'YOUR_API_KEY';
|
|
18
|
+
const client = new OlaMapsClient(apiKey);
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
### Places API
|
|
22
|
+
API for accessing geolocation services such as places autocomplete, reverse geocode.
|
|
23
|
+
|
|
24
|
+
- **Autocomplete** - Provides Autocomplete suggestions for a given substring satisfying the given criteria.
|
|
25
|
+
|
|
26
|
+
```javascript
|
|
27
|
+
try {
|
|
28
|
+
const result = await client.places.autocomplete('Gauhati University');
|
|
29
|
+
console.log('Autocomplete result:', result);
|
|
30
|
+
} catch (error) {
|
|
31
|
+
console.error('Error during autocomplete:', error);
|
|
32
|
+
}
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
- **Geocode** - Returns the geocoded address based on the provided parameters.
|
|
36
|
+
|
|
37
|
+
```javascript
|
|
38
|
+
try {
|
|
39
|
+
const result = await client.places.geocode('Taj Mahal, Mumbai');
|
|
40
|
+
console.log('Geocode result:', result);
|
|
41
|
+
} catch (error) {
|
|
42
|
+
console.error('Error during geocoding:', error);
|
|
43
|
+
}
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
- **Reverse Geocode** - Provides information of a place based on the location provided satisfying the given criteria.
|
|
47
|
+
|
|
48
|
+
```javascript
|
|
49
|
+
try {
|
|
50
|
+
const result = await client.places.reverseGeocode(26.115103, 91.703239);
|
|
51
|
+
console.log('Reverse Geocode result:', result);
|
|
52
|
+
} catch (error) {
|
|
53
|
+
console.error('Error during reverse geocoding:', error);
|
|
54
|
+
}
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
### Direction API
|
|
58
|
+
|
|
59
|
+
- **Get Directions** - Provides routable path between two or more points. Accepts coordinates in lat,long format.
|
|
60
|
+
|
|
61
|
+
```javascript
|
|
62
|
+
try {
|
|
63
|
+
const result = await client.routing.getDirections(
|
|
64
|
+
{ lat: 12.993103152916301, lon: 77.54332622119354 },
|
|
65
|
+
{ lat: 12.972006793201695, lon: 77.5800850011884 },
|
|
66
|
+
{
|
|
67
|
+
alternatives: false,
|
|
68
|
+
steps: true,
|
|
69
|
+
overview: 'full',
|
|
70
|
+
language: 'en',
|
|
71
|
+
traffic_metadata: false
|
|
72
|
+
}
|
|
73
|
+
);
|
|
74
|
+
console.log('Directions result:', result);
|
|
75
|
+
} catch (error) {
|
|
76
|
+
console.error('Error fetching directions:', error);
|
|
77
|
+
}
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
### Tiles API
|
|
82
|
+
API for accessing geolocation services such as places autocomplete, reverse geocode, turn by turn directions, map tiles in vector or image format.
|
|
83
|
+
|
|
84
|
+
- **Get Data TileJSON** - Get array of data's TileJSONs.
|
|
85
|
+
|
|
86
|
+
```javascript
|
|
87
|
+
try {
|
|
88
|
+
const result = await client.tiles.getDataTileJSON('planet');
|
|
89
|
+
console.log('Data TileJSON result:', result);
|
|
90
|
+
} catch (error) {
|
|
91
|
+
console.error('Error fetching Data TileJSON:', error);
|
|
92
|
+
}
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
- **Get PBF File** - Returns the PBF file for data.
|
|
96
|
+
|
|
97
|
+
```javascript
|
|
98
|
+
try {
|
|
99
|
+
const result = await client.tiles.getPBFFile('planet', 14, 110, 1010);
|
|
100
|
+
console.log('PBF File result:', result);
|
|
101
|
+
} catch (error) {
|
|
102
|
+
console.error('Error fetching PBF file:', error);
|
|
103
|
+
}
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
- **Get Styles** - Returns the styles for the map.
|
|
107
|
+
|
|
108
|
+
```javascript
|
|
109
|
+
try {
|
|
110
|
+
const result = await client.tiles.getStyles();
|
|
111
|
+
console.log('Styles result:', result);
|
|
112
|
+
} catch (error) {
|
|
113
|
+
console.error('Error fetching Styles:', error);
|
|
114
|
+
}
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
- **Get Style Detail** - Returns the style detail for the map.
|
|
118
|
+
|
|
119
|
+
```javascript
|
|
120
|
+
try {
|
|
121
|
+
const result = await client.tiles.getStyleDetail('default-light-standard');
|
|
122
|
+
console.log('Style Detail result:', result);
|
|
123
|
+
} catch (error) {
|
|
124
|
+
console.error('Error fetching Style Detail:', error);
|
|
125
|
+
}
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
- **Get Font Glyphs** - Returns the font glyphs for the map.
|
|
129
|
+
|
|
130
|
+
```javascript
|
|
131
|
+
try {
|
|
132
|
+
const result = await client.tiles.getFontGlyphs('Noto Sans Bold', 0, 255);
|
|
133
|
+
console.log('Font Glyphs result:', result);
|
|
134
|
+
} catch (error) {
|
|
135
|
+
console.error('Error fetching font glyphs:', error);
|
|
136
|
+
}
|
|
137
|
+
```
|
package/package.json
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "ola-map-sdk",
|
|
3
|
+
"version": "1.1.2",
|
|
4
|
+
"description": "A Node.js SDK for Ola Maps API",
|
|
5
|
+
"main": "src/index.js",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"ola",
|
|
8
|
+
"maps",
|
|
9
|
+
"sdk",
|
|
10
|
+
"ola maps sdk",
|
|
11
|
+
"ola maps api",
|
|
12
|
+
"ola maps sdk for node.js"
|
|
13
|
+
],
|
|
14
|
+
"author": "Kunal Kongkan Kashyap",
|
|
15
|
+
"license": "MIT",
|
|
16
|
+
"dependencies": {
|
|
17
|
+
"axios": "^1.7.2",
|
|
18
|
+
"dotenv": "^16.4.5",
|
|
19
|
+
"uuid": "^10.0.0"
|
|
20
|
+
}
|
|
21
|
+
}
|
package/src/client.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
const axios = require('axios');
|
|
2
|
+
const { v4: uuidv4 } = require('uuid');
|
|
3
|
+
const Places = require('./endpoints/places');
|
|
4
|
+
const Tiles = require('./endpoints/tiles');
|
|
5
|
+
const Routing = require('./endpoints/routing');
|
|
6
|
+
|
|
7
|
+
class OlaMapsClient {
|
|
8
|
+
constructor(apiKey) {
|
|
9
|
+
this.apiKey = apiKey || process.env.OLA_MAPS_API_KEY;
|
|
10
|
+
this.client = axios.create({
|
|
11
|
+
baseURL: 'https://api.olamaps.io',
|
|
12
|
+
headers: {
|
|
13
|
+
'accept': 'application/json',
|
|
14
|
+
'X-Request-Id': uuidv4(),
|
|
15
|
+
'X-Correlation-Id': uuidv4(),
|
|
16
|
+
},
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
this.places = new Places(this.client, this.apiKey);
|
|
20
|
+
this.tiles = new Tiles(this.client, this.apiKey);
|
|
21
|
+
this.routing = new Routing(this.client, this.apiKey);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
module.exports = OlaMapsClient;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
class Places {
|
|
2
|
+
constructor(client, apiKey) {
|
|
3
|
+
this.client = client;
|
|
4
|
+
this.apiKey = apiKey;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
async request(endpoint, params) {
|
|
8
|
+
try {
|
|
9
|
+
const response = await this.client.get(endpoint, { params: { ...params, api_key: this.apiKey } });
|
|
10
|
+
return response.data;
|
|
11
|
+
} catch (error) {
|
|
12
|
+
const errorMessage = error.response
|
|
13
|
+
? `Error: ${error.response.status} - ${error.response.data.message}`
|
|
14
|
+
: `Error: ${error.message}`;
|
|
15
|
+
console.error(errorMessage);
|
|
16
|
+
throw error;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
autocomplete(input) {
|
|
21
|
+
return this.request('/places/v1/autocomplete', { input });
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
geocode(address) {
|
|
25
|
+
return this.request('/places/v1/geocode', { address });
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
reverseGeocode(lat, lng) {
|
|
29
|
+
return this.request('/places/v1/reverse-geocode', { latlng: `${lat},${lng}` });
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
module.exports = Places;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
class Routing {
|
|
2
|
+
constructor(client, apiKey) {
|
|
3
|
+
this.client = client;
|
|
4
|
+
this.apiKey = apiKey;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
async getDirections(origin, destination, options = {}) {
|
|
8
|
+
const params = {
|
|
9
|
+
api_key: this.apiKey,
|
|
10
|
+
origin: `${origin.lat},${origin.lon}`,
|
|
11
|
+
destination: `${destination.lat},${destination.lon}`,
|
|
12
|
+
alternatives: options.alternatives || false,
|
|
13
|
+
steps: options.steps || false,
|
|
14
|
+
overview: options.overview || 'full',
|
|
15
|
+
language: options.language || 'en',
|
|
16
|
+
traffic_metadata: options.traffic_metadata || false,
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
try {
|
|
20
|
+
const response = await this.client.post('/routing/v1/directions', null, {
|
|
21
|
+
params,
|
|
22
|
+
headers: {
|
|
23
|
+
'accept': 'application/json',
|
|
24
|
+
},
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
return response.data;
|
|
28
|
+
} catch (error) {
|
|
29
|
+
const errorMessage = error.response
|
|
30
|
+
? `Error: ${error.response.status} - ${error.response.data.message}`
|
|
31
|
+
: `Error: ${error.message}`;
|
|
32
|
+
console.error(errorMessage);
|
|
33
|
+
throw error;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
module.exports = Routing;
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
class Tiles {
|
|
2
|
+
constructor(client, apiKey) {
|
|
3
|
+
this.client = client;
|
|
4
|
+
this.apiKey = apiKey;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
async request(endpoint, params, headers = {}, responseType = 'json') {
|
|
8
|
+
try {
|
|
9
|
+
const response = await this.client.get(endpoint, {
|
|
10
|
+
params: { ...params, api_key: this.apiKey },
|
|
11
|
+
headers: { 'accept': 'application/json', ...headers },
|
|
12
|
+
responseType,
|
|
13
|
+
});
|
|
14
|
+
return response.data;
|
|
15
|
+
} catch (error) {
|
|
16
|
+
const errorMessage = error.response
|
|
17
|
+
? `Error: ${error.response.status} - ${error.response.data.message}`
|
|
18
|
+
: `Error: ${error.message}`;
|
|
19
|
+
console.error(errorMessage);
|
|
20
|
+
throw error;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
getDataTileJSON(datasetName) {
|
|
25
|
+
return this.request(`/tiles/vector/v1/data/${datasetName}.json`);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
getPBFFile(datasetName, z, x, y) {
|
|
29
|
+
return this.request(
|
|
30
|
+
`/tiles/vector/v1/data/${datasetName}/${z}/${x}/${y}.pbf`,
|
|
31
|
+
{},
|
|
32
|
+
{ 'accept': 'application/x-protobuf' },
|
|
33
|
+
'arraybuffer'
|
|
34
|
+
);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
getStyles() {
|
|
38
|
+
return this.request('/tiles/vector/v1/styles.json');
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
getStyleDetail(styleName) {
|
|
42
|
+
return this.request(`/tiles/vector/v1/styles/${styleName}/style.json`);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
getFontGlyphs(fontstack, start, end) {
|
|
46
|
+
return this.request(
|
|
47
|
+
`/tiles/vector/v1/fonts/${encodeURIComponent(fontstack)}/${start}-${end}.pbf`,
|
|
48
|
+
{},
|
|
49
|
+
{ 'accept': 'application/x-protobuf' },
|
|
50
|
+
'arraybuffer'
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
module.exports = Tiles;
|