How to get location data over WhatsApp?

Location data can be important and useful data points on a chat service. Let's look at how you can get location data over WhatsApp.

A big thank you to Reach Digital Health for this article. It was originally published in 2020.

Part of South Africa's COVID-19 response was a WhatsApp line, Contact NDoH, and a feature on that line is the HealthCheck, which allows people to answer a series of questions, and in response get advice on whether they should self-isolate or get tested.

For the reporting of this data, location is an important data point to be able to determine increases in self-reported symptoms in specific areas, which can inform where support is needed.

Using location pins

The first way in which people can send us their location on WhatsApp is through a location pin. We also give directions in the message text, to help if they don't know or are unsure on how to send a location pin in WhatsApp. The instructions in the message that we use are as follows:

Please share your location ūüďć using WhatsApp by following these steps:

  • Tap the attach icon (+ or¬† ūüďé) on the bottom of WhatsApp.
  • Select location
  • This will bring up a map with a pin showing your current location. Tap Send Your Current Location if this is your address.
  • You can also choose from various other nearby locations if you think that the GPS is a bit off.

Alternative solutions to location pins

Some people might still struggle with sending a location pin, or for various reasons might not want to send us a location pin. This service was also offered over USSD, where location pins aren't possible. So we needed a backup solution to be able to collect location.

For this, we used the Google Places API. We also investigated alternative services, such as Open Street Map, but found that we got the best results for the kinds of search terms that we would expect from Google's service.

How this works for the person using the service is that they get prompted to enter their nearest Suburb, Township, Town or Village (or nearest). They then get presented with the first result, and asked to confirm it, or to try with a different entry.

On the backend, we take their search term and send it to the Google Places Autocomplete API, and send the first result back to the user. If they confirm, then we use the Google Places Details API to get the longitude and latitude details of the search result, and store that. Since that's the same detail we would get from a location pin, we can compare data across both without worrying which method the user chose.

Lessons learned to enhance the process

  • Use a session token for each session when making requests to the API. If the user tries again with a different search term, and we don't use the same session token for those multiple requests, it ends up being more expensive as you get charged for each search. With the session token you only get charged for a single request, even if the user makes multiple searches.
  • Store the result for repeat healthchecks. This not only improves the user experience, as they have to answer less questions for repeat healthchecks, but also saves on costs as we only do one location search for multiple healthchecks.
  • Restrict search results to South Africa using the components parameter. This makes it easier to find the place you're looking for, as well as increasing the accuracy of the data.
  • Use a location hint with the location parameter. In the previous question, we have the person select their province from a list. We then use this province as a location hint, to prioritise location results in that province, to get better search results.
The Python code that is responsible for interacting with the API is open source, and can be found here.

Was this article helpful?

How to block a user?

Contact