Created
August 13, 2019 12:08
-
-
Save manuelgeek/239ff989a7152ec3dd7db667865ed246 to your computer and use it in GitHub Desktop.
simple JS to get location, front end ......>>>> vue
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ipLookUp() { | |
axios.get('http://ip-api.com/json',{},{headers: { | |
'Access-Control-Allow-Origin': '*', | |
'Access-Control-Allow-Methods': 'GET, PUT, POST, DELETE, OPTIONS', | |
'Content-Type': 'application/json', | |
}}) | |
.then(response => { | |
console.log('User\'s Location Data is ', response); | |
console.log('User\'s Country', response.data.country); | |
this.getAddress(response.data.lat, response.data.lon) | |
}) | |
.catch( error => { | |
console.log('Request failed. Returned status of', error); | |
}); | |
}, | |
getAddress(latitude, longitude) { | |
axios.get('https://maps.googleapis.com/maps/api/geocode/json?latlng=' + latitude + ',' + longitude + '&key=' + process.env.MIX_GOOGLE_MAP_KEY) | |
.then(response => { | |
console.log('User\'s Address Data is ', response) | |
}) | |
.catch( error => { | |
console.log('Request failed. Returned status of', error); | |
}); | |
}, | |
getUserLocation(){ | |
var vm = this; | |
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); | |
vm.getAddress(position.coords.latitude, | |
position.coords.longitude) | |
}, | |
function error(error_message) { | |
// for when getting location results in an error | |
console.error('An error has occurred while retrieving location',error_message) | |
vm.ipLookUp() | |
}); | |
} else { | |
// geolocation is not supported | |
// get your location some other way | |
console.log('geolocation is not enabled on this browser') | |
vm.ipLookUp() | |
} | |
}, |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment