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.
Review Considerations for Retrieving
Territory Shapes.
Signature
Map<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
{
version: 'int' // Required. Version of the API endpoint.
points: [ {id:String, lat:Decimal, lng:Decimal, id:String}, ... ] // Array of record IDs, latitudes and longitutes.
MATerritoryIds: List<String> // List of Maps territory IDs.
}
Example
// Create a list of records.
List<Map<String, Object>> coordinateList = new List<Map<String, Object>> {
new Map<String, Object> {
'id' => 'recordId_1',
'lat' => 34.0441233,
'lng' => -84.0076379
},
new Map<String, Object> {
'id' => 'recordId_2',
'lat' => 54.0441233,
'lng' => -84.0076379
}
};
// Create a list of territory IDs.
List<String> MATerritoryIds = new List<String>();
for (maps__ShapeLayer__c sl : [SELECT Id FROM maps__ShapeLayer__c]) {
MATerritoryIds.add(sl.Id);
}
// Build the request. Only version 2 is valid.
Map<String, Object> request = new Map<String, Object> {
'version' => '2',
'points' => coordinateList,
'MATerritoryIds' => MATerritoryIds
};
// Call the PointInPolygon() method.
Map<String,Object> response = maps.Api.PointInPolygon(request);
// Log the resulting territory shapes of the regions.
system.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.
{
"results": [
{
"polygons":["a130t000000nNefAAE-1"], // The ID of the polygon in which the record resides, followed by its unique ID for all geometry records associated with the polygon.
"point":{
"id":"recordId_1",
"lat":34.0441233,
"lng":-84.0076379
}
},
{
"polygons":[],
"point":{
"id":"recordId_2",
"lat":54.0441233,
"lng":-84.0076379
}
}
]
}