Is it possible to use Django’s location features to integrate with tools like 'What city is this?' that use GPS data?

I’ve been using Django for a while and have been exploring various apps and tools online, and I came across a cool tool called “What city is this” which uses the device’s GPS location to identify the city you’re currently in. It got me thinking about how Django interacts with GPS data and if it could work with something similar in a web application setting.

For instance, I know Django has some built-in support for handling geolocation data with packages like GeoDjango, but I was wondering how far that support extends when it comes to incorporating real-time GPS data, especially when accessed via a user’s browser or device. Could Django effectively handle that kind of GPS data retrieval and process it to determine a city or address, just like “What city is this?” does?

Also, is there a way to get Django to work smoothly with the GPS data from a browser, especially on mobile? I imagine this would involve JavaScript at some level, but I’m more curious about Django’s role in receiving and processing the location info. Are there specific libraries, settings, or considerations when it comes to accessing GPS from a device and working with that data in Django?

Another thought is how Django would handle situations where GPS data is unavailable or restricted by the device. Would it need to rely on IP-based geolocation as a fallback, and how accurate would that be compared to GPS? I’m just trying to understand Django’s role better in scenarios like these where precise location data is important.

Has anyone tried something similar with Django, maybe integrating a tool that behaves like “What city is this?”? Would love to hear experiences or best practices for handling location data in Django, especially when pulling it from GPS.

Welcome @HampusKatya !

Absolutely.

See the docs at Geolocation API - Web APIs | MDN for details on the JavaScript side.

Like any other data coming from JavaScript, it’s character strings coming in. For example, if you create a view that accepts a JSON object from an AJAX call, then your view will receive it as a dict. What significance or semantics that apply to that data coming in is up to you to handle.
(There may be some Python libraries on pypi designed to facilitate the handling of input from those JavaScript APIs.)

For example, you might get something like this:

{"timestamp":1725553197356,
 "coords": 
 {"accuracy":2066.2650796269627,
  "latitude":35.4109152,
  "longitude":-99.2307648,
  "altitude":null,
  "altitudeAccuracy":null,
  "heading":null,
  "speed":null
 }
}

What libraries you are looking for are going to depend upon what you’re going to do with this data. If you’re just passing this data along to another API or library, you may not need to do anything with it at all.

Keep in mind here that Django, running on the server, is not “pulling” data at all. It will accept whatever data is given to it, from whatever sources the browser chooses to use.

You could do that. But, however bad GPS location may be in some areas, IP-based geolocation is typically worse.
In a large percentage of cases, IP-based geolocation is inaccurate at best and out-right wrong at worst. My IPv4 address is off by about 5 miles from where I’m sitting now, and my IPv6 address resolved to a location about 40 miles away. When I’m connected to either one of my work networks, I get geolocated in either New York or Virginia.

But again, none of this has anything specifically to do with Django. These are all issues associated with the client, over which Django has no control.