20180126 UPDATE!! I got some new and very useful information about maps. Please, go to Latest Update section if you have already read this entry
A couple of years ago, I wrote a post about how to visualise maps and locations on Visualforce pages, and without having that in mind, Winter ’19 helped me to deliver a second part of that one. Now, Salesforce has released a new Map Lightning Component, so I will give it a try before moving to Spring ’19 version.
You will also find a similar example on Winter ’19 maintenance exam challenge. My code is much simpler than the maintenance one, so maybe it is a good start point before making it more complex.
In a similar way as the old post, I need an object with the information to show. In my case, Doctor object with a record about my Dentist, including a Geolocation field, the Location__c one.

Having that, I need to write some code in order to deliver a Lightning Component that call the standard Map component.
I will start with the Controller in Apex in order to retrieve the information from the database. Remember that Geolocation fields is a compound field with 2 pieces, Latitude__s and Longitude__s.
public with sharing class DoctorMapController
{
@AuraEnabled
public static List<Doctor__c> getDoctors()
{
List<Doctor__c> doctors = [Select Id,
Name,
Address__c,
Email__c,
Phone__c,
SurgeryName__c,
Location__Latitude__s,
Location__Longitude__s
FROM Doctor__c];
return doctors;
}
}
We will continue with the component.
First of all, we implement usual interfaces in order to be able to visualise the component on different areas/views of our environment.
Secondly, I will add the tag <lightning:map> and its properties in order to show map information. The most important is mapMarkers. It is an array of markers that will help us to set the usual “pointer globe” icon that google maps provides. After that, we can add others like markerTitle or zoomLevel but they are not required, they just add more value to the component and the visualisation itself.
So having to provide information to mapMarkers and others, we need attributes in order to retrieve the data from our component Controller. That’s why we have 2 <aura:attribute> They are like variables on an apex class. Define the type and name of the variables that you will use among the component to get and set the data you need.
Finally <aura:handler> that will execute the action that will initialise the component via the javascript Controller. It is like the start point.
<aura:component implements="force:appHostable,
flexipage:availableForAllPageTypes,
flexipage:availableForRecordHome"
controller="DoctorMapController"
access="global" >
<aura:attribute name="mapMarkers" type="Object" access="PRIVATE" />
<aura:attribute name="markersTitle" type="String" access="PRIVATE" />
<aura:handler name="init" value="{!this}" action="{!c.init}"/>
<aura:if isTrue="{!!empty(v.mapMarkers)}" >
<lightning:map
mapMarkers="{!v.mapMarkers}"
markersTitle="{!v.markersTitle}"
zoomLevel="5"
/>
</aura:if>
</aura:component>
And the init, points to here, the javascript Controller. It depends on the way you develop. Actually you can add all method logics to here, but it is a good practice to have it on the Helper, and on the Controller, just a call to there.
({
init : function(component, event, helper) {
helper.initHelper(component, event, helper);
}
})
Finally a Helper where we will find main methods. If you are familizarize with Javascript, it will not be very difficult to understand.
Basically the variable action, access our Apex Controller getDoctors method in order to retrieve all Doctor__c records.
With this information, we can iterate over there and show all doctors on our map. But my case is much simpler, I get the first one, and populate another variable with its information. Finally I will set the values to the component attributes, markTitle and mapMarkers.
But let me focus for a second on the variable that is assigned to mapMarkers attribute. The variable markers is an array because, as I mentioned at the beginning, mapMarkers parameter is an array with locations. But what do I need to specify there? The main one is Location that could be populated with the Geolocation field data or with a direction itself. After that we only have some more descriptive data like the title or the description. They add value to the map but the required one is Location.
({
initHelper : function(component, event, helper) {
helper.defineMarkers(component, event, helper);
},
defineMarkers : function(component, event, helper) {
let action = component.get("c.getDoctors");
action.setCallback(this, function(response) {
const data = response.getReturnValue();
const dataSize = data.length;
let markers = [];
//I will only show my single record
const dentist = data[0];
markers.push({
'location': {
'Latitude':dentist.Location__Latitude__s,
'Longitude':dentist.Location__Longitude__s
},
'title' : dentist.SurgeryName__c,
'description' :
dentist.SurgeryName__c +
' dentist Location at ' +
dentist.Address__c
});
component.set('v.markersTitle',
'Out and About Communications Dentist
Locations');
component.set('v.mapMarkers', markers);
});
$A.enqueueAction(action);
}
})
And here you can find the result, a map with the “Moyua Square” with a pointer to my dentist place.

Reading this entry you can realise that using <lightning:map> is very simple and that actually, I focused on explain how to build the component itself, but if you would like to know more or create more complex pieces of code, take a look at the documentation and do some searches on internet. I’m sure people share their experiences with the new <lightning:map>.
Latest Update:
After reading this entry, my friend Alba Rivas told me she was doing some researches on maps too, and found out some useful information that shared with me:
If an address is specified for a marker, instead of its latitude and longitude, the component makes API requests to the google geolocation, and per Google documentation, it seems it has a cost. Also I would like to know if there is an event that I can listen when a marker is selected.
The API key is from Salesforce, and there are no current limitations on its use. We are going to add a click handler for selecting a marker in an upcoming release
Alba Rivas question and Salesforce response