Geocoding and Reverse Geocoding in Salesforce


In this blog you will learn to use Geocoding and Reverse Geocoding service in your Salesforce Org. Geocoding is the process of taking input text, such as an address or the name of a place, and returning a latitude/longitude location on the Earth's surface for that place. Reverse Geocoding, on the other hand, converts geographic coordinates to a description of a location, usually the name of a place or an addressable location. Geocoding relies on a computer representation of address points, the street / road network, together with postal and administrative boundaries.

Please checkout the apex class below :

Geocoding
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public class Vlog_GoogleApiApex {
    @future(callout=true)
    public static void parseJSONResponse() {     
        String address='Ajmer+Rajasthan';
        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());
        
      
        system.debug('###Finish'); 
        system.debug('###Finish'); 
        system.debug('###Finish'); 
    }   
}


Reverse Geocoding
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public class Vlog_GoogleApiApex {
    @future(callout=true)
    public static void parseJSONResponse() {     
        String address='26.452304+74.610305';
        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());
        
      
        system.debug('###Finish'); 
        system.debug('###Finish'); 
        system.debug('###Finish'); 
    }   
}


For complete tutorial and output checkout below video :


 If you have any question please leave a comment below.
If you would like to add something to this post please leave a comment below.
Share this blog with your friends if you find it helpful somehow !

Thanks
Keep Coding 



Post a Comment

0 Comments