blob: a2d19748daaef2639ce34d956ebf8a970b59d611 (
plain)
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
|
---
title: Geolocation.getCurrentPosition()
slug: Web/API/Geolocation/getCurrentPosition
tags:
- API
- Geolocation
- Méthode
- Reference
translation_of: Web/API/Geolocation/getCurrentPosition
---
{{securecontext_header}}{{APIRef("Geolocation API")}}
La méthode **`Geolocation.getCurrentPosition()`** fournit la position actuelle de l'appareil.
## Syntaxe
navigator.geolocation.getCurrentPosition(success[, error[, [options]])
### Paramètres
- `success`
- : Une fonction de rappel qui prend un objet {{domxref("Position")}} comme argument.
- `error` {{optional_inline}}
- : Une fonction de rappel qui prend un objet {{domxref("PositionError")}} comme argument.
- `options` {{optional_inline}}
- : Un objet {{domxref("PositionOptions")}} optionnel. Les options décrites par cet objet sont :
- `maximumAge` : un entier qui exprime une durée en millisecondes ou l'infini pour indiquer la durée maximale pendant laquelle mettre en cache la position.
- `timeout` : un entier qui exprime la durée, en millisecondes, avant que la fonction de rappel `error` soit appelé. Si cette propriété vaut `0`, la fonction d'erreur ne sera jamais appelée.
- `enableHighAccuracy` : un booléen qui indique si une précision élevée est requise.
## Exemples
```js
var options = {
enableHighAccuracy: true,
timeout: 5000,
maximumAge: 0
};
function success(pos) {
var crd = pos.coords;
console.log('Votre position actuelle est :');
console.log(`Latitude : ${crd.latitude}`);
console.log(`Longitude : ${crd.longitude}`);
console.log(`La précision est de ${crd.accuracy} mètres.`);
}
function error(err) {
console.warn(`ERREUR (${err.code}): ${err.message}`);
}
navigator.geolocation.getCurrentPosition(success, error, options);
```
## Spécifications
| Spécification | État | Commentaires |
| ------------------------------------ | -------------------------------- | ----------------------- |
| {{SpecName('Geolocation')}} | {{Spec2('Geolocation')}} | Spécification initiale. |
## Compatibilité des navigateurs
{{Compat("api.Geolocation.getCurrentPosition")}}
## Voir aussi
- [Utiliser la géolocalisation](/en-US/docs/WebAPI/Using_geolocation)
- {{domxref("Navigator.geolocation")}}
|