Showing
1 changed file
with
80 additions
and
0 deletions
api.js
0 → 100644
1 | +// set environment variables | ||
2 | +//export GOOGLE_PLACES_API_KEY = "AIzaSyAFbdNtVeHHzP0RYu9efx6QLCgbMK1dzP0" | ||
3 | +//export GOOGLE_PLACES_OUTPUT_FORMAT = "json" | ||
4 | + | ||
5 | +// var assert = require("assert"); | ||
6 | + | ||
7 | +/* | ||
8 | +const googleMapsClient = require('@google/maps').createClient({ | ||
9 | + key: 'AIzaSyAFbdNtVeHHzP0RYu9efx6QLCgbMK1dzP0' | ||
10 | +}); | ||
11 | + | ||
12 | +// Geocode an address. | ||
13 | +googleMapsClient.geocode({ | ||
14 | + address: '1600 Amphitheatre Parkway, Mountain View, CA' | ||
15 | +}, function(err, response) { | ||
16 | + if (!err) { | ||
17 | + console.log(response.json.results); | ||
18 | + } | ||
19 | +}); | ||
20 | + | ||
21 | +*/ | ||
22 | +// google place api 연동 | ||
23 | +// exports.apiKey = process.env.GOOGLE_PLACES_API_KEY; | ||
24 | +// exports.outputFormat = process.env.GOOGLE_PLACES_OUTPUT_FORMAT | ||
25 | + | ||
26 | + | ||
27 | +function ipLookUp () { | ||
28 | + $.ajax('http://ip-api.com/json') | ||
29 | + .then( | ||
30 | + function success(response) { | ||
31 | + console.log('User\'s Location Data is ', response); | ||
32 | + console.log('User\'s Country', response.country); | ||
33 | + getAdress(response.lat, response.lon) | ||
34 | +}, | ||
35 | + | ||
36 | + function fail(data, status) { | ||
37 | + console.log('Request failed. Returned status of', | ||
38 | + status); | ||
39 | + } | ||
40 | + ); | ||
41 | +} | ||
42 | + | ||
43 | +function getAddress (latitude, longitude) { | ||
44 | + $.ajax('https://maps.googleapis.com/maps/api/geocode/json?' + | ||
45 | + 'latlng=' + latitude + ',' + longitude + '&key=' + | ||
46 | + AIzaSyAFbdNtVeHHzP0RYu9efx6QLCgbMK1dzP0) | ||
47 | + .then( | ||
48 | + function success (response) { | ||
49 | + console.log('User\'s Address Data is ', response) | ||
50 | + }, | ||
51 | + function fail (status) { | ||
52 | + console.log('Request failed. Returned status of', | ||
53 | + status) | ||
54 | + } | ||
55 | + ) | ||
56 | +} | ||
57 | + | ||
58 | +if ("geolocation" in navigator) { | ||
59 | + // check if geolocation is supported/enabled on current browser | ||
60 | + navigator.geolocation.getCurrentPosition( | ||
61 | + function success(position) { | ||
62 | + // for when getting location is a success | ||
63 | + console.log('latitude', position.coords.latitude, | ||
64 | + 'longitude', position.coords.longitude); | ||
65 | + getAddress(position.coords.latitude, | ||
66 | + position.coords.longitude) | ||
67 | + }, | ||
68 | + function error(error_message) { | ||
69 | + // for when getting location results in an error | ||
70 | + console.error('An error has occured while retrieving' + | ||
71 | + 'location', error_message) | ||
72 | + ipLookUp() | ||
73 | + }); | ||
74 | +} | ||
75 | +else { | ||
76 | + // geolocation is not supported | ||
77 | + // get your location some other way | ||
78 | + console.log('geolocation is not enabled on this browser') | ||
79 | + ipLookUp() | ||
80 | +} | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
-
Please register or login to post a comment