Hello everybody I am currently working in a LWC Lightning map where I want to show a list of directions saved in an Address field of an SObject. But I am having some troubles. I read the lightning-map documentation and its examples and I found that the mapMarkers list has the next structure:
mapMarkers = [
{
location: {
Street: '1000 5th Ave',
City: 'New York',
State: 'NY',
PostalCode: '349078'
},
title: 'Museum of Fine Arts',
},
];
The data location is store in an Addres field so I made an Apex controller to bring the records and its relevant information:
@AuraEnabled (cacheable=true)
public static List<WrapperDirection> getListAddressData(){
List<SObject> comunidadesList = new List<SObject>();
List<WrapperDireccion> wrapperListToReturn = new List<WrapperDireccion>();
recordslist = [SELECT Id, QX_Direction__c, Name FROM SObject];
for(SObject irecord :recordslist){
WrapperDireccion wrapper = new WrapperDireccion();
Address addr = iComunidad.QX_Direction__c;
wrapper.city = addr.getCity();
wrapper.street = addr.getStreet();
wrapper.country = addr.getCountry();
wrapper.postalCode = addr.getPostalCode();
wrapper.name = irecord.Name;
wrapperListToReturn.add(wrapper);
}
System.debug('List array --> '+ wrapperListToReturn);
return wrapperListToReturn;
}
public class WrapperDireccion{
@AuraEnabled public String name{get;set;}
@AuraEnabled public String city{get;set;}
@AuraEnabled public String country{get;set;}
@AuraEnabled public String postalCode{get;set;}
@AuraEnabled public String street{get;set;}
}
Which brings me the data I need
In my .js I have the next code:
directionsListWired = [];
mapMarkers = [];
@wire(getDireccionesComunidadesEnergeticas)
wiredRelatedRecord(result){
const{data,error} = result;
if(data){
this.directionsListWired = data;
console.log('here...');
fillMapMarkersStructure();
}else if (error) {
this.isLoading =false;
this.records = undefined;
this.error = error;
}
}
fillMapMarkersStructure(){
console.log('Entro2');
this.mapMarkers = this.directionsListWired.map(direction => { return {
location: {
Street: direction.street,
City: direction.city,
PostalCode: direction.postalCode,
State: direction.country
},
title: direction.name
}
})
console.log('mapMarkers --> '+ this.mapMarkers);
}
with the fillMapMarkersStructure function I want to get the wrapper data to make the salesforce structure:
mapMarkers = [
{
location: {
Street: '1000 5th Ave',
City: 'New York',
State: 'NY',
PostalCode: '349078'
},
title: 'Museum of Fine Arts',
},
];
My .html:
<template>
<lightning-map
map-markers={mapMarkers}
markers-title='Comunidades energéticas'
list-view='visible'
zoom-level='15'>
</lightning-map>
But I am having problems with this. Could anybody help me? Thanks
directionsListWired.forEach(element => { //get the street, city, country, postalCode, title });?