Apex
Geocoding & Reverse Geocoding in Salesforce

Geocoding & Reverse Geocoding in Salesforce

Geocoding

Geocoding is the computational process by which a physical address is converted into geographic coordinates, which can be used for a variety of mapping applications.

How it Useful:

Sales and marketing professionals had challenges assigning territories and find their nearby prospects about a location. To get some useful data into the system, Salesforce partners and customers had to use number fields to store latitude and longitude pairs by having to manually type.

Salesforce unlocked a whole new way to understand their customers and run a business. Hence, they introduced the Geolocation feature in Winter 2013, which enabled their customers to identify locations by their latitude and longitude. Subsequently, this feature was further enhanced to Geocode, which converts addresses into geographic coordinates like latitude and longitude and position the map.

In summer’16, salesforce released this feature.

https://releasenotes.docs.salesforce.com/en-us/summer16/release-notes/rn_general_geocodes_aloha.htm

Where it’s Useful:

Geocoding software is implemented in a variety of industries, including but not limited to:

  • Health: assess patient access to healthcare facilities; study epidemiological patterns of a disease
  • Finance: determine lending activity in the community, including demographics data to assist in fair lending efforts
  • Public Safety: direct emergency response via locally developed street files and E911 points
  • Military: Military Grid Reference System (MGRS) is the geocoordinate standard used by NATO militaries
  • Commercial: directly factor geography into the business analytics process; monitor shipping patterns and customer sales

However, you can also get Geolocations for addresses in the records, such as Accounts, Leads and Contracts in Salesforce by making callouts to Google API.

Geocoding & Reverse Geocoding:

Geocoding is the conversion of physical human-readable addresses of places into latitude and longitude geographical coordinates. This process is known as “Geocoding,” while the reverse case (that is, converting latitude and longitude coordinates into physical addresses) is known as “Reverse Geocoding.” To clarify this explanation here is an example using the datacamp USA office address

How to do using Apex:

Geocoding:

public class Geo_apicall {
    @future(callout=true)
    public static void parseJSONResponse() {     
        String address='Kharadi+Pune+Maharastra';
        String key='AIzaSyC5Z3twwN_hX_uP1JrDnY_726nH26Zb058';
        Http httpProtocol = new Http();
        // Create HTTP request to send.
        HttpRequest request = new HttpRequest();
        // Set the endpoint URL.
        String endpoint = 'https://maps.googleapis.com/maps/api/geocode/json?address='+address+'&key='+key;
        request.setEndPoint(endpoint);
        // Set the HTTP verb to GET.
        request.setMethod('GET');
        // Send the HTTP request and get the response.
        // The response is in JSON format.
        HttpResponse response = httpProtocol.send(request);
        System.debug('####Body : '+response.getBody());
   
    }   
}

If we are using the standard edition for API Callouts to Google, then we need to use the API KEY in the End Point URL. And then it will be like:

https://maps.googleapis.com/maps/api/geocode/json?address=Street+City+State&key=YOUR_API_KEY

However, if we are using the premium version for API Callouts to Google, then we need to use Client Id as a parameter in the End Point URL. Something like:

https://maps.googleapis.com/maps/api/geocode/json?address=Street+City+State&client=YOUR_CLIENT_ID&signature=SIGNATURE

This will get the Geolocation as a response from Google API. We can then use JSON to fetch the Geolocation with two parameters: Latitude and Longitude.

Reverse Geocoding

public class ReverseGeo_apicall  {
    @future(callout=true)
    public static void parseJSONResponse() {     
        String address='18.5538+73.9477';
        String key='AIzaSyC5Z3twwN_hX_uP1JrDnY_726nH26Zb058';
        Http httpProtocol = new Http();
        // Create HTTP request to send.
        HttpRequest request = new HttpRequest();
        // Set the endpoint URL.
        String endpoint = 'https://maps.googleapis.com/maps/api/geocode/json?address='+address+'&key='+key;
        request.setEndPoint(endpoint);
        // Set the HTTP verb to GET.
        request.setMethod('GET');
        // Send the HTTP request and get the response.
        // The response is in JSON format.
        HttpResponse response = httpProtocol.send(request);
        System.debug('####Body : '+response.getBody());

    }   
}

To understand more about google maps API.

https://developers.google.com/maps/gmp-get-started


Thanks for Reading…

Leave a Reply

Your email address will not be published. Required fields are marked *