GEOLOCATION DB

  • About
  • Documentation
  • Login
  • Signup




Developers


How to use

By using one of our services, either a JSON-object or JSONP callback function is returned. You can use our services with one of the following URLs. The objects have the following structure:


JSON object

https://geolocation-db.com/json

{
    "country_code":"US",
    "country_name":"United States",
    "city":"Minneapolis",
    "postal":55455,
    "latitude":44.9733,
    "longitude":-93.2323,
    "IPv4":"126.101.76.251",
    "state":"Minnesota"
}
                                    

JSONP callback

  • https://geolocation-db.com/jsonp
  • https://geolocation-db.com/jsonp/{ipv4-address}
  • https://geolocation-db.com/jsonp/{ipv6-address}

      Examples:

  • https://geolocation-db.com/jsonp
  • https://geolocation-db.com/jsonp/82.196.6.157
  • https://geolocation-db.com/jsonp/0:0:0:0:0:FFFF:52C4:069D


callback({
    "country_code":"US",
    "country_name":"United States",
    "city":"Minneapolis",
    "postal":55455,
    "latitude":44.9733,
    "longitude":-93.2323,
    "IPv4":"126.101.76.251",
    "state":"Minnesota"
})
				


A jQuery example with html markup


<!DOCTYPE html>
<html>
<head>
    <title>Geolocation DB - jQuery example</title>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
</head>
<body>
    <div>Country: <span id="country"></span>
    <div>State: <span id="state"></span>
    <div>City: <span id="city"></span>
    <div>Latitude: <span id="latitude"></span>
    <div>Longitude: <span id="longitude"></span>
    <div>IP: <span id="IPv4"></span>
    <script>
	$.ajax({
		url: "https://geolocation-db.com/jsonp",
		jsonpCallback: "callback",
		dataType: "jsonp",
		success: function( location ) {
			$('#country').html(location.country_name);
			$('#state').html(location.state);
			$('#city').html(location.city);
			$('#latitude').html(location.latitude);
			$('#longitude').html(location.longitude);
			$('#ip').html(location.IPv4);  
		}
	});		
    </script>
</body>
</html>

				


A plain JSONP javascript example, no jQuery required.


<!DOCTYPE html>
<html>
<head>
    <title>Geolocation DB - javascript example</title>
</head>
<body>
    <div>Country: <span id="country"></span>
    <div>State: <span id="state"></span>
    <div>City: <span id="city"></span>
    <div>Latitude: <span id="latitude"></span>
    <div>Longitude: <span id="longitude"></span>
    <div>IP: <span id="IPv4"></span>
    <script>
    
	var country = document.getElementById('country');
	var state = document.getElementById('state');
	var city = document.getElementById('city');
	var postal = document.getElementById('postal');
	var latitude = document.getElementById('latitude');
	var longitude = document.getElementById('longitude');
	var ip = document.getElementById('ipv4');
               
	function callback(data)
	{
		country.innerHTML = data.country_name;
		state.innerHTML = data.state;
		city.innerHTML = data.city;
		postal.innerHTML = data.postal;
		latitude.innerHTML = data.latitude;
		longitude.innerHTML = data.longitude;
		ip.innerHTML = data.IPv4;
	}

	var script = document.createElement('script');
	script.type = 'text/javascript';
	script.src = 'https://geolocation-db.com/jsonp';
	var h = document.getElementsByTagName('script')[0];
	h.parentNode.insertBefore(script, h);
	           
    </script>
</body>
</html>
                


A Node.js example


//run `npm install request` in the root folder of your website to install request
var request = require("request");
var url = "https://geolocation-db.com/json";

request({
    url: url,
    json: true
}, function (error, response, body) {

    if (!error && response.statusCode === 200) {
        console.log(body); 
    }
});


PHP Example


<?php

    $json = file_get_contents('https://geolocation-db.com/json');
    $data = json_decode($json);

    print $data->country_code . '<br>';
    print $data->country_name . '<br>';
    print $data->state . '<br>';
    print $data->city . '<br>';
    print $data->postal . '<br>';
    print $data->latitude . '<br>';
    print $data->longitude . '<br>';
    print $data->IPv4 . '<br>';

?>
				



A C# .NET WPF example


// Add System.Web.Extensions as a reference to your project
// And add the namespace System.Web.Script.Serialization to your code
using System.Web.Script.Serialization

InitializeComponent();

using (var webClient = new System.Net.WebClient()){
	
	var data = webClient.DownloadString("https://geolocation-db.com/json");
	JavaScriptSerializer jss = new JavaScriptSerializer();
	var d = jss.Deserialize<dynamic>(data);
	
	string country_code = d["country_code"];
	string country_name = d["country_name"];
	string city = d["city"];
	string postal = d["postal"];
	string state = d["state"];
	string ipv4 = d["IPv4"];
	decimal latitude = d["latitude"];
	decimal longitude = d["longitude"];
 
	// Make sure you have the corresponding labels set up in yoy GUI
	this.LblCountryCode.Content = country_code;
	this.LblCountryName.Content = country_name;
	this.LblCity.Content = city;
	this.LblPostal.Content = postal;
	this.LblState.Content = state;
	this.LblLatitude.Content = latitude.ToString();
	this.LblLongitude.Content = longitude.ToString();
	this.LblIP.Content = ipv4;
	
}


Python Example

# Python 2

import urllib
import json

url = "https://geolocation-db.com/json"
response = urllib.urlopen(url)
data = json.loads(response.read())
print data


# Python 3

import urllib.request
import json

with urllib.request.urlopen("https://geolocation-db.com/json") as url:
data = json.loads(url.read().decode())
print(data)