🪴 yshalsager's Digital Garden

Search

Search IconIcon to open search

JavaScript Geolocation API

Last updated Aug 3, 2022

المعرفة:: JavaScript
الحالة:: #ملاحظة_مؤرشفة
المراجع:: The Complete JavaScript Course 2022 From Zero to Expert, https://developer.mozilla.org/en-US/docs/Web/API/Geolocation_API, https://developer.mozilla.org/en-US/docs/Web/API/Geolocation/getCurrentPosition


# getCurrentPosition()

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
if (navigator.geolocation) {  
  navigator.geolocation.getCurrentPosition(  
    function (position) {  
      const { latitude } = position.coords;  
      const { longitude } = position.coords;  
      console.log(`https://www.google.com/maps/@${latitude},${longitude}`);  
    },  
    function () { alert('Could not get your position') });  
}  
  
/*  
GeolocationPosition {coords: GeolocationCoordinates, timestamp: 1659510336804}  
coords: GeolocationCoordinates {latitude: xxxx, longitude: xxxxx, altitude: null, accuracy: 1, altitudeAccuracy: null, …}  
timestamp: 1659510336804  
[](Prototype): GeolocationPosition  
*/