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

Map<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

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

Example

// Create a circular geographical region.
Map<String,Object> polygon = new Map<String,Object>{
    'type' => 'circle',
    'radius' => 50,
    'lat' => 33.917760,
    'lng' => -84.379750
};

// Call the getPolygonGeometry() method with the new polygon.
Map<String,Object> response = maps.API.getPolygonGeometry(polygon);

// Log the information about the resulting polygon.
system.debug(response);

Sample Code of a Rectangle

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

Input Format of a Rectangle

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

Example

// Add the geographical coordinates of the North-Eastern and South-Western corners.
Map<String,Decimal> northEast = new Map<String,Decimal>{
    'lat' => 33.943360,
    'lng' => -84.515541
};

Map<String,Decimal> southWest = new Map<String,Decimal>{
    'lat' => 33.559707,
    'lng' => -84.101929
};

// Create the rectangular geographical region.
Map<String,Object> polygon = new Map<String,Object>{
    'type' => 'rectangle',
    'NE' => northEast,
    'SW' => southWest
};

// Call the getPolygonGeometry() method with the new polygon.
Map<String,Object> response = maps.API.getPolygonGeometry(polygon);

// Log the information about the resulting polygon.
system.debug(response);

Sample Code of a Polygon

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

Input Format of a Polygon

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

Example

// Create an array with the geographical coordinates of the corners of the polygon.
String 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}]';

// Create the polygonal geographical region.
Map<String,Object> polygon = new Map<String,Object>{
    'type' => 'polygon',
    'points'=> JSON.deserializeUntyped(pointsArray)
};

// Call the getPolygonGeometry() method with the new polygon.
Map<String,Object> response = maps.API.getPolygonGeometry(polygon);

// Log the information about the resulting polygon.
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.

{
  "unit": "meters",
  "geometries": {
    "custom": {
      "area": 7803.6128806207,
      "perimeter": 313.654849054103
    }
  },
  "count": 1,
  "custom": true
}