Here we will learn how to work with the Geolocation API.
Geolocation APIIntroduction: The Geolocation API allows a website to retrieve the current geographical location of a user. This API allows you to create an application that helps the user to reach a target location from their current location. For example, we can have a map application that gives directions to a target location. The Geolocation API retrieves location from a Device IP Address, the nearest mobile phone towers and GPS. Geolocation is the most beneficial with applications for mobile devices. For example, the Geolocation API keeps updating their location when they are in travel.The following method determines the user's location:
The getCurrentPosition() method calls a callback function that takes the position object as an argument and this object specifies the current geographical location as coordinates.The Position object returns the two properties coords and timestamp. The coords property returns the various attributes as given below:
Code Example
Note: Due to your network security firewall you may not get latitude and longitude.watchPosition(): This method returns the current location of a user and continuously updates the location while the user is moving. If you want to stop tracking the user's current location then you can call the clearWatch() method.Code Example
Error Handling: There might be a situation when you get error messages due to an applied restriction on location. An error may occur when the device goes out of the coverage area or the network connection times out. We need to consider all the possible causes of the error that can appear in the application. We can use the getCurrentPosition() method that has a second parameter as the error handler function. This function can handle the following errors:
DateTime in C#