Sub topic 10
Geolocation fields and location data
This chapter assumes you can create custom fields in Object Manager and run basic SOQL and Apex. It covers what a Geolocation field stores, how to query it, and when to use a plain number or text field instead.
What a Geolocation field stores
A Geolocation field is a compound field. One field holds a latitude and a longitude together. Latitude must fall between -90 and 90, and longitude between -180 and 180.
When you create a Geolocation field named Location__c, Salesforce exposes these API names:
| API name | Holds |
|---|---|
| `Location__c` | The whole point, used in distance queries and formulas |
| `Location__Latitude__s` | The latitude as a number |
| `Location__Longitude__s` | The longitude as a number |
The two component names end in __s, not __c. Use them when you load data or write to individual values.
During setup you choose how coordinates display (decimal or degrees, minutes, seconds) and how many decimal places to keep. Display notation only affects the user interface.
Querying by distance
SOQL filters a Geolocation field with DISTANCE and GEOLOCATION. You can't compare the compound field to a value with = or <. The distance unit is 'mi' or 'km'.
Decimal lat = 37.7749;
Decimal lon = -122.4194;
List<Store__c> nearby = [
SELECT Name, Location__c
FROM Store__c
WHERE DISTANCE(Location__c, GEOLOCATION(:lat, :lon), 'mi') < 10
ORDER BY DISTANCE(Location__c, GEOLOCATION(:lat, :lon), 'mi')
LIMIT 10
];
for (Store__c s : nearby) {
Location storeLocation = s.Location__c;
Location origin = Location.newInstance(lat, lon);
Double miles = Location.getDistance(storeLocation, origin, 'mi');
System.debug(s.Name + ': ' + miles);
}Apex has a Location class for this. Location.getDistance computes a distance in code, and the field value can be assigned directly to a Location variable.
A formula field can also call DISTANCE. Use it when users need to see the distance on a record.
Common errors
- Writing only one component. If you set the latitude but leave the longitude empty, the save fails. Always load both.
- Filtering with a comparison.
WHERE Location__c = ...is invalid. UseDISTANCE. - Swapping the order.
GEOLOCATIONtakes latitude first, then longitude. A swapped pair puts points in the wrong place or outside the valid range. - Expecting an address lookup. The field stores coordinates only. It doesn't turn a street address into a point.
When to choose something else
Use a Geolocation field when you need distance filtering, sorting by distance, or map-based features. If you only display coordinates and never query by distance, two Number fields work, but you lose DISTANCE. If you only have an address, use the address fields. Don't store coordinates as Text, because you can't calculate distance from them.
Exercise
A Store_c object has a Geolocation field named Location_c. A data loader file needs to set the point for each store. Which API field names should the file's columns use, and why not the compound name?
Show answer
Use Location_Latitudes and LocationLongitudes. The compound field Location_c is read and queried as one value, but loads write the two components. Both columns must be filled for every row.