I came across a question on the answer community where someone is looking for a solution to display the google map on detail page using geolocation field. I thought it’s a good topic to write about so everyone will get benefitted.
Use case – I have a geolocation field on the Account object for storing the client’s office location, now I want to display that location on google map. Is that possible?
Solution – Yes, it’s pretty simple and anyone can do that using a formula field. Formula field will use the google map static API to display the image of the location. So, following are the steps to display the geolocation on google map.
- Create a formula field of Text type.
- Use IMAGE function in the formula which will render the image dynamically based on the passed geolocation.
IMAGE('https://maps.googleapis.com/maps/api/staticmap?key=YOUR_API_KEY&zoom=Zoom_Level&size=Map_WidthxMap_Height&markers=color:Marker_Color|label:Marker_Label|'+ TEXT(GeoLocation_Latitude_Field) + ',' + TEXT(GeoLocation_Longitute_Field), 'Title')
You must also add your API Keys obtained from your google account.
New pricing changes went into effect on July 16, 2018 and that has made the Google API keys mandatory in the URL.
Why don’t we take a simple example? Yes, we should and that will help all of us to understand the solution.
Example – I have a field with label “Office location” (Geolocation field) and Office_Location__c is the API name. Now, it will have 2 integrated fields (Latitude__s and Longitude__s) for storing the latitude and longitude values respectively.
Now, I created a “Location on Map” formula field (Text) and following is the formula for that field.
IMAGE('https://maps.googleapis.com/maps/api/staticmap?zoom=13&size=300x200&markers=color:green|label:S|'+ TEXT(Office_Location__Latitude__s) + ',' + TEXT(Office_Location__Longitude__s), 'Office Location')
Here is the outcome from the above formula.
Following URL can be useful to update the formula to leverage wonderful features of google map API.