Geolocation getCurrentPosition() Method
Example
Get the latitude and longitude of the user's position:
var x = document.getElementById("demo");
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}
}
function showPosition(position) {
x.innerHTML = "Latitude: " + position.coords.latitude +
"<br>Longitude: " + position.coords.longitude;
}
Try it Yourself »
Description
The getCurrentPosition() method returns the current position of the device.
Browser Support
Method |
|
|
|
|
|
getCurrentPosition() |
5.0 |
9.0 |
3.5 |
5.0 |
16.0 |
Syntax
navigator.geolocation.getCurrentPosition(success, error, options)
Parameter Values
Parameter |
Type |
Description |
success |
|
Required. A callback function that takes a Position object as its sole input
parameter |
error |
|
Optional. A callback function that takes a PositionError object as its sole
input parameter |
options |
|
Optional. A PositionOptions object |
Return Value
The current position of the device |
W3schools Pathfinder
Track your progress - it's free!