In our super-connected world, looking up physical addresses and getting accurate geographic locations is becoming an almost must-have skill for our apps. Would you like to enhance your application with accurate forward and reverse batch geocoding? Wouldn’t it be nice to have a free, lightweight and easy-to-use geocoding REST API to geocode any global address or set of coordinates in real-time? Well you’re in luck, we’re going to show you how to do that and, furthermore, we’re going to do it with a really tiny amount of code.
What exactly do we mean by “geocoding”?
Before we dive into how we’re going to do things, we should perhaps spend a minute or two going over some of the technical terms we’re going to cover.
Forward geocoding is the process of looking up a plain-text address or place name (e.g. Eiffel Tower), whereas reverse geocoding is performed by passing latitude and longitude values of a desired location to the API. If successful, both types of geocoding return an extensive array of location-related data as well as multiple potential results along with confidence scores.
In this article we will see how fast and easy it is to use RAD Studio and Delphi to create a FireMonkey multidevice application using the LowCode Wizard in addition to a REST client library to take advantage of PositionStack API and retrieve a JSON format response for worldwide news, headlines and blog articles in real-time.
What is the PositionStack API?
PositionStack API offers instant access to live geocoding for global places and coordinates around the world. This is possible to be done for free (up to first 25,000 calls/month; no credit card required) and much more is available at very affordable prices and scalable to the use you make with no upfront commitments. The PositionStack API covers 2+ billion addresses sourced from multiple, highly reliable sources, such as Nominatim, GeoNames, Pelias and OpenStreetMap. Updates to our dataset are made multiple times per day and smart filters and algorithms ensure the highest possible level of consistency across API requests and accuracy in geo data responses.
Our RAD Studio and Delphi applications will be able to call the API and request information based on the name of parameters you provide.
How do I set up the PositionStack API?
Make sure you refer to PositionStack API website (https://PositionStack.com/) and and SignUp for the free Plan providing only your email and some basic information (no credit card required). Once you are in the website will redirect you to a Quickstart guide dashboard and your API Access Key will be provided. The Access Key unique, personal and is required to authenticate with the API. Keep it safe!
How do I call PositionStack API endpoints?
Now all we need to do is to call the API base URL (http://api.positionStack.com/) via a HTTP POST method with no JSON request body needed and some few requested parameters added to the URL address depending on the ednpoint we choose to call. One can do that using REST Client libraries available on several programming languages.
PositionStack offers two API endpoints to choose from
- Forward Geocoding: Geocoding by free-text place name or address.
- Reverse Geocoding: Geocoding by coordinates or IP address.
Our demo will focus on the Forward Geocoding Endpoint but all the others follow similar logic but with different parameters passed. For a complete and detailed list of endpoints and its parameters make sure you refer to PositionStack Quickstart guide (https://positionStack.com/quickstart)
// Forward Geocoding API Endpoint
? access_key = YOUR_ACCESS_KEY
& query = 1600 Pennsylvania Ave NW, Washington DC
// optional parameters:
& limit = 10
& output = json
// more parameters available please refer to the API Documentation (<a href="https://positionStack.com/documentation" target="_blank" rel="noreferrer noopener">https://positionStack.com/documentation</a>)
What does the PositionStack API endpoint return?
After the call the main results will be a very complete list with information filtered based on the parameters you sent. The list below shows the result I got when I called the API informing my personal address at the parameter “query”. As you can see below the values for latitude and longitude returned by the API are very close to the one we get from GoogleMaps if we search for my company name.
"label":"Salvador, BA, Brazil"}]}
Here’s that response, shown in Google Maps
How do I connect my applications to PositionStack API?
Once you have followed basic steps to set up the PositionStack API we need to make sure we are able to connect and communicate with it before we start writing some code.
RAD Studio Delphi and C++Builder make it very easy to connect to APIs as you can you REST Debugger to automatically create the REST components and paste them into your app.
In Delphi all the job is done using just 3 components to make the API call. They are the
TRESTResponse. Once you connect the REST Debugger successfully, copy and past the components you will notice that the API URL is set on the BaseURL of TRESTClient. On the TRESTRequest component you will see that the request type is set to
ContentType is set to
ctAPPLICATION_JSON, and that it contains one request body for the POST.
Run your RAD Studio Delphi and on the main menu click on Tools > REST Debugger. Configure the REST Debugger as follows marking the content-type as application/json, and adding the POST url, the JSON request body and the API key you created.
Once you click the Send Request button you should see the JSON response
How do I build a Firemonkey Multidevice (Windows/MacOS desktop or Android/iOS mobile) application using the PositionStack API?
Low code app development is increasingly popular today, and for good reason: it speaks to the demand for software paired with the difficulty of creating it. In many ways, Delphi led the low code revolution: RAD, where you build software by connecting components in a visual designer, is low code. The key with many low code solutions is extensibility: low code which cannot be extended to a traditionally coded app has a hard upper limit of functionality. If you’re a technology adviser, you do not want to recommend something which will limit your client or company. The best development solutions that target low code provide an avenue to grow and extend. In this too, Delphi excels.
Until recently in Delphi you still had to create a new project and create the forms or screens yourself – no matter how little code you may have to write to get powerful functionality when you did. This is something we’ve now addressed with an addon to 10.4.2. Run your RAD Studio Delphi and on the main menu click on Tools > GetIt Package Manager. In GetIt for 10.4.2 and later search for “Low Code App Wizard for FireMonkey”, our technology for cross-platform applications.
Using RAD Studio Delphi to create the app with a few simple clicks
Now that you have installed Low Code App Wizard for FireMonkey and that you were able to successfully configure and test your API calls on the REST Debugger, go back to the REST Debugger and click the Copy Components button. Open RAD Studio Delphi and on the main menu click File > New> Multi-Device Application Delphi, select the FireMonkey Template App as shown below and follow some simples Wizard steps to create your Low Code application.
The result will be a fully functional cross-platform application, which already contains basic functionality that every mobile app will need — you can run and use the app as is — and hooks to build more, plus demonstrates application development best practices with a very clean extensible design to add more screens with low code, plus a set of tests to ensure your app remains high quality as you customise it.
Can you configure the results of the RAD Studio FireMonkey FMX Low Code Wizard?
The app that Delphi builds for you is configurable through the wizard and you can choose any set of the following:
- User accounts: sign up, sign in, and a profile screen, with hooks for your own authentication
- Settings screen (where you can change the app theme (light or dark), plus some example settings you can reuse or extend)
- Contacts screen
- A database layer: using Interbase, store data in your app
- An example new screen you can use as a template or example for adding new functionality
As you can see the Wizard saves you a lot of time as it creates a complete project with many forms ready for you to implement the code. Open the recently created NewFormFrame and hit Ctrl+V in order to paste the components you copied from REST Debbuger. This acction will result in three components added to the the form RESTClient1, RESTRequest1 and RESTResponse1.
Adding some additional OnClick code
Now we will simply add very simple code to a TButton OnClick event to make sure every thing is configured correctly and voila! In five minutes we have made our very first call to PositionStack API and we are able to receive JSON response for whatever parameter we want.
procedure TNewFormFrame.Button1Click(Sender: TObject);
memo1.Lines.text := '';
RESTClient1.Accept := 'application/json, text/plain; q=0.9, text/html;q=0.8,';
RESTClient1.AcceptCharset := 'UTF-8, *;q=0.8';
RESTClient1.BaseURL := 'http://api.positionstack.com/v1/forward';
RESTClient1.HandleRedirects := True;
RESTClient1.RaiseExceptionOn500 := False;
//here is were we pass the access_key and the additional parameters
RESTRequest1.Resource := Format('?access_key=%s&query=%s', [edit1.Text, edit2.Text]);
RESTRequest1.Client := RESTClient1;
RESTRequest1.Response := RESTResponse1;
RESTRequest1.SynchronizedEvents := False;
RESTResponse1.ContentType := 'application/json';
memo1.Lines.text := RESTResponse1.Content;
The sample application features a TEdit as a place to paste in the access key and additional TEdit for each parameter, a TMemo to display the JSON results of the REST API call. Now you have every thing you need in order to interate with the response data and make your application process the information and present it visually in the way it better suits your needs!
What did we just show you in just FIVE minutes?
In this blog post we’ve seen how to sign up for the PositionStack API in order to get instant access to structured and readable position geocoding data. We’ve seen how to use the RAD Studio REST Debugger to connect to the endpoint and copy that code into a real application. And finally we’ve seen how easy and fast it is to use RAD Studio Delphi and the Low Code App Wizard for FireMonkey to create a real Multidevice (Windows/MacOS desktop or Android/iOS mobile) application which connects to the API and retrives the data ready for you to iterate!
Head on over and download the source code from here: https://github.com/checkdigits/PositionApp_MediaStackAPI_example
RAD Studio is one of the fastest possible ways to create applications which work on Windows, Linux, macOS, iOS and Android with the MINIMUM amount of effort. Why not download a trial copy to today and see how fast you can take your ideas to market and go from dreaming to developing?