\n\n\n ```\n\n ---\n\n ### Step 4: Create the Aura Component Controller\n\n ``` \n// GoogleMapsIntegrationController.js\n\n({\n init: function (component, event, helper) {\n // Fetch accounts from Salesforce\n var action = component.get(\"c.getAccountsForMap\");\n action.setCallback(this, function (response) {\n var state = response.getState();\n if (state === \"SUCCESS\") {\n component.set(\"v.accounts\", response.getReturnValue());\n helper.loadGoogleMapsScript();\n } else {\n console.error('Error fetching accounts');\n }\n });\n $A.enqueueAction(action);\n },\n\n addMarkers: function (component, event, helper) {\n var accounts = component.get(\"v.accounts\");\n var map = new google.maps.Map(document.getElementById('map'));\n\n for (var i = 0; i < accounts.length; i++) {\n var account = accounts[i];\n var address = account.BillingStreet + ', ' + account.BillingCity + ', ' +\n account.BillingState + ', ' + account.BillingCountry + ' ' + account.BillingPostalCode;\n\n // Use the address to get latitude and longitude using Google Geocoding API\n // Add a marker for each account using the obtained coordinates\n {!c.addMarker}(map, address, account.Name);\n }\n },\n\n addMarker: function (map, address, title) {\n // Use the Google Geocoding API to get latitude and longitude\n // Create a marker and add it to the map\n }\n});\n\n ```\n\n ---\n\n Please note that the __addMarker__ function needs to be implemented to make a callout to the Google Geocoding API to convert the address to latitude and longitude.\n\nAdditionally, you may need to handle asynchronous loading of the Google Maps API script. The __loadGoogleMapsScript__ function in the helper should be responsible for dynamically loading the script. Ensure that you replace 'YOUR_GOOGLE_MAPS_API_KEY' with your actual API key.\n\nRemember to handle errors, implement proper error handling, and adjust the code based on your specific requirements and best practices."
Published on

Salesforce & Google Maps Integration

Authors
  • avatar
    Name
    Himanshu Varshney
    Twitter

    Senior Salesforce Developer

GoogleMapsSalesforce


Step 1: Set up Google Maps API Credentials

  1. Go to the Google Cloud Console.
  2. Create a new project or select an existing one.
  3. Enable the "Maps JavaScript API" for your project.
  4. Create API credentials (API Key).

Step 2: Create an Apex Class for Integration

// GoogleMapsIntegrationController.cls

public class GoogleMapsIntegrationController {
   public static String getGoogleMapsApiKey() {
       // Replace 'YOUR_GOOGLE_MAPS_API_KEY' with your actual API key
       return 'YOUR_GOOGLE_MAPS_API_KEY';
   }

   public static List<Account> getAccountsForMap() {
       // Query accounts or customize as per your use case
       return [SELECT Id, Name, BillingStreet, BillingCity, BillingState, BillingCountry, BillingPostalCode FROM Account LIMIT 10];
   }
}


Step 3: Create an Aura Component


<!-- GoogleMapsIntegration.cmp -->

<aura:component controller="GoogleMapsIntegrationController">
  <aura:attribute name="accounts" type="List" />

  <aura:handler name="init" value="{!this}" action="{!c.init}" />

  <div id="map"></div>

  <script>
      // Replace 'YOUR_GOOGLE_MAPS_API_KEY' with your actual API key
      const googleMapsApiKey = '{!GoogleMapsIntegrationController.getGoogleMapsApiKey()}';

      function initMap() {
          const map = new google.maps.Map(document.getElementById('map'), {
              center: { lat: -34.397, lng: 150.644 },
              zoom: 8
          });

          // Add markers for each account
          {!c.addMarkers}
      }
  </script>
</aura:component>


Step 4: Create the Aura Component Controller

// GoogleMapsIntegrationController.js

({
   init: function (component, event, helper) {
       // Fetch accounts from Salesforce
       var action = component.get("c.getAccountsForMap");
       action.setCallback(this, function (response) {
           var state = response.getState();
           if (state === "SUCCESS") {
               component.set("v.accounts", response.getReturnValue());
               helper.loadGoogleMapsScript();
           } else {
               console.error('Error fetching accounts');
           }
       });
       $A.enqueueAction(action);
   },

   addMarkers: function (component, event, helper) {
       var accounts = component.get("v.accounts");
       var map = new google.maps.Map(document.getElementById('map'));

       for (var i = 0; i < accounts.length; i++) {
           var account = accounts[i];
           var address = account.BillingStreet + ', ' + account.BillingCity + ', ' +
               account.BillingState + ', ' + account.BillingCountry + ' ' + account.BillingPostalCode;

           // Use the address to get latitude and longitude using Google Geocoding API
           // Add a marker for each account using the obtained coordinates
           {!c.addMarker}(map, address, account.Name);
       }
   },

   addMarker: function (map, address, title) {
       // Use the Google Geocoding API to get latitude and longitude
       // Create a marker and add it to the map
   }
});


Please note that the addMarker function needs to be implemented to make a callout to the Google Geocoding API to convert the address to latitude and longitude.

Additionally, you may need to handle asynchronous loading of the Google Maps API script. The loadGoogleMapsScript function in the helper should be responsible for dynamically loading the script. Ensure that you replace 'YOUR_GOOGLE_MAPS_API_KEY' with your actual API key.

Remember to handle errors, implement proper error handling, and adjust the code based on your specific requirements and best practices.