Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use wifi to find location instead of gps, javascript

I am using navigator.geolcation like this:

navigator.geolocation.getCurrentPosition(
            displayBestPosition, 
            displayError,
            { enableHighAccuracy: true, timeout: timeoutVal, maximumAge: 0 }
        );

I'm accessing it on my android device browser so its using my gps. However, when I am inside and I keep getting timeouts I'd like to switch from gps to getting the location from wifi. Is there any way to do this?

like image 552
Charles Haro Avatar asked Nov 18 '25 21:11

Charles Haro


1 Answers

DEMO FIDDLE

<head>
<script>
var myip;
function callback(data) {
    alert(data.city);
}
function request_location() {
    var oHead = document.getElementsByTagName('head')[0];
    var oScript = document.createElement("script");
    oScript.type = "text/javascript";
    oScript.src = "http://ip-api.com/json/" + myip + "?callback=callback";
    oHead.appendChild(oScript);
}
</script>
<script type="text/javascript" src="http://l2.io/ip.js?var=myip" onload="request_location();"></script>
</head>
like image 196
Tom Avatar answered Nov 21 '25 09:11

Tom