google-geo-decoder
Advanced tools
Comparing version 1.0.4 to 1.0.5
{ | ||
"name": "google-geo-decoder", | ||
"version": "1.0.4", | ||
"version": "1.0.5", | ||
"description": "Simple google geo decoder.", | ||
@@ -16,3 +16,5 @@ "main": "./src/index.js", | ||
"geo", | ||
"decoder" | ||
"decoder", | ||
"map", | ||
"coordinates" | ||
], | ||
@@ -19,0 +21,0 @@ "author": "kurakste", |
@@ -1,5 +0,16 @@ | ||
Simple google geo decoder. | ||
# Simple google geo decoder. | ||
Just helper for google geo decoder. | ||
It gets coordinate and returns address object. | ||
Usage: | ||
console.log(geoDecoder(lon, lat, key)); | ||
## Installation | ||
$ npm install google-geo-decoder | ||
## Getting started | ||
``` | ||
const [lon, lat] = [55.635270, 51.808362]; | ||
const key = process.env.GGKEY; // You google API key. | ||
const res = geoDecoder(lon, lat, key)); | ||
returns: | ||
@@ -13,3 +24,4 @@ { | ||
route: "Ulitsa Muzykal'naya", | ||
streetNumber: '13' | ||
} | ||
streetNumber: '49', | ||
} | ||
``` |
@@ -1,44 +0,4 @@ | ||
const https = require('https'); | ||
const getAddressObject = require('./getAddressObject'); | ||
const getRequest = require('./getRequest'); | ||
function extractByType(type, googleGeoResp) { | ||
const out = googleGeoResp.results[0].address_components | ||
.filter(el => el | ||
.types.indexOf(type) !== -1); | ||
if (out.length === 0) return null; | ||
return out[0].long_name; | ||
} | ||
function getAddressObject(googleResp) { | ||
return { | ||
postalCode: extractByType('postal_code', googleResp), | ||
country: extractByType('country', googleResp), | ||
lev1: extractByType('administrative_area_level_1', googleResp), | ||
lev2: extractByType('administrative_area_level_2', googleResp), | ||
lev3: extractByType('administrative_area_level_3', googleResp), | ||
route: extractByType('route', googleResp), | ||
streetNumber: extractByType('street_number', googleResp), | ||
}; | ||
} | ||
function getRequest(url) { | ||
return new Promise((resolve, reject) => { | ||
https.get(url, resp => { | ||
let data = ''; | ||
resp.on('data', chunk => { | ||
data += chunk; | ||
}); | ||
resp.on('end', () => { | ||
try { | ||
const out = JSON.parse(data); | ||
resolve(out); | ||
} catch (err) { | ||
reject(err); | ||
} | ||
}); | ||
resp.on('error', (err) => reject(err)); | ||
}); | ||
}); | ||
} | ||
async function geoDecoder(lat, lon, key) { | ||
@@ -45,0 +5,0 @@ if (!key) throw new Error('Google API key is required.'); |
@@ -11,3 +11,3 @@ const dotenv = require('dotenv'); | ||
const locations = [ | ||
{ name: 'nizhnekamsk', loc: [55.635270, 51.808362] }, | ||
{ name: 'Nizhnekamsk', loc: [55.635270, 51.808362] }, | ||
// { name: 'kazan', loc: [55.819027, 49.050113] }, | ||
@@ -14,0 +14,0 @@ // { name: 'road tatarstan', loc: [55.664437, 49.854521] }, |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
4476
8
78
26