Here is a snippet of my code that gives me accounts based on the distance to my "current" account:

public String currentPosition { 
    get {
        if (String.isBlank(currentPosition)) {
            currentPosition = currentAccount.Geolocation__c; 
        return currentPosition;

public PageReference findNearby() {

    String lat, lon;       

    List<String> getcurrentId = currentId.split(':', 0);

    List<String> latlon = currentPosition.split(',');
    lat = latlon[0].trim();
    lon = latlon[1].trim();

    Decimal dlat = decimal.valueOf(lat);
    Decimal dlon = decimal.valueOf(lon);

 warehouses =  [
                           SELECT Id,ShippingState,  Name,URL_zum_CC__c,Mitarbeiternzahl_final__c,Auftragseingangstyp__c,AD_MS_Rel_Anzahl_bez__c,Vollst_ndiger_Name__c,ShippingStreet,LocateCity__longitude__s, LocateCity__latitude__s, OwnerId,GeoLocPosition__c,Accountinhaber_Text__c,WirtschaftszweigWZ08__c,WZ_Code_ebene_2__c,AE_letzte_12_Monate__c ,anzeigendaten_de_ID_account__c,indexurl__c
                           FROM Account 
                           WHERE Letzte_gew_Opp_OneSales_in_Tagen__c <= :decimal.valueof(myTime) AND DISTANCE(LocateCity__c, GEOLOCATION(:dlat,:dlon), 'km') < :decimal.valueof(myInput) AND Id != :theaccId 
                   //      ORDER BY DISTANCE(LocateCity__c, GEOLOCATION(:dlat,:dlon), 'km')          
                           LIMIT :recordLimit

However what I want in addition is the actual distance as a value. I want to store that value in a variable to display it in my visualforce page. (inside a table)

The result should be something like this:

enter image description here

  • Hello, not sure if this really helps me out. I am not trying to compare fields on the same object - so no formula field for me. The rest looks pretty complicated so not sure how to adapt that to my problem. – Jan Tonnet Apr 17 '20 at 15:18

after some trial and error I achieved what I wanted. The code from https://developer.salesforce.com/forums/?id=9060G000000I5KyQAK did help in my case.

1) Declare a new Map

public Map<String, String> distances{get;set;}

2) re-use it here

public FindNearbyController(ApexPages.StandardController stdController) {

    currentAccount  = [SELECT Id,Name,LocateCity__c,LocateCity__Latitude__s,LocateCity__Longitude__s,ShippingState,MetaData_Distance_Customer__c,Blacklisted__c,MetaData_Last_Deal_in_days__c,WZ_Buchstabe__c,WZ_Abteilung__c,WirtschaftszweigWZ08__c, WZ_Code_ebene_2__c,Mitarbeiternzahl_final__c,AD_MS_Rel_Anzahl_bez__c,GeoLocPosition__c,Geolocation__c,Accountinhaber_Text__c,Auftragseingangstyp__c,ShippingStreet,ShippingCity,ShippingPostalCode,URL_zum_CC__c FROM Account WHERE ID =: stdController.getID()];
    this.distances = new Map<String, String>();


3) fill the map

for (Account war: warehouses) {

    Location loc1 = Location.newInstance(currentAccount.LocateCity__Latitude__s,currentAccount.LocateCity__Longitude__s);
    Location loc2 = Location.newInstance(war.LocateCity__Latitude__s,war.LocateCity__Longitude__s);

        Integer distance = (Integer) loc1.getDistance(loc2, 'km');
        String km = distance+' km';

        this.distances.put(war.ID, km);

4) show it in the visualforce page:

<!-- distance --> 
                    <apex:outputPanel layout="block">
                        <apex:outputText value="{!distances[war.id]}"/>

Not the answer you're looking for? Browse other questions tagged or ask your own question.