Andrus Andrus - 1 month ago 6
Javascript Question

How to read current location in html5 application

ASP.NET MVC4 mobile HTML5 online application is running in Android and Apple mobiles and tablets in Chrome, Android and Safari browsers.

Application contains html button

<form action='SavePosition'>
<input type='submit' value='Send' name='position'>
</form>


which should send current location to server. Server should display locations in map.

How to implement this in HTML5/Javascript ? Based on my knowledge html5 does not have such possibility.
I can force users to use Google Chrome browser in all devices if this helps.

Answer

You can request the position via javascript.

if (navigator.geolocation)
    navigator.geolocation.getCurrentPosition(function(position) {
        console.log("latitude: " + position.coords.latitude);
        console.log("altitude: " + position.coords.altitude);
    })

To send the coords via the form you could use some hidden input fields.

You can only request the position in chrome >= 50.0 only via https.

see W3Schools HTML5 Geolocation