Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Salesforce by (11.9k points)

How do I integrate Salesforce with Google Maps? I'm just looking for information on how to...

  1. Search for contacts in Salesforce
  2. Plot those on a google map.

1 Answer

0 votes
by (26.7k points)
edited by

To integrate with Google Maps you need to follow these steps :

1. First of all, we need to create authorization key. To do that Visit

2. After that, click on create project , and then click new project.

3. After creating project, enable the API(Geocoding API).

4. Click on Credentials, and create a API key.

5. Copy API key, and Save it.

After that go for the code. Where you need to put your API key in variable "yourAPiKey".

public class GetMapLocation {

    static public void getLongitde_Latitude(){

        String address;

        address = 'Cognizant Technologies Solution Sector 135 Noida';

        address = EncodingUtil.urlEncode(address, 'UTF-8');

        

        // build callout

        Http h = new Http();        

        HttpRequest req = new HttpRequest();

        

String yourAPiKey = ''; // Enter your Api key which you have created in google api console

        String key = '&key=' + yourAPiKey;

        String endPoint = 'https://maps.googleapis.com/maps/api/geocode/json?address=' + address;

        req.setEndpoint(endPoint + key);

        req.setMethod('GET');

        req.setTimeout(5000);

        System.debug('Request ' + req);

        try{

            // callout

            HttpResponse res = h.send(req);

            // parse coordinates from response

            JSONParser parser = JSON.createParser(res.getBody());

            system.debug('Response get Body ' + res.getBody());

            

            double lat = null;

            double lon = null;

            //system.debug(parser.nextToken());

            while (parser.nextToken() != null) {  

                system.debug(parser.getCurrentToken());

                system.debug(JSONToken.FIELD_NAME);

                if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) &&

                    (parser.getText() == 'location')){

                       // system.debug('a');

                        parser.nextToken(); // object start                     

                        while (parser.nextToken() != JSONToken.END_OBJECT){

                            String txt = parser.getText();

                            parser.nextToken();

                            if (txt == 'lat')

                                lat = parser.getDoubleValue();

                            else if (txt == 'lng')

                                lon = parser.getDoubleValue();

                        }

                    }

            }

            // update coordinates if we get back

            system.debug(lat);

            if (lat != null){

                system.debug(lat+' '+lon);              

            }

        }

        catch (Exception e) {

            system.debug(e);

        }

    }

}

I hope this will help.

Want to become an Salesforce Expert? Join Salesforce Training now!!

Related questions

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...