Charles Haro
Charles Haro

Reputation: 1886

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?

Upvotes: 3

Views: 2438

Answers (1)

Tom
Tom

Reputation: 4592

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>

Upvotes: 2

Related Questions