Skip to main content

How to Create Mark Locations On Google Map With Salesforce Lightning Component

Hello friends today post , how to Mark Locations On Google Map With Salesforce Lightning Component...
so let us start..

Step=>1.
                   goto developer console
                                                         File=>New =>Apex Class
first create apex class to fetch location for mark

                              lightningMapController.apxc

public with sharing class lightningMapController {
    @AuraEnabled
    public static list<accountLocationWrapper> getLocation(){
        list<accountLocationWrapper> lstALW = new list<accountLocationWrapper>();
        /*Query accounts records with billing address information */
        for(account acc : [Select Name,description, BillingCountry, BillingCity, BillingPostalCode, BillingStreet, BillingState
                           From Account
                           Where BillingCountry != null
                           And BillingCity != null
                           limit 10]){
                               // first create "locationDetailWrapper" instance and set appropriate values
                               locationDetailWrapper oLocationWrap = new locationDetailWrapper();
                               oLocationWrap.Street = acc.BillingStreet;
                               oLocationWrap.PostalCode = acc.BillingPostalCode;
                               oLocationWrap.City = acc.BillingCity;
                               oLocationWrap.State = acc.BillingState;
                               oLocationWrap.Country = acc.BillingCountry;
                             
                               // create "accountLocationWrapper" instance, set values and add to final list.
                               accountLocationWrapper oWrapper = new accountLocationWrapper();
                               oWrapper.icon = 'utility:location';
                               oWrapper.title = acc.Name;
                               oWrapper.description = acc.description;
                               oWrapper.location = oLocationWrap;
                             
                               lstALW.add(oWrapper);
                           }
     
        return lstALW;
    }
    /* wrapper class to store required properties for "lightning:map" component' */
    public class accountLocationWrapper{
        @AuraEnabled public string icon{get;set;}
        @AuraEnabled public string title{get;set;}
        @AuraEnabled public string description{get;set;}
        @AuraEnabled public locationDetailWrapper location{get;set;}
    }
    /* sub wrapper class to store location details for "accountLocationWrapper" location property.*/
    public class locationDetailWrapper{
        @AuraEnabled public string Street{get;set;}
        @AuraEnabled public string PostalCode{get;set;}
        @AuraEnabled public string City{get;set;}
        @AuraEnabled public string State{get;set;}
        @AuraEnabled public string Country{get;set;}
    }
}


Step=>2.

        Create Lightning Component from File=>New=>Lightning Component

              lightningMap.cmp

<aura:component controller="lightningMapController">
    <!-- aura attributes to store Map component information -->
    <aura:attribute name="mapMarkersData" type="Object"/>
    <aura:attribute name="mapCenter" type="Object"/>
    <aura:attribute name="zoomLevel" type="Integer" default="4" />
    <aura:attribute name="markersTitle" type="String" />
    <aura:attribute name="showFooter" type="Boolean" default="true"/>
 
    <!-- init handler which will call 'doInit' fucntion on component load-->
    <aura:handler name="init" value="{! this }" action="{! c.doInit }"/>
 
    <!-- render map component only when we have at least 1 record in list.(mapMarkersData) --> 
    <aura:if isTrue="{!v.mapMarkersData.length > 0}" >
        <!-- the map component -->
        <lightning:map mapMarkers="{! v.mapMarkersData }"
                       center="{! v.mapCenter }"
                       zoomLevel="{! v.zoomLevel }"
                       markersTitle="{! v.markersTitle }"
                       showFooter="{ !v.showFooter }" />
    </aura:if>
</aura:component>

Step=>3.

                    lightningMapCotroller.js

({
    doInit: function (component, event, helper) {
        // call getLocation apex class method
        var action = component.get("c.getLocation");
        action.setCallback(this, function(response) {
            var state = response.getState();
            if (state === "SUCCESS") {
                // set mapMarkersData attribute values with 'accountLocationWrapper' data
                component.set('v.mapMarkersData',response.getReturnValue());
                // set the mapCenter location.
                component.set('v.mapCenter', {
                    location: {
                        Country: 'United States'
                    }
                });
                // set map markers title
                component.set('v.markersTitle', 'Google Office locations.');
            }
            else if (state === "INCOMPLETE") {
                // do something
            }
                else if (state === "ERROR") {
                    var errors = response.getError();
                    if (errors) {
                        if (errors[0] && errors[0].message) {
                            console.log("Error message: " +
                                        errors[0].message);
                        }
                    } else {
                        console.log("Unknown error");
                    }
                }
        });
        $A.enqueueAction(action);
    }
})

                                                    Output:



Buy me a coffeeBuy me a coffee

Comments

  1. Online Casino site ᐉ Best Online Casinos in 2021
    LuckyClub.live is a leading provider of online casino games which has developed and operated a large number of slot and live luckyclub casino games.

    ReplyDelete

Post a Comment

Popular posts from this blog

How to used slds-hide/show class for div hide and show in lightning component

Hello friends today post ,how to create a lightning component to use slds-hide/show class, very easy to use class and aplly.. so let us start ... firstly create a lightning component and there are two or three div or anything when you want to hide/show ... so see given bellow example and udes slds-hide/show class Step=>1.                   goto developer console=>File=>New=>Lightning Component                           sldshideshow.cmp <aura:component >     <aura:attribute name="First" type="string" default="slds-show"/>     <aura:attribute name="Second" type="string" default="slds-hide"/>         <center>     <div class="slds-box" style="width:300px;height:300px;margin-top:15px;">         <div class="{!v.First}">         ...

How to Create Floating Marquee

Hello frnds today post , how to create floating Marquee in Lightning Component so let us begin............. Step=>1. goto developer console                             File=>New=>Lightning Component                    FloatingMarq.cmp                  <aura:component>       <aura:handler name="init" value="{!this}" action="{!c.doInit}" />     <aura:attribute name="intervalId" type="Integer" default="0"/>       <div id="parentDIV" style="overflow:hidden">         <p style="position:relative;" id="tofloat">             <b><span style="color:red">Important Note : </span>             I am Floating (Left to Right) Imformation...</b...

Create Overlays with the New Modal Component

Hello friends, Now modal tag is available in #LWC, we can create a modal using the #LWC Standard tag. snapshot given below with code. Step-1 Create LWC component Step-2 Add below code Step-3 I am showing how my component is look like. Thanks for reading