Newer Version Available
Location Class
Namespace
Usage
Each of these methods is also equivalent to a read-only property. For each getter method you can access the property using dot notation. For example, myLocation.getLatitude() is equivalent to myLocation.latitude.
You can’t use dot notation to access compound fields’ subfields directly on the parent field. Instead, assign the parent field to a variable of type Location, and then access its components.
1Location loc = myAccount.MyLocation__c;
2Double lat = loc.latitude;Example
1// Select and access the Location field. MyLocation__c is the name of a geolocation field on Account.
2Account[] records = [SELECT id, MyLocation__c FROM Account LIMIT 10];
3for(Account acct : records) {
4 Location loc = acct.MyLocation__c;
5 Double lat = loc.latitude;
6 Double lon = loc.longitude;
7}
8
9// Instantiate new Location objects and compute the distance between them in different ways.
10Location loc1 = Location.newInstance(28.635308,77.22496);
11Location loc2 = Location.newInstance(37.7749295,-122.4194155);
12Double dist = Location.getDistance(loc1, loc2, 'mi');
13Double dist2 = loc1.getDistance(loc2, 'mi');Location Methods
The following are methods for Location.
getDistance(toLocation, unit)
Calculates the distance between this location and the specified
location, using an approximation of the haversine formula and the specified
unit.
Signature
public Double getDistance(Location toLocation, String unit)
Parameters
Return Value
Type: Double
getDistance(firstLocation, secondLocation, unit)
Calculates the distance between the two specified locations, using
an approximation of the haversine formula and the specified unit.
Signature
public static Double getDistance(Location firstLocation, Location secondLocation, String unit)
Parameters
Return Value
Type: Double