api.js
2.28 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// set environment variables
//export GOOGLE_PLACES_API_KEY = "AIzaSyAFbdNtVeHHzP0RYu9efx6QLCgbMK1dzP0"
//export GOOGLE_PLACES_OUTPUT_FORMAT = "json"
// var assert = require("assert");
/*
const googleMapsClient = require('@google/maps').createClient({
key: 'AIzaSyAFbdNtVeHHzP0RYu9efx6QLCgbMK1dzP0'
});
// Geocode an address.
googleMapsClient.geocode({
address: '1600 Amphitheatre Parkway, Mountain View, CA'
}, function(err, response) {
if (!err) {
console.log(response.json.results);
}
});
*/
// google place api 연동
// exports.apiKey = process.env.GOOGLE_PLACES_API_KEY;
// exports.outputFormat = process.env.GOOGLE_PLACES_OUTPUT_FORMAT
function ipLookUp () {
$.ajax('http://ip-api.com/json')
.then(
function success(response) {
console.log('User\'s Location Data is ', response);
console.log('User\'s Country', response.country);
getAdress(response.lat, response.lon)
},
function fail(data, status) {
console.log('Request failed. Returned status of',
status);
}
);
}
function getAddress (latitude, longitude) {
$.ajax('https://maps.googleapis.com/maps/api/geocode/json?' +
'latlng=' + latitude + ',' + longitude + '&key=' +
AIzaSyAFbdNtVeHHzP0RYu9efx6QLCgbMK1dzP0)
.then(
function success (response) {
console.log('User\'s Address Data is ', response)
},
function fail (status) {
console.log('Request failed. Returned status of',
status)
}
)
}
if ("geolocation" in navigator) {
// check if geolocation is supported/enabled on current browser
navigator.geolocation.getCurrentPosition(
function success(position) {
// for when getting location is a success
console.log('latitude', position.coords.latitude,
'longitude', position.coords.longitude);
getAddress(position.coords.latitude,
position.coords.longitude)
},
function error(error_message) {
// for when getting location results in an error
console.error('An error has occured while retrieving' +
'location', error_message)
ipLookUp()
});
}
else {
// geolocation is not supported
// get your location some other way
console.log('geolocation is not enabled on this browser')
ipLookUp()
}