Search code examples
javascripthtmlgeolocation

Why does geolocation not work on mobile browsers?


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 the error 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.


Solution

  • 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});