How to calculate distance between two records

I need to calculate distance between Lead and Account, but I get this error:

Method does not exist or incorrect signature: void GEOLOCATION(Decimal, Decimal)

My code:

``````for(Lead ld:[Select Id,City,State,SIC_Code_1__c,Latitude,Longitude from Lead where Id=:CurrentId ])
{
Latitudes=ld.Latitude;
Longtitudes=ld.Longitude;
}
if(Siccodes !=null && !Siccodes.isEmpty()){
Temporary.clear();
for (Opportunity opp:[Select Id,Name,StageName,Account.name,Number_of_Units__c,account.SIC_Code__c,Type,
Billing_City__c,Billing_State_Province__c,account.SIC_Industry_Name__c,
account.No_of_active_units__c,account.BillingLatitude,account.BillingLongitude from opportunity where account.SIC_Code__c
LIKE :codes and StageName='Closed Won' ORDER BY account.No_of_active_units__c Desc NULLS LAST Limit 10])
{
if(opp.account.SIC_Code__c <> null)
{
Total = DISTANCE(GEOLOCATION(opp.account.BillingLatitude,opp.account.BillingLongitude),GEOLOCATION(Latitudes,Longtitudes), 'mi');
}

}
}
``````

`DISTANCE & GEOLOCATION` are SOQL Methods, you can't use them in apex.

Thanks for your comment kurunve, so as documented here, this should work in Apex:

``````Location loc1 = Location.newInstance(28.635308,77.22496);
Location loc2 = Location.newInstance(37.7749295,-122.4194155);
Double dist = Location.getDistance(loc1, loc2, 'mi');
``````
• you can add that there is Schema.Location class, that allow to do such a things in apex developer.salesforce.com/docs/atlas.en-us.apexcode.meta/… – kurunve Oct 24 '17 at 15:37
• Is there any possibility to use default Latitude and Longitude fields in apex to retrive distance.Because i have already billing location address & lead address .So, i need to provide distance between them – Pavan tej Oct 24 '17 at 15:40
• what do you mean by "default"? You are already using the standard fields like account.BillingLatitude – Basti Oct 24 '17 at 15:42
• @Basti why i am getting null if i try to query Location loc1 = Location.newInstance(Latitudes,Longtitudes); System.debug('Data in:'+loc1); Location loc2 =Location.newInstance(opp.account.BillingLatitude,opp.account.BillingLongitude); System.debug('Data in loc2:'+loc2); – Pavan tej Oct 24 '17 at 16:40
• debug all your variables, some of them might be empty? – Basti Oct 24 '17 at 16:41