It’s obvious that if you visit a weather site, your city will be highlighted even before you start typing. If you make purchases online, your checkout page automatically switches the payment currency to the appropriate one. Behind the scenes of both cases, there was the activity of a geolocation API.
While developers use such services all the time, not everyone knows which information they return and which part of it is credible. In this article, you will learn about the two types of geolocation APIs, which information they return, and the reliability of this information.
Two different services have one common name
“Geolocation API” sounds simple. But in this case, it refers to two completely different services.
The first option is the built-in browser API accessible via navigator.geolocation, which estimates the location of the device using GPS, Wi-Fi networks, or cell towers. It works only if a user gives permission, and only over a secure HTTPS connection. Such APIs are found in almost all popular web browsers today.
The second option is a web service like MaxMind, IPinfo, or ipapi providing an estimate of where the specified IP address is used. In such a situation, there’s no need for users to give any permissions, as an estimation is made using databases that match IP address ranges to locations.
One asks the device about its location. Another asks a database about the location related to an IP address.
What information does browser geolocation API return?
After successfully getting the user’s permission, the API returns the following structure:
- timestamp;
- coords, which in turn contain:
- longitude and latitude in decimal degrees;
- accuracy in meters, representing the range in which the location estimate is correct;
- altitude and altitudeAccuracy, heading, and speed (frequently null on laptops and desktops).
In case of receiving the PERMISSION_DENIED error code 1 (if the user doesn’t want to give permission), you should have a backup plan in order to handle such an event.
What information does IP geolocation API return?
Usually, it returns a JSON object that contains a bit more context and has slightly lower accuracy compared with the browser API. Such an object usually consists of the following information:
- country (also, usually there’s a two-letter ISO country code);
- region/state;
- city;
- postal code;
- coordinates (approximate latitude and longitude);
- time zone.
Also, such API providers frequently add the following information about networks:
- autonomous system number;
- network name of the Internet Service Provider or organization;
- sometimes information about connection type (mobile, residential, business).
And also, depending on the subscription plan of the service, some services add information about:
- the usage of a Virtual Private Network (VPN) service;
- proxy service;
- Tor relay node (which hides a user’s true location);
- hosting provider.
Keep in mind that the coordinates returned in response to IP-based services won’t refer to any street or specific place. Usually, they correspond to the midpoint of a city or a postal area. Moreover, some providers add additional fields that demonstrate the range (accuracy) of this information, and some providers also add a confidence value field. Also, in this case, country information is very reliable, and city information is not so accurate.
Comparing information obtained from API with information obtained by quick look-up
First of all, before starting to use any particular API, you should compare the information returned with a quick manual check by using the same IP address in an IP address location lookup tool such as the one provided by Surfshark. Make sure that both providers give you the same ISP and country. If the city information differs, it means that the IP belongs to a mobile network or corporate networks routing traffic using one remote IP address.
And also, you can quickly do the following tests using a terminal:
- run whois in order to determine the owner of the IP address;
- do a reverse DNS lookup (using dig -x or nslookup commands), as hostnames sometimes include city or airport names;
- make requests to the provider directly via curl and parse the obtained JSON (pay attention to accuracy and confidence fields).
You can test a limited number of addresses that you know for sure, like:
- your work office connection;
- your mobile hotspot (if you are on a smartphone);
- VPN server.
You will quickly understand where your API provides you with reliable information and where you get just guesses.
Limitations and privacy
IP address information can change over time. Address blocks become reassigned. Mobile providers distribute their addresses among large geographical regions using carrier-grade NAT. And services like Virtual Private Network and Apple’s iCloud Private Relay intentionally hide the real IP address of users from external applications.
Privacy policies are important too. According to GDPR, an IP address can qualify as personally identifiable information. Geographical location of a device is considered sensitive personal information in many jurisdictions. Don’t collect more data than your feature requires. You should avoid storing exact coordinates if your app works with the city or country.
Conclusion
Geolocation API returns more information than a dot on the map. Browser geolocation provides you with precise coordinates along with an accuracy value, but in exchange, it requests permission. IP-based services provide you with information about a country, city, and time zone. Read each field carefully with its accuracy in mind, test the results manually, and consider geolocation information as just another hint and nothing else.









