Why does geolocation not work on mobile browsers?
Solution 1
Try with this code, it should work.
var successHandler = function(position) {
alert(position.coords.latitude);
alert(position.coords.longitude);
};
var errorHandler = function (errorObj) {
alert(errorObj.code + ": " + errorObj.message);
alert("something wrong take this lat " + "26.0546106 );
alert("something wrong take this lng " +-98.3939791);
};
navigator.geolocation.getCurrentPosition(
successHandler, errorHandler,
{enableHighAccuracy: true, maximumAge: 10000});
Solution 2
Navigator will only work on Android on https websites. Here is an example that will not show an error if using http, but will work fine on https (from https://www.w3schools.com/HTML/tryit.asp?filename=tryhtml5_geolocation)
<!DOCTYPE html>
<html>
<body>
<p>Click the button to get your coordinates.</p>
<button onclick="getLocation()">Try It</button>
<p id="demo"></p>
<script>
var x = document.getElementById("demo");
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}
}
function showPosition(position) {
x.innerHTML = "Latitude: " + position.coords.latitude +
"<br>Longitude: " + position.coords.longitude;
}
</script>
</body>
</html>
Solution 3
Hi @jordan I was facing the same what i did is
First make sure you have updated chrome browser on your device.
-
started Location service of mobile device 1st
-
called the getLocation() function onLoad
By doing this it asked me about permission and worked out well.
Jordan
Updated on August 06, 2022Comments
-
Jordan over 1 year
I am trying to get the location of the user with HTML5
geolocation
. On Desktop it works great, but on all my mobile devices (Samsung note, Samsung galaxy S4 And Iphone 6) its not working and not showing theerror
object.this is my code:
function showPosition(position) { var coor = position.coords.longitude+", "+position.coords.latitude; alert(coor); } function errorPosition(error) { alert(error); } function toggleGeolocation() { navigator.geolocation.watchPosition(showPosition,errorPosition); }
It asks to get permission for geolocation and I click allow (gps is working). What could be the problem?
I am using Google Chrome on all devices.
-
Jordan about 9 yearsthanks for the answer, but i still cant get error msg, I added alert msg like this: function initializeGeo() { if (navigator && navigator.geolocation) { navigator.geolocation.getCurrentPosition(showPosition, errorCallback); alert("OK"); } else { alert('Geolocation is not supported'); } } and I am getting this msg, its means Geolocation is supported but the row: navigator.geolocation.getCurrentPosition(showPosition, errorCallback); do nothing...
-
Jordan about 9 yearsI have beed added alert msg after the row that calls to getCurrentPosition, and I can see the alert msg but cant see the result of getCurrentPosition
-
PowerStat about 5 yearsHow about adding some explaing text to your answer - to make it simpler to udnerstand your solution?
-
Ken Too about 5 yearsum , my current situation is the geolocation can call at pc broswer , but it not working on phone broswer. The location permission request will pop out but it wont have any response after that ...
-
sçuçu almost 5 yearsThen what? Is it solved or not?
Geolocation
is available in secure contexts. So if you are not serving the app withouthttps
it will not work unless the origin islocalhost
in almost all supporting browsers. -
Ranjit Kumar almost 5 yearsThanks for your reply. Is it possible to make it work on localhost in android chrome?
-
CSchwarz almost 5 years@RanjitKumar You could make a self signed certificate. Let's Encrypt has some instructions on how to do it yourself: letsencrypt.org/docs/certificates-for-localhost
-
saadeez almost 4 yearsThis is actually the simple and correct answer, i tried using window.onload = getLocation; and the getLocation function i invoke without parentheses but with parentheses it work for me on mobile device as well. thanks.