Note: This release is in preview. Features described here don’t become generally available until the latest general availability date that Salesforce announces for this release. Before then, and where features are noted as beta, pilot, or developer preview, we can’t guarantee general availability within any particular time frame or at all. Make your purchase decisions only on the basis of generally available products and features.
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 WITH USER_MODE 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)
Signature
public Double getDistance(Location toLocation, String unit)
Parameters
Return Value
Type: Double
getDistance(firstLocation, secondLocation, unit)
Signature
public static Double getDistance(Location firstLocation, Location secondLocation, String unit)
Parameters
Return Value
Type: Double