Geolocation getCurrentPosition not working in mobile

alhakimyyes
 
on Dec 30, 2020 06:50 AM
4125 Views

hi all

I have this code to Show (Display) Current GPS coordinates in TextBox on Button Click in JavaScript

It works from my laptop but it does not work from the phone

<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=API_Key"></script>
<script type="text/javascript">
    function ViewCoOrdinate() {
        if (navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(function (p) {
                var lat = p.coords.latitude;
                var lng = p.coords.longitude;
                document.getElementById("txtLat").value = lat;
                document.getElementById("txtLon").value = lng;
            });
        } else {
            alert('Geo Location feature is not supported in this browser.');
        }
 
        return false;
    }
</script>
<input type="text" id="txtLat" />
<input type="text" id="txtLon" />
<input type="button" id="btnView" value="View" onclintclick="return ViewCoOrdinate()" />
Download FREE API for Word, Excel and PDF in ASP.Net: Download
Results 1 - 5 of 7
alhakimyyes
 
on Dec 30, 2020 09:11 AM

But how do I make it give the value to the textbox when I press the button

dharmendr
 
on Dec 31, 2020 03:43 AM

Hi alhakimyyes,

Refer below code. Make sure location services turned on your device.

Also make sure location service turned on for the browser you are checking.

JavaScript

navigator.geolocation.getCurrentPosition(
    function (position) {
        document.getElementById("txtLat").value = position.coords.latitude;
        document.getElementById("txtLon").value = position.coords.longitude;
    },
    function (error) {
        alert(error.code + ": " + error.message);
    },
    {
        enableHighAccuracy: true,
        maximumAge: 10000,
        timeout: 5000
    }
);

 

alhakimyyes
 
on Jan 02, 2021 12:20 AM

Do you mean to add this code next to the previous code? Like this

<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=API_Key"></script>
<script type="text/javascript">
    function ViewCoOrdinate() {
        if (navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(function (p) {
                var lat = p.coords.latitude;
                var lng = p.coords.longitude;
                document.getElementById("txtLat").value = lat;
                document.getElementById("txtLon").value = lng;
            });
        } else {
            alert('Geo Location feature is not supported in this browser.');
        }
  
        return false;
    }
</script>

navigator.geolocation.getCurrentPosition(
    function (position) {
        document.getElementById("txtLat").value = position.coords.latitude;
        document.getElementById("txtLon").value = position.coords.longitude;
    },
    function (error) {
        alert(error.code + ": " + error.message);
    },
    {
        enableHighAccuracy: true,
        maximumAge: 10000,
        timeout: 5000
    }
);

<input type="text" id="txtLat" />
<input type="text" id="txtLon" />
<input type="button" id="btnView" value="View" onclintclick="return ViewCoOrdinate()" />

 

dharmendr
 
on Jan 02, 2021 03:10 AM

Hi alhakimyyes,

Use this.

HTML

<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=API_Key"></script>
<script type="text/javascript">
    function ViewCoOrdinate() {
        navigator.geolocation.getCurrentPosition(
            function (position) {
                document.getElementById("txtLat").value = position.coords.latitude;
                document.getElementById("txtLon").value = position.coords.longitude;
            }, function (error) {
                alert(error.code + ": " + error.message);
            }, {
                enableHighAccuracy: true,
                maximumAge: 10000,
                timeout: 5000
            }
        );

        return false;
    }
</script>
<input type="text" id="txtLat" />
<input type="text" id="txtLon" />
<input type="button" id="btnView" value="View" onclintclick="return ViewCoOrdinate()" />

 

Results 1 - 5 of 7