geo-positioning is one of the most exciting features offered by HTML5.
With relatively simple JavaScript code, you can create Web applications that determine the user's geographic details, including latitude and longitude, and elevation. Some Web applications can even provide navigation by monitoring the movement of the user's location over time, which also combines a map system such as the GoogleMaps API.
As with all HTML5 functions, you can't rely on browsers to provide support. And where the browser provides support, it changes in depth and durability. Essentially, you need to provide an alternative for those users whose browsers cannot provide full support for HTML5.
In this tutorial, we'll look at some basics of creating user location information. In practice, the browser may fetch data from multiple places. For example, it could be a mobile device's GPS data or IP address data on any networked device. However, your code does not need to consider these details, you simply retrieve and use location data.
Html5web page
Use the following code to create a frame for the HTML5 page:
- <! doctypehtml>
- <html>
- <head>
- <script>
- /*locationfunctionshere*/
- </Script>
- </head>
- <body>
- </body>
- </html>
We place the JavaScript geolocation function in the location of the page header script, as well as some basic HTML features for testing and demonstrating the feature of the theme area.
HTML presentation Features
Add the following markup to the HTML body section:
- <input type= "button"value= "Get location" onclick= "getuserlocation ()"/>
- <div id= "locationdata">
- Location data here
- </div>
Geolocation functions can be called at any time, such as when a page is loaded or when the jQuery document preparation function is combined. For a better demonstration, we use a button to invoke the function and write the location data to the Div features, and the Div features initially have some simple placeholder text.
Get Position function
The GetPosition method is the primary method we use to determine the user's location. Add the following functions in the script area of the page header:
- Functiongetuserlocation () {
- Checkifthegeolocationobjectissupported,ifsogetposition
- if (navigator.geolocation)
- Navigator.geolocation.getCurrentPosition (Displaylocation,displayerror);
- Else
- document.getElementById ("Locationdata"). Innerhtml= "Sorry-yourbrowserdoesn ' tsupportgeolocation!";
- }
The function is called when the user taps the button. The code first checks to see if the Navigator's Geolocation object is displayed and the browser supports it if it is displayed. If the Geolocation object is supported, then the code uses it to invoke the GetCurrentPosition method.
The Getcurrerentposition method takes two parameters to indicate a callback function. , a function that is called when geolocation information is fetched, and one that is called when an error is returned.
If the Geolocation object is not supported, then the function simply writes the error message to the page Div feature.
When a site first attempts to retrieve user location data, the user's browser commands them to determine whether they agree to share the data. This function is only executed when the user consents.
Display position function
Next, the function we need to deploy is actually the first parameter in the GetCurrentPosition method call. When the browser receives the function, the user location data is passed to the function. Add the following code after the Getuserlocation function:
- Functiondisplaylocation (position) {
- /buildtextstringincludingco-ordinatedatapassedinparameter
- vardisplaytext= "Userlatitudeis" +position.coords.latitude+ "Andlongitudeis" +position.coords.longitude;
- Displaythestringfordemonstration
- document.getElementById ("Locationdata"). Innerhtml=displaytext;
- }
This code first creates a variable that creates a string in the variable that contains the latitude and longitude data for the positional parameter. The function then writes this information and some informational text to the page. In your own website, you can also use this data for other purposes, rather than just writing pages--for demonstration purposes only.
Error function
Errors may occur when using geo-location devices. For example, users may not agree to share their geolocation data, the browser may not be able to retrieve data, and so on. So we need to add a function to handle the error, using the second function of the GetCurrentPosition method. Add the following functions after the Displaylocation function:
- function DisplayError (Error) {
- Get a reference to the HTML element ForWriting result
- var locationelement =document.getelementbyid ("Locationdata");
- Find out which error we have, outputmessage accordingly
- Switch (error.code) {
- Case error. Permission_denied:
- Locationelement.innerhtml= "Permission was denied";
- Break
- Case error. Position_unavailable:
- Locationelement.innerhtml= "Location data not available";
- Break
- Case error. TIMEOUT:
- Locationelement.innerhtml= "Location request Timeout";
- Break
- Case error. Unknown_error:
- Locationelement.innerhtml= "An unspecified error occurred";
- Break
- Default
- Locationelement.innerhtml= "Who knows, what happened ...";
- Break
- }}
This function retrieves the error message that occurs after the location request is issued. The function first obtains a reference to the page Div feature to write the appropriate error message. This information is adjusted with a swap statement so that it adapts to the special properties of the error.
Consider and choose
The above functions cover the basic points of user location data retrieval. You can use the retrieved location data to display the user's location on the map using the Google Maps API, primarily by creating a data variable in a custom URL and then loading it into the HTML image features of your page.
You can also use the Watchposition method to track the user's location and even the speed at which they move, even though the data is not accurate because it is not from a mobile device's GPS, such as data that might come from a wireless network.
These functions will bring a brighter future for web developers. However, their success not only requires the browser to provide support, but also users like, hardware support.
HTML5 Development: Location Orientation Guide