In today’s world where everything is a connected smart device, there is location data all around us. Reverse geocoding is the process of converting a GPS coordinate with latitude and longitude values to an address that is recognizable to the human eye. The viewer will then be able to identify the correct street, landmark, zip code, or country of that particular GPS location. There are a host of public reverse geocoding services available through API and web services. These services require an input coordinate, usually acquired from localization tools like an in-built GPS, cellphone towers, and Wi-Fi traces or even by selecting a point in the map to look up the address.
Google Geocoding API is one such popular Reverse Geocoding API that is commonly used. It is a RESTful service and can be used in C# to reverse geocode a GPA location.
Prerequisites
-
Visual Studio
-
Google Developer Account
Getting Started
Adding Google reverse geocoding API to the .NET project involves 2 processes:
Setting up Google project
For using google geocoding API we need an API Key with geocoding API enabled. Setting up a Google API key for reverse geocoding includes the following process
- The next step is to enable the Geocoding API. For that select, the newly created project and go to Library → Maps → Geocoding API, and click Enable
- After enabling now, we can create an API key for our usage. For this, in the Developer Console go to Credentials → Click Create Credentials → Select API Key. Google will generate a new API
key.
Integrating Geocoding API to .NET project for reverse geocoding:
Google reverse geocoding can be done by calling the API URL with geographic coordinates and API keys.
https://maps.googleapis.com/maps/api/geocode/json?latlng=,&key=
The below are optional parameters that you can include in a reverse Geocoding request:
-
language — The language in which to return results.
-
result_type — A filter of one or more address types, separated by a pipe (|)
-
location_type — A filter of one or more location types, separated by a pipe (|).
The API will return a list of nearby addresses to the coordinate. You can find the description of the fields in the Geocoding responses here. In this section, we will use a Console application to show how to integrate Reverse geocoding to C#.
Step 1
Create a new Console application project and install Newtonsoft.Json from NuGet.
Step 2
Creating output classes which will be used for deserializing the JSON response obtained from Geocoding API. This should be based on the resulting structure of the geocoding API.
namespace Zerone.GoogleReverseGeocoding
{
public class GoogleGeoCodeResponse
{
public string status { get; set; }
public results[] results { get; set; }
}
public class results
{
public string formatted_address { get; set; }
public geometry geometry { get; set; }
public string[] types { get; set; }
public address_component[] address_components { get; set; }
}
public class geometry
{
public string location_type { get; set; }
public location location { get; set; }
}
public class location
{
public string lat { get; set; }
public string lng { get; set; }
}
public class address_component
{
public string long_name { get; set; }
public string short_name { get; set; }
public string[] types { get; set; }
}
}
Step 3 – Reverse geocoding
After creating the output classes, we can now call the API using the key and coordinate. The API result will be in Json format which can be deserialized using Newton soft.
The below is the result of reverse geocoding the coordinates 40.714224, -73.961452
The reverse geocoding will return the nearby address to the coordinates we have provided with the nearest one first. Here we are only printing the formatted address. You can manipulate the results as your need as the result contains each component along with the formatted address.
Conclusion
Reverse geocoding is a critical component of location-based services with a wide range of application areas. Some of the more common uses are in fleet management, IoT sensors, photo and video enrichment, delivery tracking, and so on. Organizations worldwide have started to leverage reverse geocoding to improve their market strategies. However, reverse geocoding has also given rise to some serious ethical and privacy concerns while the precise location is being revealed. Though it has a host of advantages it will be wise to use this technology with prudence, especially when using it in an organizational setup.