-
Notifications
You must be signed in to change notification settings - Fork 0
/
geo.js
54 lines (46 loc) · 1.62 KB
/
geo.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
const gooleGeoURL = `https://maps.googleapis.com/maps/api/geocode/json?address=91740`
const axios = require('axios');
const config = require('../../config/config');
const mapQuestKey = config.mapQuestNodeWeatherKey;
var geoData = {
name: null,
lat: null, //34.135653,
lng: null //-117.8287845
};
var fetchGeoData = (address) => {
const encodedAddress = encodeURIComponent(address);
const geoCodeURL = `http://www.mapquestapi.com/geocoding/v1/address?key=${mapQuestKey}&location=${encodedAddress}`;
return axios.get(geoCodeURL)
.then((response) => {
let errorcode = response.data.info.statuscode;
if ( errorcode === 400) {
throw new Error('Input error. You probably typed something wrong. Make sure you are passing a valid URL.');
} else if (errorcode === 403) {
throw new Error('Something is wrong with the API key. Make sure it is valid.');
} else if (errorcode === 500) {
throw new Error('Make sure you are entering a valid location. Could be a server error. Not really sure.')
}
setLocationData(response.data.results[0].locations[0]);
return geoData;
})
.catch((err)=>{
if(err.code === 'ENOTFOUND') {
console.log("Unable to connect to MapQuest server.");
} else {
console.log(err.message);
}
});
};
var getGeoData = () => {
return geoData;
};
var setLocationData = (geoResults) => {
//geoReults is response.data.results[0]
geoData.name = geoResults.adminArea5;
geoData.lat = geoResults.latLng.lat;
geoData.lng = geoResults.latLng.lng;
};
module.exports = {
fetchGeoData,
getGeoData
};