Here’s something that happens a lot with successful apps:
In this article, we walk you through how to use the GeoDB Cities API to implement Step 3 – getting the user’s current city.
The first point to consider is what we mean by ‘current city’. Consider a user drifting 100 miles off the coast of Burma with a satellite phone (and a life jacket). Since Atlantis isn’t yet a thing, we clearly mean the nearest city to the user’s current location. And no offense to podunk towns and villages, but we probably also mean only cities large enough to either have a landing strip or a dungeon to keep a flying dragon. In other words, we mean the nearest city to the user’s current location that has at least some sufficiently large number of people living in it.
The GeoDB Cities API is an online database of the world’s cities, regions, and countries. It offers various city-querying operations with optional filters to get just the cities relevant to a given use-case.
For this use-case, we leverage the Find-Cities-Near-Location operation:
GET /v1/geo/locations/{LOCATION_ID}/nearbyCities ?radius={RADIUS} &minPopulation={MIN_POPULATION} &limit=MAX_RESULTS
Where:
So the sailor floating somewhere in the Bay of Bengal might send the following HTTP request from his phone:
GET /v1/geo/locations/19.098801+92.979204/nearbyCities ?radius=100 &minPopulation=100000 &limit=1
unirest.get("https://wft-geo-db.p.rapidapi.com/v1/geo/locations/19.098801%2B92.979204/nearbyCities?limit=1&minPopulation=100000&radius=100") .header("X-RapidAPI-Host", "wft-geo-db.p.rapidapi.com") .header("X-RapidAPI-Key", "YOUR_RAPID-API_KEY") .end(function (result) { console.log(result.status, result.headers, result.body); });
$response = UnirestRequest::get("https://wft-geo-db.p.rapidapi.com/v1/geo/locations/19.098801%2B92.979204/nearbyCities?limit=1&minPopulation=100000&radius=100", array( "X-RapidAPI-Host" => "wft-geo-db.p.rapidapi.com", "X-RapidAPI-Key" => "YOUR_RAPID-API_KEY" ) );
response = unirest.get("https://wft-geo-db.p.rapidapi.com/v1/geo/locations/19.098801%2B92.979204/nearbyCities?limit=1&minPopulation=100000&radius=100", headers={ "X-RapidAPI-Host": "wft-geo-db.p.rapidapi.com", "X-RapidAPI-Key": "YOUR_RAPID-API_KEY" } )
With this request, we should get a single result: the nearest city within one hundred miles of latitude=19.098801, longitude=92.979204, with at least 100,000 people.
{ "data":[ 0:{ "id":71941, "wikiDataId":"Q738748", "type":"CITY", "city":"Sittwe", "name":"Sittwe", "country":"Myanmar", "countryCode":"MM", "region":"Sagaing Region", "regionCode":"01", "latitude":20.14624, "longitude":92.89835, "distance":72.56 } ] "metadata":{ "currentOffset":0, "totalCount":1 } }
Here, we see the nearest city is Sittwe, Myanmar (formerly Burma). In addition, the response indicates the straight-line distance from the specified location is about 72 miles.
If we want Sittwe’s population, we will have to make another request to get its details as follows:
GET /v1/geo/cities/71941 (or /Q738748)
Try it here in the online demo.
This example should show how easy it is to use GeoDB Cities to query and incorporate just the needed city data in your apps. There’s a whole lot more to check out in the API docs, including the ability to display results in multiple languages.
Explore away, Magellan!
The author’s views are entirely his or her own and may not reflect the views of RapidAPI.
We're thrilled to announce the latest update to the Rapid Enterprise API Hub (version 2024.3)!…
Are you curious about what your API consumers are searching for? Is your Hub effectively…
The RapidAPI team is excited to announce the February 2024 update (version 2024.2) for the…
This January's release brings exciting features and improvements designed to empower you and your developers.…
Rapid API is committed to providing its users with the best possible experience, and the…
In today's fast-paced digital world, APIs (Application Programming Interfaces) have become the backbone of modern…