
One among my favourite latest side-projects has been Meet Cute, a playful net map that generates tiny “micro-romance” tales everytime you click on on a location. The self-esteem is easy: click on on a map, and out comes a love story set within the nearest city.
However behind that easy expertise was a not-so-romantic technical downside: discovering the title of the closest city.
At first, I leaned on the Overpass API, which is constructed on high of OpenStreetMap. Each time a person clicked someplace on the map, my code would:
-
Ship a question to Overpass.
-
Ask it to return the closest
place=metropolis|city|village
node. -
Use the closest title to plug into my romance grammar.
This labored superbly… typically.
Overpass is a shared useful resource, and as many OSM builders know, it may be gradual when overloaded. On dangerous days, a single click on may stall Meet Cute for a number of seconds. Worse, I didn’t need to preserve hammering the Overpass servers each time somebody bought carried away clicking for story after story.
Constructing My Personal Geocoder
So I made a decision to roll my very own light-weight client-side geocoder.
I realised that as an alternative of calling Overpass for each click on, I may pre-build an inventory of cities and cities and serve it as a static file. The truth is I knew that TripGeo has simply such an inventory of over 11,000 cities and cities around the globe, that it makes use of for its each day Scrambled Maps Problem.
Right here’s what I did:
-
I grabbed the TripGeo dataset of world cities (latitude, longitude, title).
-
I saved it right into a
cities.json
file and hosted it on GitHub Pages. -
I wrote just a little Geocoder class that masses this JSON into reminiscence and, given a latitude/longitude (outlined by a person map click on), finds the closest matching metropolis by brute drive distance calculation.
All the geocoder lives in a single JavaScript class which is loaded into Meet Cute after which referred to as when the map must discover a new location.
Now, when the person clicks on the map, my code doesn’t must make a community name in any respect. It simply seems to be up the closest metropolis regionally. The result's that Meet Cute is all of a sudden very snappy. Customers not have to attend for Overpass to reply and each click on immediately produces a brand new love story.
You Can Use the Geocoder Too
As a result of the code is hosted on GitHub you may as well drop my light-weight geocoder straight into your personal map initiatives. All it's good to do is embody the script and level it to the cities.json file hosted on GitHub Pages. For instance:
const geocoder = new CityGeocoder("https://mapsmania.github.io/geocoder/cities.json");
const nearest = geocoder.reverse(52.517, 13.388);
console.log(nearest.title); // "Berlin"
That’s it - no API keys, no server calls, and no ready on exterior companies. Only a single JavaScript class, a static JSON file of cities, and you've got a ready-to-go reverse geocoder that works immediately inside any JavaScript mapping library.
⚠️ One factor to notice: the dataset is proscribed to simply over 11,000 cities and cities worldwide. That makes it light-weight and quick, nevertheless it additionally means it’s not as detailed as industrial geocoders. It really works completely in the event you solely must establish the closest main city or metropolis, nevertheless it received’t return smaller villages, streets, or particular person home or enterprise addresses.