GMap.NET Tutorial: Calculating the area of a polygon on the map

GMap.NET LogoIf you’re using GMap.NET to add maps to your .NET application, then the time may come when you want to calculate the area of a polygon on your map. In my case, I have an application that allows users to actually create polygons using a GMap.NET control, by placing and connecting markers to shape their polygon. Since the map system is used to indicate the limits of an area occupied by a rural community, it’s necessary to calculate how many hectares the delimited area occupies.

Google Maps actually provides a function in its API to calculate polygon areas:

google.maps.geometry.spherical.computeArea(yourPolygon.getPath());

Even so, unfortunately GMap.NET does not offer access to this API. Moreover, your GMap.NET control may be configured to work with a different map provider from Google Maps, like Bing. Consequently, you’ll have to implement your own polygon area calculation algorithms. Luckily, this is not all that complicated, and algorithms are available on the net.

Here’s my approach.

  • Assuming that you have a list of PointLatLng values that form the polygon…
  • For each PointLatLng value, create a new value with the latitude and longitude values converted to meters.
  • Calculate the area of the polygon using this algorithm.

In code:

IList<PointLatLng> coords = // List of polygon coordinates
 
// Add all coordinates to a list, converting them to meters:
IList<PointLatLng> points = new List<PointLatLng>();
foreach (PointLatLng coord in coords)
{
  PointLatLng p = new PointLatLng(
    coord.Longitude * (System.Math.PI * 6378137 / 180),
    coord.Latitude * (System.Math.PI * 6378137 / 180)
  );
  points.Add(p);
}
// Add point 0 to the end again:
points.Add(points[0]);
 
// Calculate polygon area (in square meters):
var area = System.Math.Abs(points.Take(points.Count - 1)
  .Select((p, i) => (points[i + 1].Lat - p.Lat) * (points[i + 1].Lng + p.Lng))
  .Sum() / 2);

1 Comment

  1. YG says:

    hello
    I’d like without GMap.Net information balloons can be put, if the clip in the polygon to show me the area of the polygon


Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" cssfile="">