Question

how can i do this or like this ?

Is there any free web service to detect location of users ?

I want to use this in a webpage

Avoid maintaining a huge database of IP's and their locations!

My Priorities are : The service should be : 1.Free 2.Most Accurate

Was it helpful?

Solution

From http://blog.programmableweb.com/2009/03/31/3-free-ways-to-geolocate-by-ip/ I got

Hostip.info is a community-powered database of IP mapping. Its REST API is easy to incorporate into server-side code, with several options for the type of output. Check out our hostip.info API profile, where you can see the mashups already using this API.

MaxMind-Geo Lite is an API of a different sort. Rather than call to a web service, its free version is distributed as a binary. There are open source libraries for common programming languages to access the IP data.

Also see http://www.eggheadcafe.com/articles/20051109.asp

Similar question : https://stackoverflow.com/questions/283016/know-a-good-ip-address-geolocation-service

OTHER TIPS

I use GeoIP Country Lite. Every month they publish updates to the database and they claim 99.5% accuracy. It is trivial to use as they also provide library code to query the database.

MaxMind also provide city based lookups if you need higher resolution than by country.

The HTML5 Geolocation API works in never browsers (Safari, Chrome, Firefox), though it requires some kind of location service on the computer. I would use this first and then fallback on location by IP if HTML5 Geolocation is't available. Sample code:

if (navigator.geolocation) {
  navigator.geolocation.getCurrentPosition(function (pos) {
    console.log([pos.latitude, pos.longitude]);
  }, function (err) { 
    // Do something else, IP location based?
  });
} else {
  // Do something else, IP location based?
}

To find the Country, City and even address of the user you can use Google Maps Geocoding API

This works great as far I tested it.

Get an api key from ipinfodb.org

var Geolocation = new geolocate(false, true);
Geolocation.checkcookie(function() {
    alert('Visitor latitude code : ' + Geolocation.getField('Latitude'));
    alert('Visitor Longitude code : ' + Geolocation.getField('Longitude'));
});

function geolocate(timezone, cityPrecision) {
    alert("Using IPInfoDB");
    var key = 'your api code';
    var api = (cityPrecision) ? "ip_query.php" : "ip_query_country.php";
    var domain = 'api.ipinfodb.com';
    var version = 'v2';
    var url = "http://" + domain + "/" + version + "/" + api + "?key=" + key + "&output=json" + ((timezone) ? "&timezone=true" : "&timezone=false" ) + "&callback=?";
    var geodata;
    var JSON = JSON || {};

    // implement JSON.stringify serialization
    JSON.stringify = JSON.stringify || function (obj) {
        var t = typeof (obj);
        if (t != "object" || obj === null) {
            // simple data type
            if (t == "string") obj = '"'+obj+'"';
            return String(obj);
        } 
        else {
            // recurse array or object
            var n, v, json = [], arr = (obj && obj.constructor == Array);
            for (n in obj) {
                v = obj[n]; t = typeof(v);
                if (t == "string") v = '"'+v+'"';
                else if (t == "object" && v !== null) v = JSON.stringify(v);
                json.push((arr ? "" : '"' + n + '":') + String(v));
            }
            return (arr ? "[" : "{") + String(json) + (arr ? "]" : "}");
        }
    };

    // implement JSON.parse de-serialization
    JSON.parse = JSON.parse || function (str) {
        if (str === "") str = '""';
        eval("var p=" + str + ";");
        return p;
    };

    // Check if cookie already exist. If not, query IPInfoDB
    this.checkcookie = function(callback) {
        geolocationCookie = getCookie('geolocation');
        if (!geolocationCookie) {
            getGeolocation(callback);
        } 
        else {
            geodata = JSON.parse(geolocationCookie);
            callback();
        }
    }

    // Return a geolocation field
    this.getField = function(field) {
        try {
            return geodata[field];
        } catch(err) {}
    }

    // Request to IPInfoDB
    function getGeolocation(callback) {
        try {
            $.getJSON(url,
                    function(data){
                if (data['Status'] == 'OK') {
                    geodata = data;
                    JSONString = JSON.stringify(geodata);
                    setCookie('geolocation', JSONString, 365);
                    callback();
                }
            });
        } catch(err) {}
    }

    // Set the cookie
    function setCookie(c_name, value, expire) {
        var exdate=new Date();
        exdate.setDate(exdate.getDate()+expire);
        document.cookie = c_name+ "=" +escape(value) + ((expire==null) ? "" : ";expires="+exdate.toGMTString());
    }

    // Get the cookie content
    function getCookie(c_name) {
        if (document.cookie.length > 0 ) {
            c_start=document.cookie.indexOf(c_name + "=");
            if (c_start != -1){
                c_start=c_start + c_name.length+1;
                c_end=document.cookie.indexOf(";",c_start);
                if (c_end == -1) {
                    c_end=document.cookie.length;
                }
                return unescape(document.cookie.substring(c_start,c_end));
            }
        }
        return '';
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top