Newer Version Available
Retrieve the Territories in Which the Records Reside
The PointInPolygon() Apex method
returns the territory shape in which each record resides. It reads in the geographical
coordinates of one or more records and compares each against the surrounding territory
shape.
Signature
1Map<String, Object> maps.API.PointInPolygon(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.
- PointInPolygon() is the method.
Sample Code
This code reads in the geographical coordinates of two records and returns the territory shape in which each resides.
Input Format
1{
2version: 'int' // Required. Version of the API endpoint.
3points: [ {id:String, lat:Decimal, lng:Decimal, id:String}, ... ] // Array of record IDs, latitudes and longitutes.
4MATerritoryIds: List<String> // List of Maps territory IDs.
5}Example
1// Create a list of records.
2List<Map<String, Object>> coordinateList = new List<Map<String, Object>> {
3 new Map<String, Object> {
4 'id' => 'recordId_1',
5 'lat' => 34.0441233,
6 'lng' => -84.0076379
7 },
8 new Map<String, Object> {
9 'id' => 'recordId_2',
10 'lat' => 54.0441233,
11 'lng' => -84.0076379
12 }
13};
14
15// Create a list of territory IDs.
16List<String> MATerritoryIds = new List<String>();
17for (maps__ShapeLayer__c sl : [SELECT Id FROM maps__ShapeLayer__c]) {
18 MATerritoryIds.add(sl.Id);
19}
20
21// Build the request. Only version 2 is valid.
22Map<String, Object> request = new Map<String, Object> {
23 'version' => '2',
24 'points' => coordinateList,
25 'MATerritoryIds' => MATerritoryIds
26};
27
28// Call the PointInPolygon() method.
29Map<String,Object> response = maps.Api.PointInPolygon(request);
30
31// Log the resulting territory shapes of the regions.
32system.debug(response);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"results": [
3 {
4 "polygons":["a130t000000nNefAAE-1"], // The ID of the polygon the record resides in.
5 "point":{
6 "id":"recordId_1",
7 "lat":34.0441233,
8 "lng":-84.0076379
9 }
10 },
11 {
12 "polygons":[],
13 "point":{
14 "id":"recordId_2",
15 "lat":54.0441233,
16 "lng":-84.0076379
17 }
18 }
19]
20}