Implementing Geolocation

Introduction Geolocation and Offline Support for Data

You are visiting a city for the first time and looking for sight-seeing locations close to you hotel. However, you do not know where to go and which places to look for. Therefore, it will be helpful if you have some applications that can help you locate  such places and provide feedback for the same. This can be done by implementing Geolocation API.

In addition, you may need that users are able to access a Web page even when they are not connected to the Internet.

Implementing Geolocation

ShopHere is large retail store based in Ohio. The store offers clothes, accessories, and footwear. In addition, it sells the home furnishing goods and electronic items, such as refrigerator, air conditioner, laptops, and mobile devices. As part of its business strategy to attract new customers and build a stable customer base, the store offers discounts and special deals on products. In addition, the official website of the store has a feature that enables users to share their current location, based on which, they are guided to reach the nearest store.

Such a feature is incorporated in websites by using the Geolocation API. The Geolocation API- enabled website can locate usrs current geographical location, display points of interest around that location or guide the users from their current location to the target destination.

Implementing the Geolocation API


The Geolocation API allows a website to retrieve the current geographical location of a user. The API enables you to create applications that guide users how to reach a target location from their current location. An example of such an application is a map application that gives directions to a target locations.

The users location is not retrieved just through code or browser. Instead, the Geolocation API uses certain features , such as Global Positioning System (GPS), Internet Protocol(IP) address of a device, nearest mobile phone towers , and input from the user, in the users devices to retrieve the users locations.

The users location retrieved by using the Geolocation API is almost accurate depending upon the type of source used to retrieve the location. For Example, the IP address  gives a location that can be close to the users location, while, the GPS is able to give more accurate results.

Identifying the users location may, at times, compromise the users privacy. Hence the location of users is not available unless they approve it. The prompt appears that asks the users if they would like to share their current location and also specifies the reason for collecting this data. In addition, it should specify where the data will be shared.

Geolocation is more beneficial with applications used for mobile devices. This is because while the users are travelling , the Geolocation API keeps updating  their locations. In case of desktop applications, the users location remains constant and can be set only once.

The Geolocation  API  provides the following methods to determine the users locations:


  • getCurrentPosition()
  • watchPosition()
getCurrentPosition()

The getCurrentPosition() method  is used to retrieve the current geographical location of the user. The location is retrieved as a set of coordinates.

The  syntax for the getCurrentPosition() method is:

getCurrentPosition(CallbackFunction, ErrorHandler, Options);

Before retrieving the users location by using the getCurrentPosition() method , you need to check whether the browser support the geographical feature. For this you can use the geolocation property  of the navigator object. The navigator object contains the information about the browser. 
Consider the following code snippet to check whether the browser supports the geolocation features:

if(navigator.geolocation)
var geo=navigator.geolocation
else
{
alert("Your browser doesnt support geolocation")
}
The preceding code snippet checks whether the geolocation feature is supported by the browser. If it is not supported , a relevant message is displayed.

Consider the following code snippet for retrieving the users location by using the getCurrentPosition () method:

<!DOCTYPE HTML>
<HTML>
<BODY>
<P ID="button">Check here to know your location coordinate</P>
<BUTTON onclick="getLocation()">Get Location</BUTTON>
<SCRIPT>
var geo=document.getElementById("button");
function getLocation()
{
if(navigator. geolocation)
{
navigator.geolocation.getCurrentPosition(getposition);
{
else(geo.innerHTML="Geolocation is not supported by the browser";}
}
function getPosition(position)
{
geo.innerHTML="Latitude: "+position .coords.latitude+"<BR>Longitude: "+position.coords.longitude;
}
</SCRIPT>
</BODY>
</HTML>

About Codingwelove

Codingwelove
Recommended Posts × +

0 comments:

Post a Comment