Think. Build. Salesforce Solutions.

Salesforce Consulting Services across Salesforce Clouds & Across App Lifecycles

Blog

SALESFORCE GEOLOCATION DATA

By |2020-07-23T10:35:00+00:00October 18th, 2019|

Dealing with data in a location friendly manner is not a new problem. Way past, when Apex was, however, a brand new language and lacked some higher mathematical functions like sine and cosine Trying to determine location without things like sine and cosine was technically not possible. So that was three things that salesforce lacked. One solution can be to tag data based on a database that could translate ZIP codes into latitude and longitude and then do queries based on squares. Because when you don’t have trig on your side, squares are much easier to deal with.
Although it was too hectic as a task, it was also kind of fun.
But now after Winter ’13, you can get even more awesome without the hackery.

Salesforce Objects with Geolocation

The first thing you’ll need to do as a new custom field to your object. Go to the object definition in Setup, and click New Custom Field. After Winter ’13, you’ll see a new field type – Geolocation.

salesforce geolocation data
Currently, you will be unable to feature Geolocation fields via Schema Builder. One of the choices you’ll need to make is whether you will want the field to be defined in degree or decimal notation. Personally, I realize decimal is used much when using most geolocation services and is overall easier to store. Apex will also refer to the value with decimal notation regardless, so this decision is mostly about how it will be represented externally and you’ll need to determine the number of decimal places to extend your field with regardless. More decimal places mean much greater accuracy, with six decimal places equalling to about 1 meter of accuracy:

salesforce geolocation data

Data search based on Location

A geolocation search query :
SELECT Name FROM Contact WHERE DISTANCE(Location__c, GEOLOCATION(37.7945391,-122.3947166), ‘mi’) < 1
That SOQL statement can do the distance calculation and return our one contact within a mile for the coordinates as asked in the query (One Market Street in San Francisco):

salesforce geolocation data
If I reverse the query that lesser than over to a greater than, we get everyone outside of a mile from

Salesforce HQ, namely:

geolocation data type in salesforce

Geolocation with Apex and Visualforce

What if we want to update this programmatically? Or if you have a database of existing geolocation data based on ZIP code? Most HTML5 friendly browsers support geolocation via JavaScript, therefore, it’d simply be a matter of extending some basic Visualforce with that particular bit of functionality. The trick here is knowing the syntax for the latitude and longitude fields, which goes:
Custom_Field__Latitude__s and Custom_Field__Longitude__
Note the multiple uses of double underlines after the custom field name. So in our case, Contact.Location__c_Latitude__s would refer specifically to the Latitude portion of our Location field.

Geolocation with API

If I sent this POST data via REST to create a new record:
{ “Name” : “Geo”, “FirstName” : “Neo”, “Location__Latitude__s” : 37.794539,
“Location__Longitude__s” : -122.394717 }
The record would be created with the Location fields filled out. Or if I wanted to search,
I could send this over GET:
/services/data/v26.0/query?q=SELECT+Name+FROM+Contact+WHERE+DISTANCE(L ocation__c,+GEOLOCATION(37.7945391,-122.3947166),+’mi’)+<+1

Leave A Comment