Click here to Skip to main content
15,893,904 members
Articles / Web Development / HTML5

HTML5 Quick Start Web Application

Rate me:
Please Sign up or sign in to vote.
4.91/5 (105 votes)
15 Feb 2013CPOL11 min read 365.6K   11.8K   239  
HTML5 self learning center - Explore and understand the new features!
function checkSupport() {
    if (navigator.geolocation) {
        document.getElementById("supportstatus").innerHTML = "HTML5 Geolocation is supported in your browser.";
    }
    else {
        document.getElementById("supportstatus").innerHTML = "HTML5 Geolocation is <strong>not</strong> supported in your browser.";
    }
}

function getLocation() {
    if (navigator.geolocation) {
        // The watchPosition() function has the same structure as getCurrentPosition()
        navigator.geolocation.getCurrentPosition(updateLocation, handleLocationError, {
            timeout: 1000000,
            maximumAge: 1000,
            enableHighAccuracy: false
        });
    }
    else {
        document.getElementById("supportstatus").innerHTML = "HTML5 Geolocation is <strong>not</strong> supported in your browser.";
    }
}

function updateLocation(position) {
    var latitude = position.coords.latitude;
    var longitude = position.coords.longitude;
    var timeTaken = new Date(position.timestamp);
    
    if (!latitude || !longitude) {
        document.getElementById("supportstatus").innerHTML = "HTML5 Geolocation is supported in your browser, but location is currently not available.";
        return;
    }

    document.getElementById("latitude").innerHTML = latitude;
    document.getElementById("longitude").innerHTML = longitude;
    document.getElementById("timestamp").innerHTML = timeTaken;
}

function handleLocationError(error) {
    switch (error.code) {
        case 0: // UNKNOWN_ERROR
            updateStatus("There was an error while retrieving your location: " + error.message);
            break;
        case 1: // PERMISSION_DENIED
            updateStatus("The user prevented this page from retrieving a location.");
            break;
        case 2: // POSITION_UNAVAILABLE
            updateStatus("The browser was unable to determine your location: " + error.message);
            break;
        case 3: // TIMEOUT
            updateStatus("The browser timed out before retrieving the location.");
            break;
    }
    displayError(error);
}

function updateStatus(message) {
    document.getElementById("errorstatus").innerHTML = message;
}

function displayError(error) {
    var errors = {
        0: 'Unknown error',
        1: 'Permission denied',
        2: 'Position unavailable',
        3: 'Request timeout'
    };
    alert("Error: " + errors[error.code]);
}

//////////////////////

function showMap() {
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(showMapLocation, handleLocationError, {
            timeout: 1000000,
            maximumAge: 1000,
            enableHighAccuracy: true
        });
    }
    else {
        document.getElementById("supportstatus").innerHTML = "HTML5 Geolocation is <strong>not</strong> supported in your browser.";
    }
}

function showMapLocation(position) {
    var latlng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
    var myOptions = {
        zoom: 16,
        center: latlng,
        mapTypeControl: false,
        navigationControlOptions: { style: google.maps.NavigationControlStyle.SMALL },
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    var map = new google.maps.Map(document.getElementById("mapView"), myOptions);
    
    var marker = new google.maps.Marker({
            position: latlng,
            map: map,
            title: "You are here! (Accuracy: " + position.coords.accuracy + " meter radius)"
        });
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect Intuit India
India India


A software professional for more than 17+ years building solutions for Web and Desktop applications.

Currently working at Intuit India.

Website: Learn By Insight
Github: Sandeep Mewara
LinkedIn: Sandeep Mewara

Strongly believe in learning and sharing knowledge.



Comments and Discussions