How to: Reverse Geocode with an API and App Inventor

How to: Reverse Geocode with an API and App Inventor

App Inventor can reverse geocode using your the Location Sensor and your geocoordinates but it cannot reverse geocode using arbitrary coordinates. This tutorial describes a way to use an API to reverse geocode a geocoordinate latitude / longitude pair. The api has lots of free usage; api usage fees are moderate for heavy users.

Reverse Geocoding
Reverse geocoding is identifying latitude and longitude coordinates and 'converting' them into location information. The has a little known accessory to convert coordinates "at the level of city names, areas and districts, countries and states". Google's Geocoding API Overview  |  Geocoding API  |  Google Developers can obtain a street address (if CurrentAddress is available in the database) through reverse geocoding. This Geocoding api does not return address information such as street address.

Read more about the api at this link to the openweathermap Geocoding API Geocoding API - OpenWeatherMap . The tutorial shows a way to harness the power of an api in App Inventor which can identify a city or town as the location in many cases but not a street address.

The Geocoding API provides Direct geocoding (provides geocoordinates from a provided location name (city for example); Reverse geocoding; and Coordinates by zip/post code. Only reverse geocoding using the api is explained in this tutorial. This is an advanced tutorial; the Blocks are not totally annotated.

The example app reverse geocodes custom coordinates using either a Button (for custom hard coded coordinates) or touching a MIT Map control map to select a location you want to reverse geocode.

Information fields in the API response vary based on the country in which you select a location. The api provides:

  • name Name of the found location. Perhaps a city but not a street address.
  • local_names.
    local_names.[language code] Name of the found location in different languages. What displays depends on the location etc, and how you set a 'local' language flag.
  • lat Latitude of the found location
  • lon Longitude of the found location
  • country Country of the found location
  • state State/Province of the found location (where available).

Language Codes for Local Names
The api uses language codes to define local place names. These codes need to be 'interpreted to provide the full country names. The codes are parsed using a ListOfPairs Block. The api provides local names that may be available in several languages for some locations and geographic areas. The app allows you to chose a language in addition to English for some place names.

Country information in the api uses ISO Alpha-2 two character country identification. The characters are converted from the two letter 'code' with a List of countries and abbreviations . The country list is traversed until the code provided by the api is found to resolve the full country name (the search for a specific two letter code terminates when an abbreviation is found and the break Block terminates the search). The user can request this information be captured and displayed using a ListPicker.

Country Abbreviation List
The country listing was created from a csv file embedded directly into a Text box. A copy of the csv (Country3.csv) is 'stored' in Media. It provides a country abbreviation / country table. The csv file was copied using Notepad. From Notepad, the text of the file was dragged and pasted into the Text Block attached to the initialize global countryCSV to Block.

On Initialization the country abbreviation Text information is converted into List ( abbreviation, country). The List is used to create a Dictionary.

This api requires a free API key to work. Obtain your free key from OpenWeatherMap

How good is the resulting reverse geocoded information?
It is reasonable; sometimes precise and sometimes vague. Results vary depending on where your searched location is and the Map Zoom setting (which determines how precise your touch elicits a latitude and longitude).

The api response to a Get Block query based on geocoordinates (latitude and longitude) is parsed with a Dictionary to realize the api field information. Be aware, the plotted Marker location is not the exact latitude/longitude determined from a Map press. The plotted position is the nearest location to what was pressed in the api's database. :cry: The api location information is NOT precise but perhaps more than adequate to use in your app.

Download the aia.

ReverseGeocodingExample.aia (15.6 KB)

Load the aia to your Designer. After the app appears in the Designer, go to the Blocks Screen and enter your api key into the Obfuscated Text Block.

The app will not run without the api key. Obtain this api key from openweathermap as described earlier. Now you can test the app using live development with Companion or compile the app.

Protect your api key.
The Obfuscated Text Block provides only very low security against expert code hacking. You probably do not want to share your api key with others (Usage associated with the key counts toward your free usage.

The app example was designed on a Tablet. Works on Android 8.1 and 11. The code does not on Android 4.2 or the basic emulator (the api works; the Map does not render. See image below.).

Experiment and

  • Use the app with the LocationSensor to query your present location. Code similar to the code in Button1.Click could be used in the LocationSensor.LocationChanged Block to set up the request needed to query the api from the user's present location.

  • Use similar coding techniques to code a weather app using the openweathermap weather api.

  • make the api do what you want to do. The example presently is generic. You may not wish to display all the information the api is capable of using.

If you need more precise information than provided by this api, consider the Google Geocoding api instead. Be aware of Google's terms of use.

I hope someone finds this code and api helpful,


(added to FAQ)