---
title: "Postcodes instead of cities"
description: "Hargapedia used to let people type any city or town when setting their location. We've replaced that with a five-digit postcode, although the app doesn't quite use it yet."
url: "https://rayson.me/writing/hargapedia-postcodes/"
category: "Engineering"
author: "Janarayson"
published: 2020-03-13
---

When you create a Hargapedia account, we ask where you're from. It's part of the sign-up process, and you can change it later in Settings. The idea is to show promotions and retailers that are relevant to your location.

Until this week's update, you picked your state from a list and typed the name of your city or town. We've kept the state selector, but replaced the city field with a five-digit postcode.

The old approach had a problem. People could type whatever they wanted into the city field, including misspellings or names that didn't quite match where they lived. We had no way to check whether the entry was correct.

A postcode gives us something more consistent to work with, and we can use it to look up the town and state rather than rely on what someone typed.

## What we were actually using

The city field wasn't doing much in the first place.

Each state in Hargapedia has a pair of coordinates, representing a point somewhere within that state. When you choose a state, those coordinates become your location. The app uses them to find nearby retailers during sign-up and saves them as the latitude and longitude on your profile.

The city name doesn't affect those coordinates.

We joined whatever you typed with the state name and saved it as a label, something like "Petaling Jaya, Selangor". That was the only place we used it.

So although we were asking for a city, the app was still treating everyone in the same state as being at the same approximate location.

## Asking for five digits instead

The new postcode field is restricted to five digits. It opens the number pad, removes anything that isn't a number as you type, and stops accepting input after five characters.

You still need to select your state, and both fields have to be filled before the OK button works.

We've also changed what gets saved. Instead of combining the city and state into one label, the profile now stores the state as its label and the postcode in a separate field.

The database already had a postcode column from when we first added the location fields, so there was no need to change the database structure. The app just needed to start collecting it.

There is one thing I missed. The question above the field still says "Which city/town are you from?" even though we're now asking for a postcode. The placeholder says Postcode, and the number pad comes up when you tap it, but the question itself is a leftover from the old form.

<Screens
  screens={[
    { src: newAccount, alt: "The New Account screen, second step: the text ...you're almost done!, a Your location field with a pin icon, and a greyed-out Continue button" },
    { src: setLocation, alt: "The Set Location screen: a note that we'll share hyper-local promotions relevant to where you're at, a Which state are you from? dropdown reading Select state.., a Which city/town are you from? field with the placeholder Postcode, and an OK button" },
    { src: setLocationTyped, alt: "The same Set Location screen with Selangor chosen, 47500 typed into the postcode field and the number pad open" },
  ]}
  caption="The location step during sign-up, followed by the new postcode field before and after entering a postcode."
/>

For now, that's all the change does. We save the postcode, but the app still uses the state's coordinates when it needs a location. We're collecting more specific information without actually using it to locate anyone more precisely.

<Note label="Update, January 2021">
The postcode finally has another use in [Hargapedia 3.0](/writing/hargapedia-3/).

The new About Me screen includes address fields, and entering a postcode automatically fills in the town and state. We no longer have to ask people to type those details themselves.

The first implementation used an API. We put a list of Malaysian postcodes in an S3 bucket and exposed it through API Gateway. When the app submitted a postcode, the API looked it up, returned the matching location and kept the data cached for an hour.

A couple of weeks later, we moved the lookup into the app itself.

The postcode list is now a JSON file bundled with the app, with each postcode as a key and its town and three-letter state code as the value. Finding a postcode is just a property lookup, so it's fast and works without an internet connection.

The data came from [heiswayi's malaysia-postcodes repository on GitHub](https://github.com/heiswayi/malaysia-postcodes), which contains Pos Malaysia postcode information down to street level. We simplified it to one town per postcode for our lookup.

So the postcode we started collecting in March now saves people some typing when they fill in their address. And unlike the old free-text city field, it gives us a consistent value to look up.
</Note>
