+ Start a Discussion
Ola BamideleOla Bamidele 

Geolocation - Displaying distance of accounts on Map

Hi Gurus, 

I have build a Geolocation app that displays the location of an Account on a map. However to improe and take it further, I have decided to add a function that displays the "Distance" of the account to the person that is viewing the map. 

After days of research, this is how far I have come but I still cant get it work. So please if anyone knows what i am doing wrong or how to make it work, please let me know!

My Code:
public with sharing class AccountController {
    @AuraEnabled
    public static List<Account> findAll() {
    return [SELECT id, name, Location__Latitude__s, Location__Longitude__s, Industry
            FROM Account
            WHERE Location__Latitude__s != NULL AND Location__Longitude__s != NULL
            LIMIT 50];
    DISTANCE(GEOLOCATION(BillingLatitude , BillingLongitude ),
    Account_Name__r.GeoLocation__Latitude__s , Branch_Assigned__r.GeoLocation__Longitude__s )  , 
    'mi')         
 	} 
    	
						
}

Error Message (which doesnt make much sense):
Expecting ';' but was: ','

Thanks very much!
 
Steven NsubugaSteven Nsubuga
The error message totally makes sense, unlike your code. You have a single method called findAll, which is defined as returning a list of Accounts.
  • What or where is the distance supposed to be stored or displayed?
  • The very first line in the method is a return statement!! A return statement needs to be at the very end of all your method logic!!
  • This code DISTANCE(GEOLOCATION(BillingLatitude , BillingLongitude ),
        Account_Name__r.GeoLocation__Latitude__s , Branch_Assigned__r.GeoLocation__Longitude__s )  , 
        'mi')
     does not make sense. It is in the wrong place. Your method returns a list of all the Accounts in your system that have a Geolocation. Then out of the blue you call the DISTANCE function!! Which account's distamce is it supposed to calculate from the viewer?!
Steven NsubugaSteven Nsubuga
Here is my change to your code, it just prints out to the debug logs the distance between the current logged in user and each account's geolocation field. 
public with sharing class AccountController {
    @AuraEnabled
    public static List<Account> findAll() {
        
    List<Account> accounts = [SELECT id, name, Location__Latitude__s, Location__Longitude__s, Industry
            FROM Account
            WHERE Location__Latitude__s != NULL AND Location__Longitude__s != NULL
            LIMIT 50];
            
    //Assuming that there is a custom field called 'UserLocate' of type Geolocation on the user object
    User currentUser = [SELECT id, name, UserLocate__Latitude__s, UserLocate__Longitude__s FROM User where Id=:UserInfo.getUserId()];
    
    for (Account account : accounts){}
        System.debug('Distance between '+currentUser.name + ' and ' + account.Name  + '=' +
        DISTANCE(GEOLOCATION(currentUser.UserLocate__Latitude__s , currentUser.UserLocate__Longitude__s),
        GEOLOCATION(account.Location__Latitude__s , account.Location__Longitude__s), 'mi'));        
     } 
        
    return accounts;            
}


You can modify it as you deem fit.
Ola BamideleOla Bamidele
Hi Steven Nsubuga, 

Thanks for the response and sorry for causing you a coding headache :D 

I tried the code yiu suggested and there where a few error - but i think it is relation to the fact I dont have a UserLocate field. 

Also to add, the idea to have it automatically use the Geolocation of the user of the app, rather than a fixed location. Will this be achieveable?

Thanks very much for teh help!
Steven NsubugaSteven Nsubuga
Yes, it is because you do not have that UserLocate field on your User object. My assumption was that all Users of your App would have a registered Geolocation in Salesforce, hence the custom UserLocate field on the User object.
If I understand your question correctly, you want to be able to automatically detect the current location of your app user and then work out the distance to the Accounts. That is quite complex, beyond me. Hopefully someone else in the communuty can help.
Ola BamideleOla Bamidele
Hi Steven Nsubuga, 

Exactly, thats what i am trying. Totally understandable, it is very complex hint why I needed help.

nevertheless take very much for your time :)