In this resource guide, you’ll find the resources that you’ll need to determine a user’s latitude and longitude via programming. There are also some non-technical resources below, so please stay even if you are not interested in the actual implementation.

How to Get the User’s Longitude and Latitude?

The process of finding out the coordinates or location of the user is called Geolocation. A common implementation is by making use of a Geolocation API or SDK.

Before we again, I just want to stress on the fact that the only way to get accurate user coordinates is through GPS. And that implies that you need the user’s permission to access their device.

Geolocation Overview

Developing a geolocation system from scratch is beyond the scope of this guide, but you can find out more about how it’s done at a higher level from these overviews.

  • Investopedia: Geolocation. A concise article on geolocation with examples on financial services and privacy issues. Read it here.
  • Khan Academy: Geolocation. This article provides an answer to the question of how does user geolocation work. Read it here.

W3C Geolocation API

Some people call it the HTML5 Geolocation API or JavaScript Geolocation API or just Geolocation API.

On the client side, user locations can be determined using the W3C Geolocation API. For this API to work, you’ll need obtain user permission. The accuracy of the API depends on the best information available to the API (e.g. GPS, IP, etc.). The result of the API could yield 4 location attributes depending on the information source. The attributes are longitude, latitude, altitude, and accuracy.

The API is free.

Google Geolocation API

Another popular API that does geolocation is the Google Map Geolocation API. The API can be accessed anywhere–both frontend and backend. It detects user location based on cell towers and WiFi nodes, with the option to fall back to IP geolocation.

I’m sharing this API here for completeness due to how popular it seems to be. But personally, I think the requirements for cell towers and WiFi nodes make it a little difficult to implement for many applications.

Google Geolocation API is not free but you get a free $200 Google Maps Platform credit every month that you can use toward Geolocation.

See it here.

Determine User Coordinates Without Client Device Access

In some cases, a programmer might not have access to the client device. Without device access, more accurate location information sources such as GPS and WiFi nodes are not available to any Geolocation API or SDK. This means you’ll have to rely on IP geolocation.

Unfortunately, IP geolocation is not very accurate. It will get the country right and most probably the city too. But beyond that, it’s not very reliable. You’ll still be getting the latitude and longitude that you’re looking for, but just keep it that it won’t be very helpful.

Without device access, HTML5 Geolocation API is out of the question. You can’t use that without user approval. Other APIs such as Google Geolocation API are still usable, although at reduced accuracy.

Other Platform-Independent Geolocation APIs

I’m not going to review all available Geolocation APIs. If you’re interested in this topic check out these reviews:-

  • Rakuten Rapid API: Top 10 Best IP Geolocation API – See it here.
  • RapidAPI: Top 10 Best IP Geolocation APIs. See it here.

Language-Specific Geolocation Programming Modules

  • PHP: Geoip – this uses MaxMind’s GeoIP legacy database which is older. See it here.
  • PHP: MaxMind GeoIP2 Code example. See it here.
  • Rust: maxminddb – library for reading MaxMind GeoIP2 and GeoLite2 databases. See it here.
  • Python: geopy – this Python module provides access to the OpenStreetMap Nomatim API. It isn’t really a Geolocation module, it’s actually a Geocoding module. But if you know the user address, this will help you find the coordinates. See it here.
  • Android: Build location-aware apps. Read it here.
  • Android: Location package reference. See it here.
  • Apple: Getting user location. See it here.

My Personal Recommendations

For accuracy, you’ll want access to the user device’s GPS. Use the HTML5 Geolocation API for web apps. For native mobile apps, use a native library.

Without device access, your best bet would be IP geolocation. This can be done by using a number of APIs shared above, and even Google Geolocation API.