[SalesForce] Using Current Location to get nearby addresses

I have a function in my VisualForce page that gets my current location and stores it in a variable called valueLat and valueLong. Is there anyway that I can reference these variables in my controller to use them in my query in the GEOLOCATION area.

I get an unexpected token 'Valuelat' Error

VisualForce

<script>
    function getLocation() {
        if (navigator.geolocation){
            navigator.geolocation.getCurrentPosition(showPosition);
        }
        else{
            alert ("Error");
        }
    }
        function showPosition(position) {
            findMe(position.coords.latitude,position.coords.longitude);
        }

    </script>
<apex:form>
    <span style="cursor: pointer;" onclick="getLocation()">Find Nearby</span>
    <br/><br/>
    <apex:actionFunction name="findMe" action="{!iFoundYou}" rerender="coordinates, map">
        <apex:param name="lat" value="" assignTo="{!valueLat}"/>
        <apex:param name="long" value="" assignTo="{!valueLong}" />
    </apex:actionFunction>
    <apex:outputPanel id="coordinates">
        <apex:outputText value="Latitude: {!valueLat}" /><br/>
        <apex:outputText value="Longitude: {!valueLong}" /><br />
    </apex:outputPanel>
</apex:form>

Controller

   public class FindNearbyMeCtrl {
    public String valueLong {get; set;}
    public String valueLat {get; set;}
    public List<Account> addresses {get; set;}
    public List<Account> nearby {get; set;}
    //public Decimal valueLat;
    //public Decimal valueLong;


    public PageReference iFoundYou(){
        return null;
    }

    public FindNearbyMeCtrl(ApexPages.StandardController stdController){

        addresses = [SELECT Name, BillingStreet, BillingCity, BillingState, BillingPostalCode, BillingCountry, Phone
                     FROM Account];

        nearby = [SELECT Id, Name, BillingAddress
                  FROM Account
                  WHERE DISTANCE(BillingAddress, GEOLOCATION(valueLat,valueLong), 'mi') < 20 
                  ORDER BY DISTANCE(BillingAddress, GEOLOCATION(valueLat,valueLong), 'mi')
                  LIMIT 10];

    }

}

Best Answer

In your SOQL query, make sure to use bind expression for the valueLat and valueLong variables. GEOLOCATION is a "function" but not an Apex function, it's a SOQL function.

Try this instead, note the : preceding valueLat and valueLong variables:

nearby = [SELECT Id, Name, BillingAddress
        FROM Account
        WHERE DISTANCE(BillingAddress, GEOLOCATION( :valueLat, :valueLong), 'mi') < 20 
        ORDER BY DISTANCE(BillingAddress, GEOLOCATION( :valueLat, :valueLong), 'mi')
        LIMIT 10];

Here is a trivial full example for reference:

Visualforce Page

<apex:page controller="GeolocationController">

    <apex:pageBlock >

        <apex:pageBlockTable var="acct" value="{!nearbyAccounts}">
            <apex:column value="{!acct.id}"/>
            <apex:column value="{!acct.name}"/>
            <apex:column value="{!acct.billingStreet}"/>
            <apex:column value="{!acct.billingCity}"/>
            <apex:column value="{!acct.billingState}"/>
        </apex:pageBlockTable>

    </apex:pageBlock>

</apex:page>

Apex Controller

public class GeolocationController {

    public Decimal latitude { get; set; }

    public Decimal longitude { get; set; }

    public GeolocationController() {
        this.latitude = 38.9572751;
        this.longitude = -95.2528444;
    }

    public List<Account> getNearbyAccounts() {
        return [
            SELECT
                id, name, billingStreet, billingCity, billingState
            FROM
                Account
            WHERE
                DISTANCE( BillingAddress, GEOLOCATION( :latitude, :longitude ), 'mi' ) < 10
            ORDER BY
                DISTANCE( BillingAddress, GEOLOCATION( :latitude, :longitude ), 'mi' )
        ];
    }

}
Related Topic