Friday 25 September 2015

GEOLOCATION field in Salesforce

Geolocation is a compound field in Salesforce which stores the Longitude and Latitude. Below are few key points about Geolocation field:

  • It is a compound field which stores Longitude and Latitude, Once stored it will be treated as Location in Salesforce.
  • It can be used to find the distance between two Locations
  • It can be used in SOQL query condition.
  • It can be used to display the location of Google Map using Salesforce Standard feature.
Example:

Let's assume that we have created a Geolocation field in Salesforce with the name Geo_Location__c,  then Salesforce automatically creates three fields in the background with this:
  • Longitude: Geo_Location__Longitude__s
  • Latitude: Geo_Location__Latitude__s
  • System field: For internal use

Location Based SOQL Query:

Using DISTANCE keyword we can find the distance between two Geolocation values, below is a quick example to understand this:

Public Class ShowNearByPlaces{

    Public static List<Account> findNearby(String lat, String lon) {

        Decimal Latitude = Decimal.valueOf(lat);
        Decimal Longitude = Decimal.valueOf(lon);

        //for example, below query is on Account
        List<Account> accList = [Select Id,Name, BillingStreet, BillingCity, BillingState, BillingPostalCode,
BillingCountry, BillingLatitude, BillingLongitude,Geo_Location__Longitude__s, Geo_Location__Latitude__s FROM Account WHERE DISTANCE(Geo_Location__c, GEOLOCATION(:Latitude, :Longitude), 'mi') < 10];

        return accList;
    }

}

No comments:

Post a Comment