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
andvalueLong
variables. GEOLOCATION is a "function" but not an Apex function, it's a SOQL function.Try this instead, note the
:
precedingvalueLat
andvalueLong
variables:Here is a trivial full example for reference:
Visualforce Page
Apex Controller