lightning:map – Showing multiple markers in Map in Salesforce Lightning Component?

lightning:map – Showing multiple markers in Map in Salesforce Lightning Component?

Sample Code:


Lightning Component:


  1. <aura:component implements=“force:appHostable” controller=“AccountListController” >  
  2.       
  3.     <aura:attribute name=“markers” type=“Object[]”/>  
  4.     <aura:attribute type=“Account[]” name=“acctList”/>  
  5.     <aura:attribute name=“mycolumns” type=“List”/>  
  6.       
  7.     <aura:handler name=“init” value=“{! this }” action=“{! c.fetchAddr }”/>  
  8.       
  9.     <div class=“slds-grid slds-grid_vertical”>  
  10.           
  11.         <div class=“slds-col”>  
  12.               
  13.             <lightning:datatable data=“{! v.acctList }”   
  14.                                  columns=“{! v.mycolumns }”   
  15.                                  keyField=“id”  
  16.                                  hideCheckboxColumn=“true”/>  
  17.               
  18.         </div>  
  19.           
  20.         <div class=“slds-col”>  
  21.              
  22.             <lightning:map mapMarkers=“{! v.markers }”/>  
  23.               
  24.         </div>  
  25.           
  26.     </div>  
  27.                      
  28. </aura:component>  



Lightning Component Controller:


  1. ({  
  2.       
  3.     fetchAddr : function(component, event, helper) {  
  4.           
  5.         component.set(‘v.mycolumns’, [  
  6.             {label: ‘Account Name’, fieldName: ‘Name’, type: ‘text’},  
  7.             {label: ‘Billing State’, fieldName: ‘BillingState’, type: ‘text’},  
  8.             {label: ‘Billing City’, fieldName: ‘BillingCity’, type: ‘Text’}  
  9.         ]);  
  10.           
  11.         var action = component.get(“c.fetchAddresses”);  
  12.         action.setCallback(this, function(response) {  
  13.               
  14.             var state = response.getState();  
  15.             if ( state === “SUCCESS” ) {  
  16.                   
  17.                 component.set( “v.acctList”, response.getReturnValue().listAcct );  
  18.                 component.set( “v.markers”, response.getReturnValue().listAddrWrap );  
  19.                   
  20.             }  
  21.               
  22.         });  
  23.         $A.enqueueAction( action );  
  24.           
  25.     }  
  26.       
  27. })  



Apex Class:


  1. public class AccountListController {  
  2.       
  3.     @AuraEnabled  
  4.     public static Wrapper fetchAddresses() {  
  5.           
  6.         List < CompleteAddressWrapper > listAddrWrap = new List < CompleteAddressWrapper >();  
  7.         List < Account > listAcct = new List < Account >();  
  8.         Wrapper wrap = new Wrapper();  
  9.            
  10.         for ( Account acc: [ SELECT Id, Name, BillingStreet, BillingState, BillingCountry, BillingPostalCode,  
  11.                                     BillingCity, BillingLatitude, BillingLongitude  
  12.                                FROM Account   
  13.                               WHERE BillingStreet != null   
  14.                               LIMIT 100 ] ) {  
  15.               
  16.             AddressWrapper addrWrap = new AddressWrapper();       
  17.             CompleteAddressWrapper objWrap = new CompleteAddressWrapper();  
  18.             addrWrap.Street = acc.BillingStreet;  
  19.             addrWrap.City = acc.BillingCity;  
  20.             addrWrap.State = acc.BillingState;  
  21.             addrWrap.Country = acc.BillingCountry;  
  22.             addrWrap.PostalCode = acc.BillingPostalCode;  
  23.             objWrap.location = addrWrap;  
  24.             objWrap.icon = ‘custom:custom32’;  
  25.             objWrap.title = acc.Name;  
  26.             listAddrWrap.add(objWrap);  
  27.             listAcct.add(acc);  
  28.               
  29.         }   
  30.           
  31.         wrap.listAcct = listAcct;  
  32.         wrap.listAddrWrap = listAddrWrap;  
  33.         system.debug(‘Out ‘ + listAddrWrap);  
  34.         return wrap;  
  35.           
  36.     }  
  37.       
  38.     public class Wrapper {  
  39.           
  40.         @AuraEnabled  
  41.         public List < Account > listAcct;  
  42.         @AuraEnabled  
  43.         public List < CompleteAddressWrapper > listAddrWrap;  
  44.           
  45.     }  
  46.       
  47.     public class AddressWrapper {  
  48.       
  49.         @AuraEnabled   
  50.         public String Street;  
  51.         @AuraEnabled   
  52.         public String City;  
  53.         @AuraEnabled   
  54.         public String State;  
  55.         @AuraEnabled   
  56.         public String Country;  
  57.         @AuraEnabled   
  58.         public String PostalCode;  
  59.           
  60.     }  
  61.       
  62.     public class CompleteAddressWrapper {  
  63.           
  64.         @AuraEnabled   
  65.         public String title;  
  66.         @AuraEnabled   
  67.         public String icon;  
  68.         @AuraEnabled   
  69.         public AddressWrapper location;  
  70.           
  71.     }  
  72.       
  73. }  



Output:



Leave a Reply