IP Geolocation

What is IP geolocation? How does it relate to geocoding?

IP geolocation is the process of determining the real-world geographic location of an internet-connected device, such as a computer or mobile phone, based on its IP address. This can be used to determine the location of the device, as well as its internet service provider (ISP) and other information.

Geocoding, on the other hand, is the process of converting a physical address or location into geographic coordinates, such as latitude and longitude (or vice versa).

The key difference: IP geolocation starts from an IP address and returns an approximate location, typically no more precise than city or postal-code level. Geocoding starts from an address (or coordinates) you already have and returns precise geographic information about it. They're often used together. See "Combining IP geolocation and geocoding" below.

Accuracy

IP geolocation is approximate, not precise. Ttreat it as a reasonable guess, not a reliable location.

The main factors that affect its accuracy:
  • Database freshness. IP geolocation relies on databases mapping IP ranges to locations. These are updated regularly, but ISPs reassign IP blocks over time, so any database can be out of date.
  • Precision ceiling. Even with a good match, most IP geolocation services only resolve to city or postal-code level — not a street address or precise coordinates. If you need street-level accuracy, use browser geolocation instead.
  • Mobile networks. Mobile carriers often route many users through a small number of shared IP addresses (carrier-grade NAT), which can place a mobile visitor's IP geolocation result a city or more away from their actual location.
  • VPNs and proxies. These mask a user's real IP address, so the geolocation result will reflect the VPN/proxy server's location, not the user's.

When to use IP geolocation vs. browser geolocation

  • Use IP geolocation when you want a coarse, no-consent-needed default. For example, pre-selecting a country/currency, or coarse analytics. It works silently, with no permission prompt, on page load.
  • Use browser geolocation when you need an accurate location and can ask the user for permission For example, "find stores near me" or turn-by-turn directions. It requires an explicit consent prompt but can be GPS-accurate. Learn more in our browser geolocation guide.

A common pattern is to use IP geolocation as an immediate, silent default, then offer browser geolocation as an opt-in for a more precise result.

Combining IP geolocation and geocoding

Once you have used IP geolocation to obtain the user's approximate location in the form of coordinates, those coordinates can be reverse geocoded using the OpenCage geocoding API and thus turned into geographic information like an address, country, state/province, time zone, etc.

Code example

Here's a small javascript example combining IPinfo (see Recommendations below) with the OpenCage geocoding API.

async function locateByIP() {
  // Step 1: look up the visitor's approximate coordinates from their IP
  // sign up for an IPinfo token at https://ipinfo.io
  var ipinfo_token = 'YOUR-IPINFO-TOKEN';
  var ipinfo_response = await fetch('https://ipinfo.io/json?token=' + ipinfo_token);
  var ipinfo_data = await ipinfo_response.json();

  if (!ipinfo_data.loc) {
    console.log('unable to determine location from IP');
    return;
  }

  console.log('approximate coordinates: ' + ipinfo_data.loc); // e.g. "52.5200,13.4050"

  // Step 2: reverse geocode those coordinates with OpenCage to get
  // richer location details (formatted address, country, timezone, etc)
  var opencage_key = 'YOUR-API-KEY';
  var opencage_url = 'https://api.opencagedata.com/geocode/v1/json'
    + '?key=' + opencage_key
    + '&q=' + encodeURIComponent(ipinfo_data.loc)
    + '&pretty=1'
    + '&no_annotations=1';

  var geo_response = await fetch(opencage_url);
  var geo_data = await geo_response.json();

  if (geo_response.status === 200 && geo_data.results.length > 0) {
    console.log('formatted location: ' + geo_data.results[0].formatted);
    console.log('country: ' + geo_data.results[0].components.country);
  } else {
    console.log('unable to geocode! Response code: ' + geo_response.status);
  }
}

locateByIP();

Recommendations

If you need IP address to location lookups, there are several free and paid services out there. Have a look at IPinfo or ipregistry. In Episode 62 of the Geomob podcast OpenCage co-founder Ed Freyfogle interviews Ben Dowling, the founder of IPinfo, and they discuss IP geolocation in detail.

Privacy considerations

A user's IP address can be considered private information

Depending on your jurisdiction, it may be treated as personal data requiring a legal basis to process. It can be used to infer a user's approximate location and identify their internet service provider (ISP), which could be used to track a person's online activity or compromise their privacy. Even though IP geolocation doesn't require an explicit consent prompt the way browser geolocation does, it's still worth being transparent in your own app's privacy policy about the fact that you're deriving location data from a visitor's IP address.

Further reading

Happy geocoding!

Start your free trial

2,500 geocoding API requests/day - No credit card required