Reverse Geocoding in Android
Reverse-Geocoding is a process used to convert coordinates (latitude and longitude) to human-readable addresses. This is not exactly the opposite of Geocoding. In Geocoding, the Place is associated with a name and fixed coordinates. These coordinates are Double in nature. Negligible change in these coordinates may still refer to the same place, but we shall never get the place name as it is associated with only those fixed coordinates. Therefore, we shall definitely get the complete address in reverse geocoding, but the place name is not guaranteed. Through this article, we will show you an example of how to perform reverse-geocoding in Android. But before moving ahead, please refer to the below articles:
- Google Cloud Platform – Creating Google Cloud Console Account & Projects
- Generating API Keys For Using Any Google APIs
- How to Hide API and Secret Keys in Android Studio?
- How to Implement Google’s Places AutocompleteBar in Android?
- How to Implement Current Location Button Feature in Google Maps in Android?
For reverse-geocoding, we will need latitude and longitude in Double data type. So we will implement a Google Map and consider its center as our primary latitude and longitude. We can easily get the center coordinates using the camera position (refer to main code). Whenever the Map is dragged, the center will change and cause a change in latitude and longitude. Once the Map is idle, i.e. it stops being dragged or moving, the reverse geocoding algorithm will consider the center coordinates and process them to get a complete address. This will then be instantly posted to a textView. This will confirm that reverse-geocoding works fine in the code. Follow the below steps to create this project.
What we are going to build in this article?
Step 1: Create a New Project in Android Studio
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. We demonstrated the application in Kotlin, so make sure you select Kotlin as the primary language while creating a New Project.
Step 2: Get and hide API Key
- Our application utilizes Google’s Places API, so we need to get the Places API key from Google. To get an API key, please refer to Generating API Keys For Using Any Google APIs.
- Hiding an API key is essential and to do so, please refer to How to Hide API and Secret Keys in Android Studio?.
Step 3: Add these dependencies
This dependency will be required for reverse geocoding.
Step 4: Adding a Map fragment, a custom location marker, and a text view in the layout (activity_main.xml) file
We downloaded this image in PNG format from the internet. It has no background color and can be referred to as a transparent image. Once downloaded, you can directly copy it from wherever it is stored, open Android Studio, and paste it into the drawable folder present in the res folder. While doing this, we renamed it as “marker” which you may find in the ImageView attributes in the activity_main.xml.
Shape.xml file (Background of the Linear Layout in activity_main.xml)
We have set a white background and corner radius with some value. This is to make the layout look better.
Preview of activity_main.xml:
Step 5: Working on the backend (MainActivity.kt)
We take the coordinates of the place present at the center of the screen and convert them into text addresses. Once the screen is dragged, the center coordinates change and the address change respectively. Changes take place once the screen is idle and not in motion and that’s why we implemented our reverse-geocoding algorithm in the setOnCameraIdleListener. The below code is quite easy to understand. We have provided some comments to help you understand better.
Observe, the address changes with a change in lat-long values. Lat-long values change when the user drags the screen, as the center keeps changing.
Please Login to comment...