I have two location fields of type Geolocation named point1 and point2. I have one radius field . i need to determine whether point2 is located within radius distance from point1 or not. How can this be determined using apex without using SOQL ?


2 Answers 2


this solved my problem :

Location loc1 = Location.newInstance(28.635308,77.22496);
Location loc2 = Location.newInstance(37.7749295,-122.4194155);
Double dist = Location.getDistance(loc1, loc2, 'mi');
Double dist2 = loc1.getDistance(loc2, 'mi');

Edit: it seems there is a Location class in apex that does this already. You should use it instead of rolling your own!

The Haversine formula is what seems to be the generally accepted way to do this. It's not native to apex that I can tell. It's been implemented in java before and seems portable to apex with a bit of work.

There's no Math.toRadians in apex, but I believe you can implement this as

private static toRadians(double degree){
    return degree * (Math.PI/180.0);

You must log in to answer this question.

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