Retrieve the Geographical Data of Polygons

The getPolygonGeometry() Apex method returns the geometric data, such as the area and perimeter, for the requested geographical shape or custom polygon.

Signature

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

Sample Code of a Circle

This code passes a circular geographical region to the Salesforce Maps API.

If you invoke methods within a flow, process builder, or trigger, avoid uncommitted work errors when you perform one of the following.

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

Different processes refresh the token, such as plotting a route or schedule. The refresh process is almost immediate after each qualifying action occurs.

Warning

Input Format of a Circle

1{
2   type: 'circle', // Shape.
3   radius: Decimal, // Radius in meters from the center of the polygon.
4   lat: Decimal, // Latitude coordinate of the center of the circle.
5   lng: Decimal // Longitude coordinate of the center of the circle.
6}

Example

1// Create a circular geographical region.
2Map<String,Object> polygon = new Map<String,Object>{
3    'type' => 'circle',
4    'radius' => 50,
5    'lat' => 33.917760,
6    'lng' => -84.379750
7};
8
9// Call the getPolygonGeometry() method with the new polygon.
10Map<String,Object> response = maps.API.getPolygonGeometry(polygon);
11
12// Log the information about the resulting polygon.
13system.debug(response);

Sample Code of a Rectangle

This code passes a rectangular geographical region to the Salesforce Maps API.

Input Format of a Rectangle

1{
2   type: 'rectangle', // Shape.
3   NE: Map<String,Decimal>, // North-Eastern corner of the rectangle.
4   SW: Map<String,Decimal> // South-Western corner of the rectangle.
5}

Example

1// Add the geographical coordinates of the North-Eastern and South-Western corners.
2Map<String,Decimal> northEast = new Map<String,Decimal>{
3    'lat' => 33.943360,
4    'lng' => -84.515541
5};
6
7Map<String,Decimal> southWest = new Map<String,Decimal>{
8    'lat' => 33.559707,
9    'lng' => -84.101929
10};
11
12// Create the rectangular geographical region.
13Map<String,Object> polygon = new Map<String,Object>{
14    'type' => 'rectangle',
15    'NE' => northEast,
16    'SW' => southWest
17};
18
19// Call the getPolygonGeometry() method with the new polygon.
20Map<String,Object> response = maps.API.getPolygonGeometry(polygon);
21
22// Log the information about the resulting polygon.
23system.debug(response);

Sample Code of a Polygon

This code passes a polygonal geographical region to the Salesforce Maps API.

Input Format of a Polygon

1{
2   type: 'polygon', // Shape.
3   points: List<Map<String,Decimal>> // List of latitude and longitude coordinates of the polygon corners.
4}

Example

1// Create an array with the geographical coordinates of the corners of the polygon.
2String pointsArray = '[{"lat":34.29494470071453,"lng":-83.86492811369591},{"lat":34.167779992443144,"lng":-85.15032850432091},{"lat":32.80256665568334,"lng":-84.63397108244591},{"lat":33.32736906742004,"lng":-82.72234998869591}]';
3
4// Create the polygonal geographical region.
5Map<String,Object> polygon = new Map<String,Object>{
6    'type' => 'polygon',
7    'points'=> JSON.deserializeUntyped(pointsArray)
8};
9
10// Call the getPolygonGeometry() method with the new polygon.
11Map<String,Object> response = maps.API.getPolygonGeometry(polygon);
12
13// Log the information about the resulting polygon.
14system.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  "unit": "meters",
3  "geometries": {
4    "custom": {
5      "area": 7803.6128806207,
6      "perimeter": 313.654849054103
7    }
8  },
9  "count": 1,
10  "custom": true
11}