You can use the Geolocation API to find the users location. First, you should use the getCurrentPosition() method to request permission to location the user. If the user grants permission, you can use the coords object to access the following information:
Property | Description |
---|---|
accuracy |
The latitude and longitude measurement's accuracy in meters. |
altitude |
Meters above Earth. |
altitudeAccuracy |
The altitude's accuracy in meters. |
heading |
Movement from previous location in degrees clockwise from true north. |
latitude |
Latitude in decimal degrees. |
longitude |
Longitude in decimal degrees. |
speed |
Movement from previous location in meters/second. |
The latitude, longitude, and accuracy properties should always be available. The availability of the other property values depends on the positioning server.
<script>
function reqLoc() {
navigator.geolocation.getCurrentPosition(findMe);
}
function findMe(pos) {
var lat = pos.coords.latitude;
var long = pos.coords.longitude;
var acc = pos.coords.accuracy;
alert("Your latitude is: " + lat + " and your longitude is: " + long + " within " + acc + " meters.");
}
</script>
<a href="javascript:void()" onClick="reqLoc()">Where am I?</a>