Skip to content

Instantly share code, notes, and snippets.

@lukethacoder
Last active May 2, 2020 06:19
Show Gist options
  • Save lukethacoder/9cf12d3ac6fabc6a27fd856a93f4074d to your computer and use it in GitHub Desktop.
Save lukethacoder/9cf12d3ac6fabc6a27fd856a93f4074d to your computer and use it in GitHub Desktop.
Apex method to check distance between to lon/lat points
List<Account> listOfAccounts = [ SELECT Id, Name, ShippingLongitude, ShippingLatitude FROM Account WHERE ShippingLatitude != NULL];
Location userLocation = Location.newInstance(-35.4330529, 149.0699176);
System.debug('a userLocation ' + userLocation);
for (Account account : listOfAccounts) {
Location placeLocation = Location.newInstance(account.ShippingLatitude, account.ShippingLongitude);
Double distanceBetween = Location.getDistance(userLocation, placeLocation, 'km');
System.debug('Distance Between ' + distanceBetween + 'km');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment