The Geolocation API (JavaScript based) is used for locating the user's position. It lets you share your location with trusted websites. Today most browsers (for Desktop as well as mobile devices) support the Geolocation API.
How the Geolocation API determines your location
There are many techniques used to determine/identify the location of the user but they vary in the degree of accuracy. Desktop browsers can use the IP Address (this method is reliable to the city level) or WiFi to detect your location whereas Mobile devices can use the Global Positioning System (GPS), WiFi and Cell Phone Triangulation. GPS provides the most accurate location. Cell Phone Triangulation determines the location much faster compared to GPS but the location's accuracy is less than for GPS.
Browser Support for Geolocation
We can also check Browser Compatibility using the Geolocation property of the Navigator object.
if(navigator.geolocation)
{
alert("Your Browser Support Geolocation API");
}
else
{
alert("Your Browser does not Support Geolocation API");
}
Before beginning to look, the following are a couple of geographical definitions:...Latitude: Latitude specifies a north and south point on Earth. It is measured from the equator...Longitude: Logitude specifies an east and west point. It is measured from Greenwich, England.
Example 1
In this example we get our Location Coordinates (in other words latitude and longitude). The things we have done here are:
- Create a paragraph tag in the body section and provide id="display".
- Create a button (to find the location).
- Then create a script. In the script, we create a function, getmylocation() that will be called when a user clicks on the button (onclick="getmylocation()").
- If the Geolocation API is supported by the browser then it calls the getCurrentPosition(showPosition) method. Actually this method can carry three parameters (two are optional) but we are using a single parameter, in other words the showPosition function.We will learn about another two callback functions or parameters later in this article.
5. The getCurrentPosition's success callback (in other words showPosition) is passed with a position that contains the latitude and longitude of your location.We get the latitude and longitude of the location from the position.coords object.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Geolocation Example 1</title>
</head>
<body>
<p id="display">Click the button to get your coordinates:</p>
<button onclick="getmylocation()">Find my Location</button>
<script>
var x=document.getElementById("display");
function getmylocation()
{
if (navigator.geolocation)
{
navigator.geolocation.getCurrentPosition(showPosition);
}
else
{
x.innerHTML="Geolocation API is not supported by this browser.";
}
}
function showPosition(position)
{
x.innerHTML="Latitude: " + position.coords.latitude + "<br>Longitude: " + position.coords.longitude;
}
</script>
</body>
</html>
Preview:
On clicking the Find my location Button the user will be prompted with a dialog requesting the user's permission to share the location information.
When the user permits the sharing of the location information the user gets the latitude and longitude successfully.
If the user's browser does not support the Geolocarion API then he will get output like this:
I have created a diagram for understanding the entire process easily.
Example 2
In the above example, we learned how to get the location coordinates. Suppose that the user denied sharing of the location information or the browser is unable to get the location or the request times out. In this case, we need to handle the error and display the message to the user.
We need to add an error callback function, in other words showerror in the getCurrentPosition() method. Geolocation passes an error object when getting the numeric code (in other words error.code). We can also display an error message using the error.message property.
Here, we have created a var errortype in which error messages are supplied along with the error codes (0 to 3). Using error.code we display our error message (in other words errmsg).
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Geolocation Example 2</title>
</head>
<body>
<p id="display">Click the button to get your coordinates:</p>
<button onclick="getmylocation()">Find my Location</button>
<script>
var x=document.getElementById("display");
function getmylocation()
{
if (navigator.geolocation)
{
navigator.geolocation.getCurrentPosition(showPosition,showerror);
}
else
{
x.innerHTML="Geolocation API is not supported by this browser.";
}
}
function showPosition(position)
{
x.innerHTML="Latitude: " + position.coords.latitude + "<br>Longitude: " + position.coords.longitude;
}
function showerror(error){
var errortype={
0:"Unknown Error",
1:"Permission denied by user",/*used when user denied the request*/
2:"Position not available",/*used when browser tried, but failed to get location*/
3:"Request time out"
};
var errmsg=errortype[error.code];
var divbox=document.getElementById("display");
divbox.innerHTML=errmsg;
}
</script>
</body>
</html>
In the getCurrentPosition() method, the last function(optn) sets the timelimit (in other words timeout), Accuracy and so on.
if(navigator.geolocation){
var optn={
timeout:infinity,
enableHighAccuracy:true,
};
navigator.geolocation.getCurrentPosition(showPosition,showerror,optn);
}
Here, timeout is in milliseconds and its default value is Infinity and enableHighAccuracy gets an accurate position. If false is used for enableHighAccuracy then a less accurate position is obtained (default value is false).
That's all. I hope you like it.
Thanks
No comments :
Post a Comment