Location Class

Contains methods for accessing the component fields of geolocation compound fields.

system

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.

“Location” in Salesforce can also refer to the Location standard object. When referencing the Location object in your Apex code, always use Schema.Location instead of Location to prevent confusion with the standard Location compound field. If referencing both the location object and the Location field in the same snippet, you can differentiate between the two by using System.Location for the field and Schema.Location for the object.

The following are methods for Location.

Calculates the distance between this location and the specified location, using an approximation of the haversine formula and the specified unit.

public Double getDistance(Location toLocation, String unit)

  • toLocation:

    Type: Location

    The Location to which you want to calculate the distance from the current Location.

  • unit:

    Type: String

    The distance unit you want to use: mi or km.

Type: Double

Calculates the distance between the two specified locations, using an approximation of the haversine formula and the specified unit.

public static Double getDistance(Location firstLocation, Location secondLocation, String unit)

  • firstLocation:

    Type: Location

    The first of two locations used to calculate distance.

  • secondLocation:

    Type: Location

    The second of two locations used to calculate distance.

  • unit:

    Type: String

    The distance unit you want to use: mi or km.

Type: Double

Returns the latitude field of this location.

public Double getLatitude()

Type: Double

Returns the longitude field of this location.

public Double getLongitude()

Type: Double

Creates an instance of the Location class, with the specified latitude and longitude.

public static Location newInstance(Decimal latitude, Decimal longitude)

Type: Location