Time for action – getting geolocation data

In this section we will add some code to our weather widget example to access the Geolocation API. You can find the code for this section in chapter8/example8.3.

First let's go into weather.html and add a section to show the user's location next to the Check Weather button:

<div id="controls">
    <div>
        Latitude: <input id="latitude" type="text"/><br/>
        Longitude: <input id="longitude" type="text"/>
    </div>
    <button id="getWeather">Check Weather</button>
    <div class="error">
        Error: <span></span>
    </div>
</div>

We add a <div> element with text fields to show the user's latitude and longitude that we got from the Geolocation API. We also add a <div class="error"> element to show the error message if geolocation fails. ...

Get HTML5 Web Application Development By Example Beginner's guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.