img1

Geolocation is a technology that uses data acquired from an individual’s computer or mobile device to identify or describe the user’s actual physical location using longitude and latitude values. Salesforce allows its users to accomplish this feat by giving the ability to create geolocation fields, Geolocation field includes latitude and longitude values.

Latitude and longitude Refresher

Are coordinate systems employing the position or location of any place on Earth’s surface, which can be determined and described. Latitude is a measurement on a globe or map of location north or south of the Equator. Longitude is a measurement of location east or west of the prime meridian at Greenwich, the specially designated imaginary north-south line that passes through both geographic poles and Greenwich, London.

Geolocation Field

The Geolocation field type is a native field type that can be used to encode locations with geolocation coordinates.

Query Geolocation Fields

use the following format to access Geolocation latitude and longitude values. Assume that a Geolocation field named Location was created on the Account object.

Note that the compound field type introduces a new suffix to custom fields: __s (for ‘subfield’). A compound custom geolocation field called myLocation__c will have the subfields myLocation__latitude__s and myLocation__longitude__s.

1
[SELECT Location__latitude__s, Location__longitude__s FROM Account WHERE Id ='account-id-here']

To access individual fields in APEX use the following syntax.

1
2
3
4
Account record = [SELECT Id,Location__c FROM Account WHERE Id ='account-id-here'];
Location loc = record.Location__c;
Double latitude = loc.latitude;
Double longitude = loc.longitude;

Geolocation Field Standard Validations

Geolocation fields only accept valid location pairs.

  • Latitudes must be between -90 and +90 degrees.
  • Longitudes must be between -180 and +180 degrees.

Geolocation Field Limitations

Geolocation is a compound field that counts toward your org’s limits as three custom fields.

  • One for Latitude
  • One for Longitude
  • One for internal use

Calculating Distances APEX

Developers can opt to use the Location utility class, it contains methods for accessing the component fields of geolocation compound fields.