My current location is stored in currentPosition variable in terms of latitude and longitude.

public String currentPosition{get;set;}////37.77493,-122.419416

I have enabled Geolocation codes for Account billing Address feature to get the Account address in terms of latitude and longitude(BillingLatitude,BillingLongitude)

How to Query the account records near to me in 50kms radius using BillingLatitude&BillingLongitude? Can anyone help me?

3 Answers 3


You will need this:

    ROUND(ROUND(geography::Point(CurrentPositionLatitude, CurrentPositionLongitude, 4326).STDistance(geography::Point(BillingLatitude, BillingLongitude, 4326)), 2)/1609.344, 2) < 50
  • It measures the distance in miles though Aug 11, 2017 at 8:03
  • From review: Adding a bit more details and an explanation on how this solves the problem might help. Thank you! Aug 11, 2017 at 8:05

You can use the below link, which describes solution to your requirement in a very precise way, and it can help with other things you might need as you are playing with Location.



You'll want to use the GEOLOCATION and DISTANCE filters, outlined here:

String[] parts = currentPosition.split(',');
Decimal latitude = Decimal.valueOf(parts[0]), longitude = Decimal.valueOf(parts[1]);
Account[] results = [
    SELECT Name FROM Account 
    WHERE DISTANCE(BillingAddress, GEOLOCATION(:latitude, :longitude), 'km') < 50];

I don't have any good data to test with, but this should get you started.

  • unexpected syntax: 'missing RPAREN at ',''
    – Mahesh M
    Aug 11, 2017 at 8:49
  • expecting a right parentheses, found ','
    – Mahesh M
    Aug 11, 2017 at 8:49
  • Hi, I am getting the above two errors when i am trying to execute the above query
    – Mahesh M
    Aug 11, 2017 at 8:50
  • @MaheshM Yes, there was a missing ")". I added it into the code above.
    – sfdcfox
    Aug 11, 2017 at 16:19

You must log in to answer this question.

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