“Get out form smog” – get current location from user

Get out from smog – part VII

In a previous posts. I showed you how to calculate distance between points. It’s time to get current location from user. We will take care of this in our view.

We’ll use for this method getCurrentPosition from geolocation.

function getLocation() {
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(function (position) {
            alert('Latitude ' + position.coords.latitude);
            alert('Longitude ' + position.coords.longitude);
                url: '@Url.Action("Coordinate", "Home")',
                data: { 'lat': position.coords.latitude, 'lon': position.coords.longitude },
                type: "post",
                cache: false,
                success: function () {
                error: function (thrownError) {
                    alert('Something was wrong');

This simple script will pass two parametrs(lon,lat) to my controller. Later my application return the closest and least pulluted air – place within 100km. I showed it in ealier posts.

P.S Of course, we have to give consent to make localization available. 🙂


Link to the project.

Leave a Reply

Your email address will not be published. Required fields are marked *