0

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];

    }

}
1
  • Please provide more of the controller code for action iFoundYou and the variable declarations for valueLat and valueLong. Also please share how your Visualforce page calls function findMe. Thanks
    – Doug Ayers
    Commented Jul 17, 2017 at 15:59

1 Answer 1

2

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' )
        ];
    }

}
3
  • Hey Doug, I'm still getting the following error:Invalid bind expression type of String for column of type Decimal
    – Kush Patel
    Commented Jul 17, 2017 at 16:18
  • 1
    So change those property types to Decimal? Notice you have declared these properties as public String valueLong.
    – Adrian Larson
    Commented Jul 17, 2017 at 16:30
  • @KushPatel, yes, you will need to change your data type to Decimal. I've updated my answer to include a trivial example.
    – Doug Ayers
    Commented Jul 17, 2017 at 16:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .