Skip to content

Instantly share code, notes, and snippets.

@choudharymanish8585
Created September 18, 2018 15:13
Show Gist options
  • Save choudharymanish8585/3f2ddfb60a36f640e43bf10b2b7c48e4 to your computer and use it in GitHub Desktop.
Save choudharymanish8585/3f2ddfb60a36f640e43bf10b2b7c48e4 to your computer and use it in GitHub Desktop.
public class LightningMapDemoController {
@AuraEnabled(cacheable=true)
public static List<CarWrapper> getAllCars(){
List<Car__c> cars = [SELECT Id, Name, Available_For_Rent__c, Build_Year__c,
Mileage__c, Per_Day_Rent__c,
(SELECT Id, Name, City__c,Coordinates__Latitude__s, Coordinates__Longitude__s,
Country__c, Pin_Code__c, State__c, Street__c FROM Address__r)
FROM Car__c Limit 100];
List<CarWrapper> result = new List<CarWrapper>();
//process the result in a wrapper format
//which would be easy to pass to map attriutes in lightning component
for(Car__c car : cars){
//Creating a carWrapper object
CarWrapper carWrap = new CarWrapper();
carWrap.Name = car.Name;
carWrap.AvailableForRent = car.Available_For_Rent__c;
carWrap.BuildYear = car.Build_Year__c;
carWrap.Mileage = car.Mileage__c;
carWrap.PerDayRent = car.Per_Day_Rent__c;
List<CarAddressWrapper> carAddressList = new List<CarAddressWrapper>();
for(Address__c add : car.Address__r){
CarAddressWrapper carAddress = new CarAddressWrapper();
carAddress.icon = 'custom:custom31';
carAddress.title = car.Name;
//Creating a AddressWrapper object
AddressWrapper addWrap = new AddressWrapper();
addWrap.Latitude = add.Coordinates__Latitude__s;
addWrap.Longitude = add.Coordinates__Longitude__s;
addWrap.Street = add.Street__c;
addWrap.City = add.City__c;
addWrap.State = add.State__c;
addWrap.Country = add.Country__c;
addWrap.PostalCode = add.Pin_Code__c;
carAddress.location = addWrap;
carAddressList.add(carAddress);
}
carWrap.carAddressList = carAddressList;
result.add(carWrap);
}
return result;
}
/**
* Main wrapper class which will hold car properties
* along with a list of CarAddressWrapper class which will
* hold multiple location of a car
* */
public class CarWrapper{
@AuraEnabled public String Name;
@AuraEnabled public Boolean AvailableForRent;
@AuraEnabled public Decimal Mileage;
@AuraEnabled public Decimal BuildYear;
@AuraEnabled public Decimal PerDayRent;
@AuraEnabled public List<CarAddressWrapper> carAddressList;
}
/**
* CarAddressWrapper class which will hold icon and title of marker
* along with multiple locations
* */
public class CarAddressWrapper{
@AuraEnabled public String title;
@AuraEnabled public String icon;
@AuraEnabled public AddressWrapper location;
}
/**
* AddressWrapper class to hold address properties
* */
public class AddressWrapper{
@AuraEnabled public String Street;
@AuraEnabled public String City;
@AuraEnabled public String State;
@AuraEnabled public String Country;
@AuraEnabled public String PostalCode;
@AuraEnabled public Decimal Latitude;
@AuraEnabled public Decimal Longitude;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment