IPGeolocation would be a better option for you. They build their own database and update their database twice a week. They have 99% accuracy at the country level and 75% at the city level. They also offer paid as well as free (1000 requests/day) API plans that you can see here: https://ipgeolocation.io/pricing.html.
To facilitate the developers, IPGeolocation API offers some SDKs for various programming languages:
The endpoint to get geolocation information of an IP address:
This endpoint is meant to be called from the server side.
$ curl 'https://api.ipgeolocation.io/ipgeo?apiKey=API_KEY&ip=8.8.8.8'
The JSON response will be:
{
"ip": "8.8.8.8",
"hostname": "google-public-dns-a.google.com",
"continent_code": "NA",
"continent_name": "North America",
"country_code2": "US",
"country_code3": "USA",
"country_name": "United States",
"country_capital": "Washington",
"state_prov": "California",
"district": "",
"city": "Mountain View",
"zipcode": "94043",
"latitude": "37.4229",
"longitude": "-122.085",
"is_eu": false,
"calling_code": "+1",
"country_tld": ".us",
"languages": "en-US,es-US,haw,fr",
"country_flag": "https://ipgeolocation.io/static/flags/us_64.png",
"isp": "Level 3 Communications",
"connection_type": "",
"organization": "Google Inc.",
"geoname_id": "5375480",
"currency": {
"code": "USD",
"name": "US Dollar",
"symbol": "$"
},
"time_zone": {
"name": "America/Los_Angeles",
"offset": -8,
"current_time": "2019-01-14 03:30:00.135-0800",
"current_time_unix": 1547465400.135,
"is_dst": false,
"dst_savings": 1
}
}
You can also filter specific parameters from the entire response.
After getting the visitor's location, you can also redirect him to the page according to his language/currency by using this simple code:
var request = new XMLHttpRequest();
request.onreadystatechange = function() {
if (4 === this.readyState && 200 === this.status) {
var json = JSON.parse(this.responseText);
if (json.country_code2 == "FR" && window.location.hostname !== "http://fr.exmaple.com") {
window.location.href = "https://fr.exmaple.com";
} else if (json.country_code2 == "US" && window.location.hostname !== "http://us.exmaple.com") {
window.location.href = "https://us.exmaple.com";
} else if (json.country_code2 == "UK" && window.location.hostname !== "http://uk.exmaple.com") {
window.location.href = "https://uk.exmaple.com";
}else {
window.location.href = "https://exmaple.com";
}
}
}
request.open("GET", "https ://api.ipgeolocation.io/ipgeo?apiKey=YOUR_API_KEY&fields=country_code2", false);
request.setRequestHeader("Accept", "application/json");
request.send();
I hope it would be the best option for you.