Newer Version Available

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

Get the Geographical Coordinates of Addresses in Batch

The BatchGeocode() Apex method takes one or more addresses and returns their geographical coordinates and formatted addresses.

Signature

1Map<String, Object> maps.API.BatchGeocode(Map<String, Object>)
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.
  • BatchGeocode() is the method.

Sample Code

This code returns the geographical coordinates of the White House and Washington Monument. The output also returns the formatted address and adds missing information, such as postal code and country, if available.

Input Format of an Address

1String address = '{HouseNumber} {Street}, {City}, {State} {PostalCode} {Country}';
2String address = '{HouseNumber} {Street}, {City}, {State}';

Example

1// Create a list of addresses.
2Map<string,Map<string,string>> addressesToSend = new Map<string,Map<string,string>>();
3
4String whiteHouseAddress = '1600 Pennsylvania Avenue NW, Washington, DC 20500 USA';
5Map<string,string> whiteHouseInfo = new Map<string,string>{
6    'address' => whiteHouseAddress
7};
8    
9String washingtonMonumentAddress = '2 15th St NW, Washington, DC 20024 USA';
10Map<string,string> washingtonMonumentInfo = new Map<string,string>{
11    'address' => washingtonMonumentAddress
12};
13
14addressesToSend.put('whiteHouse', whiteHouseInfo);
15addressesToSend.put('washingtonMonument', washingtonMonumentInfo);
16
17// Build the request.
18Map<string,object> batchOptions = new Map<String,Object> {
19            'version' => '2', // Required. Version of the API endpoint.
20            'address_info' => JSON.serialize(addressesToSend)
21        };
22
23// Call the BatchGeocode() method with the addresses.
24Map<String, Object> matrix = maps.API.BatchGeocode(batchOptions);
25
26// Log the resulting geographical coordinates and formatted addresses. 
27for(String k : matrix.keySet()){
28    system.debug(matrix.get(k));

Sample Response

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  "baseUrl": "https://internal.na.sfmapsapi.com/core/batchgeocode/2",
3  "results": {
4    "whitehouse": {
5      "data": {
6        "houseNumber": "1600",
7        "matchLevel": "Address",
8        "score": 100,
9        "country": "USA",
10        "postal": "20500",
11        "state": "DC",
12        "city": "Washington",
13        "street": "Pennsylvania Ave NW",
14        "fullAddress": "1600 Pennsylvania Ave NW, Washington, DC 20500, United States",
15        "position": {
16          "lng": -77.03655,
17          "lat": 38.89768
18        }
19      },
20      "source": "http",
21      "success": true
22    },
23    "washingtonmonument": {
24      "data": {
25        "houseNumber": "2",
26        "matchLevel": "Address",
27        "score": 97,
28        "country": "USA",
29        "postal": "20004",
30        "state": "DC",
31        "city": "Washington",
32        "street": "15th St NW",
33        "fullAddress": "2 15th St NW, Washington, DC 20004, United States",
34        "position": {
35          "lng": -77.0330958,
36          "lat": 38.889043
37        }
38      },
39      "source": "http",
40      "success": true
41    }
42  },
43  "success": true
44}

Sample Response If Not Geocoded

If an address can't be geocoded, you receive this response. 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  "success": false,
3  "message": "No results for that particular address."
4}