June 6, 2019
GeoLocation API in Javascript
GeoLocation API in Javascript
In this video tutorial, you will learn how to use Geolocation api in javascript.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <body> <button onclick="getGeoLocation()">Find My Location</button> <br><br> <div style="display:none;"> <a href="#">Click Here</a> </div> <script src="script.js"></script> </body> </html> |
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 |
function getGeoLocation(){ if(navigator.geolocation){ navigator.geolocation.getCurrentPosition(onSuccess, onError); }else { console.log('Not Supported'); } } function onSuccess(position){ // const {latitude, longitude} = position.coords; const {latitude, longitude} = { latitude: '40.785091', longitude: '-73.968285' }; const url = `https://www.latlong.net/c/?lat=${latitude}&long=${longitude}`; document.querySelector('a').setAttribute('href', url); document.querySelector('div').style.display = 'block'; } function onError(error){ console.log(error); } |