Get the Current Location from Windows 8.1 Store App using JavaScript

Published on October 11, 2014 by abundantcode

Filed under Windows

Last modified December 24, 2014

Print this page

rate 1 star rate 2 star rate 3 star rate 4 star rate 5 star
Your rating: none, Average: 0 (0 votes)

This article have been viewed 3706 times

Below is a sample code snippet demonstrating the procedure on how to get the current user location Windows 8.1 Store App using JavaScript.

How to Get the Current Location from Window 8.1 Store App using JavaScript ?

Add the button and the span element to the html page where you want to show the current location.

<button id="GetLocation"> Get Location </button>
<span id="locationinformation"></span>

Add the methods to get the current location in the javascript file . To retreive the location , we should use the Geolocator class that is defined in the Windows.Devices.Geolocation namespace and then use the getGeopositionAsync method .>
Bind an event handler once the location is received so that the data can be retreived from the position and displayed in the span id that is defined in the screen .

function GetLocationFunction() 
{
   var  location = new Windows.Devices.Geolocation.Geolocator();
   location.getGeopositionAsync().then(UpdateLocation, null);
}
function UpdateLocation(position)
{
   document.getElementById('locationinformation').innerHTML = position.coordinate.point.position.latitude + "," + position.coordinate.point.position.longitude;
}

image

If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.

Leave a Comment


x

You might be Interested in these Posts

ES6: The future is now at Warrington on October 18 , 2016

JavaScript North West User Group is hosting its user group meet on the topic "ES6: The future i...

How to append an array to another in JavaScript ?

In JavaScript , you may want to append one array to another array(s) to combine their elements resul...

How to pass a function as parameter in JavaScript ?

There are times when you might want to pass a function as argument to another function in JavaScript...