Display Maps in LWC

 


In this blog, we will look at how to display Google Maps in your Lightning Web Components. Salesforce provides a lightning-map component to display a map of one or more locations, using geocoding data and mapping imagery from Google Maps. 

Now, let's look at how to use this component in your Lightning Web component.

HTML:


  • Pass the location(s) that you want to display in the map-marker property.
  • A marker contains:
    • Location Information: A coordinate pair of latitude and longitude, or an address composed of address elements to be geocoded.
    • Descriptive Information: Optional title, description, and an icon. These items are relevant to the marker but not specifically related to location.
Map markers have a few more properties that can be used to customize the map display, please refer to this documentation on Maps.


Display a Single Marker

Let's understand how we can pass single location info in the map-marker and display it on the map.

In this example, we will create a new Lightning Web Component to display the location of the contact on the Contact Page.

Apex Class:



JavaScript:



Output:




In the above example, I have created a lightning web component to display the location of a Contact on the standard contact page.

Now let's look at how we can display multiple addresses.


Display Multiple Addresses

You can pass multiple markers in map-marker array and the lightning-map component will render a list of tiles with location tiles and addresses, with a heading displayed above the list. Each location tile contains an icon, a tile, and an address.

Let's take a look at an example.

HTML:



  • mapMarkers property will contain a list of multiple addresses.
  • markers-title is used to provide a custom heading for the locations.

JavaScript:



Output:




Thank you for reading the blog. 

You can share your thoughts with me by dropping a message to me on LinkedIn

You can find my other blogs on my blog page - LWC - Blog


Comments

Popular posts from this blog

Calling Apex method from LWC

Navigation Service in LWC (Lightning Web Components)