input license here

Lesson 43: Geolocation in HTML5

Geolocation is used to locate users in HTML5.


Click the button below to show your location on the map:


You are viewing: What is Geolocator?


Locate user location


Geolocation is used to locate users in HTML5.


To protect privacy, location determination is only done with the user's consent.


Supported browsers


Below is a list of some early browser versions that fully support Geolocation.


Geolocation API 5.0 – 49.0 (http) 50.0 (https) 9.0 3.5 5.0 16.0


Using Geolocation to locate a location in HTML


To return the user's location, you can use the function: getCurrentPosition().


See more: Save money with free English learning websites for students


Here is a simple example illustrating the function getCurrentPosition(), return user coordinates


For example:


Click the button to get your coordinates


Example explanation:



  • Give it a check Geolocation is browser supported?

  • If browser supports it, run method getCurrentPosition(). If not, display a message to the user.

  • If the function getCurrentPosition() run successfully, it returns to the user the location coordinates through the parameter passing function (showPosition)

  • The showPosition() function prints the user's latitude and longitude.


The above is a basic example of Geolocation, it has not yet implemented the error detection.


Error identification and display


The second parameter in the getCurrentPosition() method is used to determine the error generated during the positioning process. It will automatically call the default function when it receives the error message:


For example:


Click the button to get your coordinates.


Show user location on map


To display your location on a map, you can use online mapping services such as Google Maps.


In the example below, the latitude and longitude values ​​will be displayed on a Google Map (using a still image):


For example:


Click button to get position.


Example: Google Map Script


Click button to get position.


Related Posts
Diệp Quân
Nguyen Manh Cuong is the author and founder of the vmwareplayerfree blog. With over 14 years of experience in Online Marketing, he now runs a number of successful websites, and occasionally shares his experience & knowledge on this blog.
SHARE

Related Posts

Subscribe to get free updates

Post a Comment

Sticky