0

I have requirement like,Find the Person Accounts within X miles from the current Location. My code as follows: I am getting error on Where part. I am not sure how to include the miles variable. Pls advise.

public PageReference findNearby() {

    String lat, lon;
    String miles;
    // Format: "<latitude>,<longitude>" (must have comma, but only one comma)
    List <String> latlon = currentPosition.split(',');
    lat = latlon[0].trim();
    lon = latlon[1].trim();
    // SOQL query to get the nearest Contacts
    String queryString =
        'SELECT Id, Name, MailingStreet, MailingCity, MailingPostalCode , MailingState, Donation_Request__c, Phone, Location__longitude__s, Location__latitude__s ' +
        'FROM Contact where Location__longitude__s != null and Location__latitude__s != null ' +
        'WHERE DISTANCE(Location__c, GEOLOCATION(' + lat + ',' + lon + '), \'mi\') < :miles ' +
        'ORDER BY DISTANCE(Location__c, GEOLOCATION(' + lat + ',' + lon + '), \'mi\') ' +
        'LIMIT 10';
    System.Debug('>>>> the value of queryString is ' + queryString);
    contacts = database.query(queryString);
    if (contacts.size() <= 0) {
        System.debug('No results. Query: ' + queryString);
    }
    return null;
}

VF Markup

<apex:pageBlock title="Map" id="map">
    <apex:pageBlockSection rendered="{!resultsAvailable}" title="Locations">
        <apex:map width="590px" height="300px" center="38.9047,77.0164">
            <!-- Add markers for contacts -->
            <apex:repeat value="{!lstAccount}" var="con">
                <apex:mapMarker title="{!con.Name}" position="{latitude:{!con.Location__Latitude__s},longitude:{!con.Location__Longitud‌​e__s}}">
                    <apex:mapInfoWindow>

Another Issue is , I have already mapped some Values(coming from a Search criteria) to apex:map. Along with that , How can i incorporate the result from the above case(X miles from the current location). I couldn't paste code reg with the issue here. It pasted in the below part.

3
  • I could nt paste the follwoing code to my above question. So pasted here. Pls look over here. <apex:pageBlock title="Map" id="map"> <apex:pageBlockSection rendered="{!resultsAvailable}" title="Locations"> <apex:map width="590px" height="300px" center="38.9047,77.0164" > <!-- Add markers for contacts --> <apex:repeat value="{!lstAccount}" var="con"> <apex:mapMarker title="{!con.Name}" position="{latitude:{!con.Location__Latitude__s},longitude:{!con.Location__Longitude__s}}"> <apex:mapInfoWindow > Oct 27, 2015 at 17:00
  • Replace < :miles ' + by < '+miles+
    – abdn
    Oct 27, 2015 at 17:48
  • @abdn - That is not necessary, you can use bind variables in dynamic SOQL
    – Eric
    Oct 27, 2015 at 20:46

1 Answer 1

1

You miles property is a string and the DISTANCE returns a ?decimal so you will simply need to change the field type:

Decimal miles;

Also, you are not setting the value for miles in your code so you will need to assign it a value, per your requirements, as well

As for your second question, once you get the query part done, please ask as a new question as it is unclear what your are asking and without code there is no context.

1
  • Thanks Eric . It worked. :). And for the Second Issue, like you said ,I raised a new Question. Pls check that. Oct 28, 2015 at 6:09

You must log in to answer this question.

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