HTML5 Development: Location Orientation Guide

Source: Internet
Author: User

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:

  1. <! doctypehtml>
  2. <html>
  3. <head>
  4. <script>
  5. /*locationfunctionshere*/
  6. </Script>
  7. </head>
  8. <body>
  9. </body>
  10. </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:

    1. <input type= "button"value= "Get location" onclick= "getuserlocation ()"/>
    2. <div id= "locationdata">
    3. Location data here
    4. </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:

    1. Functiongetuserlocation () {
    2. Checkifthegeolocationobjectissupported,ifsogetposition
    3. if (navigator.geolocation)
    4. Navigator.geolocation.getCurrentPosition (Displaylocation,displayerror);
    5. Else
    6. document.getElementById ("Locationdata"). Innerhtml= "Sorry-yourbrowserdoesn ' tsupportgeolocation!";
    7. }

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:

    1. Functiondisplaylocation (position) {
    2. /buildtextstringincludingco-ordinatedatapassedinparameter
    3. vardisplaytext= "Userlatitudeis" +position.coords.latitude+ "Andlongitudeis" +position.coords.longitude;
    4. Displaythestringfordemonstration
    5. document.getElementById ("Locationdata"). Innerhtml=displaytext;
    6. }

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:

  1. function DisplayError (Error) {
  2. Get a reference to the HTML element ForWriting result
  3. var locationelement =document.getelementbyid ("Locationdata");
  4. Find out which error we have, outputmessage accordingly
  5. Switch (error.code) {
  6. Case error. Permission_denied:
  7. Locationelement.innerhtml= "Permission was denied";
  8. Break
  9. Case error. Position_unavailable:
  10. Locationelement.innerhtml= "Location data not available";
  11. Break
  12. Case error. TIMEOUT:
  13. Locationelement.innerhtml= "Location request Timeout";
  14. Break
  15. Case error. Unknown_error:
  16. Locationelement.innerhtml= "An unspecified error occurred";
  17. Break
  18. Default
  19. Locationelement.innerhtml= "Who knows, what happened ...";
  20. Break
  21. }}

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.