Newer Version Available

This content describes an older version of this product. View Latest

Retrieve Data That Salesforce Maps Hosts

The GetHostedData() Apex method retrieves geographical IDs for properties and displays them in Salesforce Maps. We accomplish this retrieval in two requests while using the same method. The first request returns geographical IDs for properties that Salesforce Maps hosts for a specified area and filter criteria. The second request takes these geographical IDs and display parameters, such as legend and popup information, and displays them in Salesforce Maps.

Signature

1Map<String, Object> maps.API.GetHostedData(String parameters)
Where,
  • maps is the namespace that's available after you install Salesforce Maps.
  • API is the class that contains the global methods exposed to developers.
  • GetHostedData() is the method.

Sample Code to Retrieve Geographical IDs

The first example request using the GetHostedData() method returns geographical IDs for women-owned businesses that Salesforce Maps hosts. Those businesses produce revenue between $100,000 and $1,000,000 in a specified area.

If you invoke methods within a flow, process builder, or trigger, do one of the following to avoid uncommitted work errors:

  • Call the methods through a future method
  • Call the methods as queueable

Warning

Input Format of the Parameters

1{
2  "mapinfo": { // Boundary area.
3    "offset": integer, // Pagination when the number of results is large.
4    "limit": integer, // Maximum number of geographical IDs to return in response.
5    "celng": decimal, // Longitude coordinate to search closest records.
6    "celat": decimal, // Latitude coordinate to search closest records.
7    "swlng": decimal, // South-Western longitude coordinate for map boundary.
8    "swlat": decimal, // South-Western latitude coordinate for map boundary.
9    "nelng": decimal, // North-Eastern longitude coordinate for map boundary.
10    "nelat": decimal  // North-Eastern latitude coordinate for map boundary.
11  },
12  "data": {
13    "filters": [{}], // Filters to limit the results.
14    "defaultMarkerColor": "String:String", // Hex color : Type of marker to be returned
15    "level_id": "String", // Sub-section of the data specified by file_id.
16    "file_id": "String" // Data being queried, for example, business, property, political boundaries, etc.
17  }
18}

Example

1// Create the boundary area.
2Map<String,Decimal> mapInfo = new Map<String,Decimal>{
3    'nelat' => 49.92293,
4    'nelng' => -55.72265,
5    'swlat' => 29.49698,
6    'swlng' => -140.00976,
7    'celat' => 40.48038,
8    'celng' => -97.86621,
9    'limit' => 6,
10    'offset' => 0            
11};
12
13// Create the filters.
14Map<String,Object> femaleOwned = new Map<String,Object>{
15    'topic_id' => 'female_owned',
16    'operator' => 'equals',
17    'values' => new List<String>{'Yes'}
18};
19Map<String,Object> locationSalesTotal = new Map<String,Object>{
20    'topic_id' => 'location_sales_total',
21    'operator' => 'range',
22    'min' => '100000',
23    'max' => '1000000'
24};
25    
26List<Map<String,Object>> filters = new List<Map<String,Object>>();
27filters.add(femaleOwned);
28filters.add(locationSalesTotal);
29
30// Add the filters and additional 'data' parameters.
31Map<String,Object> dataMap = new Map<String,Object>{
32    'filters' => filters,
33    'defaultMarkerColor' => '93c47d:Circle',
34    'level_id' => '42', 
35    'file_id' => 'business'
36};   
37 
38// Build the request.
39Map<String,Object> request = new Map<String,Object>();
40request.put('data', dataMap);
41request.put('mapinfo', mapInfo);
42String requestString = JSON.serialize(request);
43
44// Call the GetHostedData() method with the boundary area and filter criteria.
45Map<String, Object> response = maps.API.GetHostedData(requestString);
46
47// Log the resulting locations. 
48System.debug(JSON.serialize(response));

Sample Response of Geographical IDs

Although the return value is an Apex Map<String, Object> object, this JSON response illustrates the essential data you receive in the resulting map.

1{
2   "endPoint":"https://internal.na.sfmapsapi.com/data/markers/2",
3   "limitInfo":{
4      "QueryRows":"12 / 50000",
5      "Queries":"8 / 100",
6      "HeapSize":"70683 / 6000000",
7      "CPUTime":"171 / 10000"
8   },
9   "request":"{\"mapinfo\":{\"offset\":0,\"limit\":6,\"celng\":-97.86621,\"celat\":40.48038,\"swlng\":-140.00976,\"swlat\":29.49698,\"nelng\":-55.72265,\"nelat\":49.92293},\"data\":{\"filters\":[{\"values\":[\"Yes\"],\"operator\":\"equals\",\"topic_id\":\"female_owned\"},{\"max\":\"1000000\",\"min\":\"100000\",\"operator\":\"range\",\"topic_id\":\"location_sales_total\"}],\"defaultMarkerColor\":\"93c47d:Circle\",\"level_id\":\"42\",\"file_id\":\"business\"}}",
10   "params":{
11      "data":{
12         "file_id":"business",
13         "level_id":"42",
14         "defaultMarkerColor":"93c47d:Circle",
15         "filters":[
16            {
17               "topic_id":"female_owned",
18               "operator":"equals",
19               "values":[
20                  "Yes"
21               ]
22            },
23            {
24               "topic_id":"location_sales_total",
25               "operator":"range",
26               "min":"100000",
27               "max":"1000000"
28            }
29         ]
30      },
31      "mapinfo":{
32         "nelat":49.92293,
33         "nelng":-55.72265,
34         "swlat":29.49698,
35         "swlng":-140.00976,
36         "celat":40.48038,
37         "celng":-97.86621,
38         "limit":6,
39         "offset":0
40      }
41   },
42   "success":true,
43   "data":{ // List of geographical IDs.
44      "ids":[
45         "11140017125231",
46         "11140017127280",
47         "11140017102994",
48         "11140017127285",
49         "11132553548894",
50         "11140017127286"
51      ]
52   }
53}

Sample Code to Display Geographical IDs

The second request using the GetHostedData() method takes the geographical IDs and display parameters, such as legend and popup information, and displays them in Salesforce Maps.

Input Format of the Parameters

1{
2  "mapinfo": {
3    "offset": integer, // Pagination when the number of results is large.
4    "limit": integer, // Maximum number of geographical IDs to return in response.
5    "celng": decimal, // Longitude coordinate to search closest records.
6    "celat": decimal, // Latitude coordinate to search closest records.
7    "swlng": decimal, // South-Western longitude coordinate for map boundary.
8    "swlat": decimal, // South-Western latitude coordinate for map boundary.
9    "nelng": decimal, // North-Eastern longitude coordinate for map boundary.
10    "nelat": decimal  // North-Eastern latitude coordinate for map boundary.
11  },
12  "data": {
13    "popup": { // Popup information for the displayed locations.
14      "tabs": [{}],// Array of tab objects to be displayed in the popup.
15      "header": [{}] // Array of objects containing information of the tab headers.
16    },
17    "legend": { // Map legend.
18      "rows": [{}], // Array of objects containing information of each row in the legend.
19      "subTitle": "String", // Subtitle of the legend.
20      "title": "String" // Title of the the legend.
21    },
22    "defaultMarkerColor": "String:String", // Hex color : Type of marker to be returned.
23    "level_id": "String", // Sub-section of the data specified by file_id.
24    "file_id": "String" // Data being queried, for example, business, property, political boundaries, etc.
25  },
26  "aggregates": Boolean, // Aggregated data of all the records returned. It contains the min, max, sum, and averages of the records.
27  "details": Boolean, // Return the details for the requested records.
28  "ids": [] // Array of geographical IDs.
29}

Example

1// Create the map legend.
2 Map<String,Object> row1 = new Map<String,Object>
3{
4    'topic_id' => 'female_owned',
5    'operator' => 'equals',
6    'values' => new List<String>{'Yes'},
7    'color' => '38d84a:Marker'
8};
9
10Map<String,Object> row2 = new Map<String,Object>
11{
12    'topic_id' => 'location_sales_total',
13    'operator' => 'range',
14    'min' => '10',
15    'max' => '1000000',
16    'color' => '38d87c:Marker'
17};
18
19List<Map<String,Object>> legendRows = new List<Map<String,Object>>();        
20legendRows.add(row1);
21legendRows.add(row2);
22
23Map<String,Object> legend = new Map<String,Object>{
24    'title' => 'Total Company Sales',
25    'subTitle' => 'Breakdown of businesses based on company sales',
26    'rows' => legendRows
27};        
28
29// Create the popup header.
30List<Map<String,String>> header = new List<Map<String,String>>();
31header.add(new map<String,String>{
32        'file_id' => 'business',
33        'topic_id' => 'location_sales_total'
34    }
35);        
36
37// Create the popup tabs.
38List<Map<String,String>> tabData = new List<Map<String,String>>();
39tabData.add(new map<String,String>{'file_id' => 'business', 'topic_id' => 'location_sales_total'});
40
41Map<String,Object> tab = new map<String,Object>{
42    'tab_id' => '1520003558209',
43    'tab_label' => 'Additional info',
44    'data' => tabData
45};
46
47List<Map<String,Object>> tabs = new List<Map<String,Object>>();
48tabs.add(tab);
49
50// Build the popup object.     
51Map<String,Object> popup = new Map<String,Object>{
52    'header' => header,
53    'tabs' => tabs
54};
55
56// Build the 'data' object.
57Map<String,Object> data = new Map<String,Object>{
58    'file_id' => 'business',
59    'level_id' => '42',
60    'defaultMarkerColor' => '93c47d:Circle',
61    'legend' => legend,
62    'popup' => popup
63};
64
65// Create the boundary area.
66Map<String,Decimal> mapInfo = new Map<String,Decimal>{
67    'nelat' => 49.92293,
68    'nelng' => -55.72265,
69    'swlat' => 29.49698,
70    'swlng' => -140.00976,
71    'celat' => 40.48038,
72    'celng' => -97.86621,
73    'limit' => 6,
74    'offset' => 0            
75};
76
77// Create the array of geographical IDs.
78List<String> returnedIds = new List<String>{'11140017125231','11140017127280','11140017102994','11140017127285','11132553548894','11140017127286'};
79
80// Build the request.
81Map<String,Object> request = new Map<String,Object>{
82    'ids' => returnedIds,
83    'details' => true, // Return the details of the requested records.
84    'aggregates' => true, // Get the aggregated record data.
85    'data' => data,
86    'mapinfo' => mapInfo
87};
88
89String requestString = JSON.serialize(request);
90
91// Call the GetHostedData() method with the geographical IDs and display parameters.
92Map<String, Object> response = maps.API.GetHostedData(requestString);
93
94//Log the results.
95System.debug(JSON.serialize(response));

Sample Response of Displaying the Geographical IDs

Although the return value is an Apex Map<String, Object> object, this JSON response illustrates the essential data you receive in the resulting map.

1{
2  "endPoint": "https://internal.na.sfmapsapi.com/data/markers/2",
3  "limitInfo": {
4    "QueryRows": "12 / 50000",
5    "Queries": "8 / 100",
6    "HeapSize": "79714 / 6000000",
7    "CPUTime": "129 / 10000"
8  },
9  "request": "{\"mapinfo\":{\"offset\":0,\"limit\":6,\"celng\":-97.86621,\"celat\":40.48038,\"swlng\":-140.00976,\"swlat\":29.49698,\"nelng\":-55.72265,\"nelat\":49.92293},\"data\":{\"popup\":{\"tabs\":[{\"data\":[{\"topic_id\":\"location_sales_total\",\"file_id\":\"business\"}],\"tab_label\":\"Additional info\",\"tab_id\":\"1520003558209\"}],\"header\":[{\"topic_id\":\"location_sales_total\",\"file_id\":\"business\"}]},\"legend\":{\"rows\":[{\"color\":\"38d84a:Marker\",\"values\":[\"Yes\"],\"operator\":\"equals\",\"topic_id\":\"female_owned\"},{\"color\":\"38d87c:Marker\",\"max\":\"1000000\",\"min\":\"10\",\"operator\":\"range\",\"topic_id\":\"location_sales_total\"}],\"subTitle\":\"Breakdown of businesses based on company sales\",\"title\":\"Total Company Sales\"},\"defaultMarkerColor\":\"93c47d:Circle\",\"level_id\":\"42\",\"file_id\":\"business\"},\"aggregates\":true,\"details\":true,\"ids\":[\"11140017125231\",\"11140017127280\",\"11140017102994\",\"11140017127285\",\"11132553548894\",\"11140017127286\"]}",
10  "params": {
11    "ids": [
12      "11140017125231",
13      "11140017127280",
14      "11140017102994",
15      "11140017127285",
16      "11132553548894",
17      "11140017127286"
18    ],
19    "details": true,
20    "aggregates": true,
21    "data": {
22      "file_id": "business",
23      "level_id": "42",
24      "defaultMarkerColor": "93c47d:Circle",
25      "legend": {
26        "title": "Total Company Sales",
27        "subTitle": "Breakdown of businesses based on company sales",
28        "rows": [
29          {
30            "topic_id": "female_owned",
31            "operator": "equals",
32            "values": [
33              "Yes"
34            ],
35            "color": "38d84a:Marker"
36          },
37          {
38            "topic_id": "location_sales_total",
39            "operator": "range",
40            "min": "10",
41            "max": "1000000",
42            "color": "38d87c:Marker"
43          }
44        ]
45      },
46      "popup": {
47        "header": [
48          {
49            "file_id": "business",
50            "topic_id": "location_sales_total"
51          }
52        ],
53        "tabs": [
54          {
55            "tab_id": "1520003558209",
56            "tab_label": "Additional info",
57            "data": [
58              {
59                "file_id": "business",
60                "topic_id": "location_sales_total"
61              }
62            ]
63          }
64        ]
65      }
66    },
67    "mapinfo": {
68      "nelat": 49.92293,
69      "nelng": -55.72265,
70      "swlat": 29.49698,
71      "swlng": -140.00976,
72      "celat": 40.48038,
73      "celng": -97.86621,
74      "limit": 6,
75      "offset": 0
76    }
77  },
78  "success": true,
79  "data": {
80    "aggregates": [
81      {
82        "avg": 329333.3333333333,
83        "sum": 1976000,
84        "max": 461000,
85        "min": 303000,
86        "label": "Sales: Total (USD)"
87      }
88    ],
89    "legend": {
90      "rows": [
91        {
92          "values": [
93            "Yes"
94          ],
95          "row_id": "row-0",
96          "operator": "equals",
97          "color": "38d84a:Marker",
98          "topic_id": "female_owned"
99        },
100        {
101          "max": "1000000",
102          "min": "10",
103          "row_id": "row-1",
104          "operator": "range",
105          "color": "38d87c:Marker",
106          "topic_id": "location_sales_total"
107        },
108        {
109          "values": [
110            "--Other--"
111          ],
112          "row_id": "row-other",
113          "operator": "equals",
114          "color": "93c47d:Circle",
115          "topic_id": "female_owned"
116        }
117      ],
118      "subTitle": "Breakdown of businesses based on company sales",
119      "title": "Total Company Sales"
120    },
121    "markers": [
122      {
123        "popup": {
124          "tabs": [
125            {
126              "data": [
127                {
128                  "formatted_value": "$303,000.00",
129                  "value": "303000",
130                  "topic_id": "location_sales_total",
131                  "label": "Sales: Total (USD)"
132                }
133              ],
134              "tab_id": "1520003558209",
135              "tab_label": "Additional info"
136            }
137          ],
138          "header": [
139            {
140              "formatted_value": "$303,000.00",
141              "value": "303000",
142              "topic_id": "location_sales_total",
143              "label": "Sales: Total (USD)"
144            }
145          ]
146        },
147        "position": {
148          "lng": "-97.886011",
149          "lat": "40.441185"
150        },
151        "color": "38d84a:Marker",
152        "rowid": "row-0",
153        "datatype": "business",
154        "c2c": true,
155        "uid": "11140017125231",
156        "geoid": "11140017125231",
157        "label": "Urbauer Family Farm"
158      },
159      {
160        "popup": {
161          "tabs": [
162            {
163              "data": [
164                {
165                  "formatted_value": "$303,000.00",
166                  "value": "303000",
167                  "topic_id": "location_sales_total",
168                  "label": "Sales: Total (USD)"
169                }
170              ],
171              "tab_id": "1520003558209",
172              "tab_label": "Additional info"
173            }
174          ],
175          "header": [
176            {
177              "formatted_value": "$303,000.00",
178              "value": "303000",
179              "topic_id": "location_sales_total",
180              "label": "Sales: Total (USD)"
181            }
182          ]
183        },
184        "position": {
185          "lng": "-97.800868",
186          "lat": "40.466693"
187        },
188        "color": "38d84a:Marker",
189        "rowid": "row-0",
190        "datatype": "business",
191        "c2c": true,
192        "uid": "11140017102994",
193        "geoid": "11140017102994",
194        "label": "Rauscher Family Farm"
195      },
196      {
197        "popup": {
198          "tabs": [
199            {
200              "data": [
201                {
202                  "formatted_value": "$461,000.00",
203                  "value": "461000",
204                  "topic_id": "location_sales_total",
205                  "label": "Sales: Total (USD)"
206                }
207              ],
208              "tab_id": "1520003558209",
209              "tab_label": "Additional info"
210            }
211          ],
212          "header": [
213            {
214              "formatted_value": "$461,000.00",
215              "value": "461000",
216              "topic_id": "location_sales_total",
217              "label": "Sales: Total (USD)"
218            }
219          ]
220        },
221        "position": {
222          "lng": "-97.850325",
223          "lat": "40.408435"
224        },
225        "color": "38d84a:Marker",
226        "rowid": "row-0",
227        "datatype": "business",
228        "c2c": true,
229        "uid": "11132553548894",
230        "geoid": "11132553548894",
231        "label": "Johnson Insurance & Financial Services"
232      },
233      {
234        "popup": {
235          "tabs": [
236            {
237              "data": [
238                {
239                  "formatted_value": "$303,000.00",
240                  "value": "303000",
241                  "topic_id": "location_sales_total",
242                  "label": "Sales: Total (USD)"
243                }
244              ],
245              "tab_id": "1520003558209",
246              "tab_label": "Additional info"
247            }
248          ],
249          "header": [
250            {
251              "formatted_value": "$303,000.00",
252              "value": "303000",
253              "topic_id": "location_sales_total",
254              "label": "Sales: Total (USD)"
255            }
256          ]
257        },
258        "position": {
259          "lng": "-97.861957",
260          "lat": "40.549917"
261        },
262        "color": "38d84a:Marker",
263        "rowid": "row-0",
264        "datatype": "business",
265        "c2c": true,
266        "uid": "11140017127285",
267        "geoid": "11140017127285",
268        "label": "Schmer Family Farm"
269      },
270      {
271        "popup": {
272          "tabs": [
273            {
274              "data": [
275                {
276                  "formatted_value": "$303,000.00",
277                  "value": "303000",
278                  "topic_id": "location_sales_total",
279                  "label": "Sales: Total (USD)"
280                }
281              ],
282              "tab_id": "1520003558209",
283              "tab_label": "Additional info"
284            }
285          ],
286          "header": [
287            {
288              "formatted_value": "$303,000.00",
289              "value": "303000",
290              "topic_id": "location_sales_total",
291              "label": "Sales: Total (USD)"
292            }
293          ]
294        },
295        "position": {
296          "lng": "-97.863180",
297          "lat": "40.557545"
298        },
299        "color": "38d84a:Marker",
300        "rowid": "row-0",
301        "datatype": "business",
302        "c2c": true,
303        "uid": "11140017127286",
304        "geoid": "11140017127286",
305        "label": "Leininger Family Farm"
306      },
307      {
308        "popup": {
309          "tabs": [
310            {
311              "data": [
312                {
313                  "formatted_value": "$303,000.00",
314                  "value": "303000",
315                  "topic_id": "location_sales_total",
316                  "label": "Sales: Total (USD)"
317                }
318              ],
319              "tab_id": "1520003558209",
320              "tab_label": "Additional info"
321            }
322          ],
323          "header": [
324            {
325              "formatted_value": "$303,000.00",
326              "value": "303000",
327              "topic_id": "location_sales_total",
328              "label": "Sales: Total (USD)"
329            }
330          ]
331        },
332        "position": {
333          "lng": "-97.900864",
334          "lat": "40.509699"
335        },
336        "color": "38d84a:Marker",
337        "rowid": "row-0",
338        "datatype": "business",
339        "c2c": true,
340        "uid": "11140017127280",
341        "geoid": "11140017127280",
342        "label": "Cloet Family Farm"
343      }
344    ]
345  }
346}