Search code examples
javascriptsslhttpsgeolocation

Free JavaScript Plugin to get user location


Is there any free API/Web Service where I can get geolocation of those who visit my HTTP secure website?

before I use https://ssl.geoplugin.net/ to check this, but now they require a premium key to request location. I also tried to use the Google Maps GeoLocation API but it gives a wrong location. So I end up asking for any free javascript plugin to use. Thanks


Solution

  • This is actually a feature of HTML5, so to do this:

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

    Sourced from http://www.w3schools.com/html/html5_geolocation.asp