For any webmaster interested to know where his users came from there are two methods available:
- compare users IP address to a database of longitude/latitude or use an online API for this purpose;
- or use a W3C HTML Geolocation API.
HTML Geolocation API (Request from user using JS) provides scripted access to geographical location information associated with the hosting device. This is the most powerful and precise method of user's geolocation available.
Common sources of location information include Global Positioning System (GPS) and location inferred from network signals such as IP address, RFID, WiFi and Bluetooth MAC addresses, and GSM/CDMA cell IDs, as well as user input. Although W3C Geolocation API doesn't guarantee that the API returns the device's actual location it will try it's best for sure.
But it is more complicated to implement and it will require user to explicitly allow to use his location data:
List of the most reliable IP Geolocation APIs
IPData.co
Endpoint: api.ipdata.co?api-key=test
API Key: Required
API Latency: 65ms
Their smallest plan is $10 a month for 2500 requests daily or 75,000 requests a month.
Maxmind
Endpoint: https://{account_id}:{license_key}@geoip.maxmind.com/geoip/v2.1/city/{ip}
API Key: Required
API Latency: 44ms
For basic city level geolocation via the City endpoint, it'll cost $400 per million calls or $0.0004 per request.
IPInfo.io
Endpoint: ipinfo.io/json
API Key: Not Required
API Latency: 165ms
Their Basic Plan including 250,000 requests a month for a $49 monthly.
ipapi.com
Endpoint: api.ipapi.com/api/161.185.160.93
API Key: Not Required
It's free for small projects up to 10,000 requests per month.
ipapi.co
Endpoint: ipapi.co/json
API Key: Not Required
API Latency: 270ms
It's free up to 1000 requests a day, than it'll cost starting from $15 monthly depending from a daily volume of requests.
db-ip.com
Endpoint: api.db-ip.com/v2{api-key}/1.1.1.1
API Key: Required
API Latency: 225ms
It'll cost from €15.90 for 50,000 requests daily
ipwhois.io
Endpoint: free.ipwhois.io/json{IP} or free.ipwhois.io/xml{IP}
API Key: Not Required
API Latency: 75ms
It's free for small projects up to 10,000 requests per month (IP and Referer identification).
Автор: ushim