Private Sub GMap_PolygonClick (SelectedPolygon As MapPolygon)
Log(CalcPolygonArea(SelectedPolygon))
End Sub
'Calculates the area of a Polygon object in square meters
Public Sub CalcPolygonArea(SelectedPolygon As MapPolygon) As Double
Private i As Int
Private CosLat, LngScale, LatScale As Double
Private joMapShape, joPath, joPoint As JavaObject
'attribute a JavaObject fpr the selected Polygon
joMapShape = joMapShape.InitializeNewInstance("com.lynden.gmapsfx.shapes.Polygon", Null)
joMapShape = SelectedPolygon
joPath = joPath.InitializeNewInstance("com.lynden.gmapsfx.javascript.object.MVCArray", Null)
joPath = joMapShape.RunMethod("getPath", Null)
'get the number or vertexes of the polygon
Private Size As Int = joPath.RunMethod("getLength", Null)
'get the first vertex of the polygon
joPoint = joPath.RunMethod("getAt", Array As Object (0))
Private FirstPoint As LatLng = joPoint.InitializeNewInstance("com.lynden.gmapsfx.javascript.object.LatLong", Array(joPoint))
'calculate the scale factors to transform the degrees to meters
CosLat = CosD(FirstPoint.Latitude)
LatScale = cPI * 6317000 / 180 ' converts latitude degrees to m
LngScale = LatScale * CosLat ' converts longitude degrees to m
Private joPoint As JavaObject
joPoint = joPath.RunMethod("getAt", Array As Object (0))
Private ll As LatLng = joPoint.InitializeNewInstance("com.lynden.gmapsfx.javascript.object.LatLong", Array(joPoint))
'calculate the area,
'get the first point
Private Area, xx0, yy0, xx1, yy1 As Double
yy0 = ll.Latitude * LatScale
xx0 = ll.Longitude * LngScale
Area = 0
'calculates the area
For i = 1 To Size - 1
Private joPoint As JavaObject
'get the next vertext
joPoint = joPath.RunMethod("getAt", Array As Object (i))
Private ll As LatLng = joPoint.InitializeNewInstance("com.lynden.gmapsfx.javascript.object.LatLong", Array(joPoint))
yy1 = ll.Latitude * LatScale
xx1 = ll.Longitude * LngScale
'calculates the area of a trapeze
Area = Area + (xx1 - xx0) * (yy1 + yy0) / 2
xx0 = xx1
yy0 = yy1
Next
'gets the first point again to calculate the last trapeze
Private joPoint As JavaObject
joPoint = joPath.RunMethod("getAt", Array As Object (0))
Private ll As LatLng = joPoint.InitializeNewInstance("com.lynden.gmapsfx.javascript.object.LatLong", Array(joPoint))
yy1 = ll.Latitude * LatScale
xx1 = ll.Longitude * LngScale
Area = Abs(Area + (xx1 - xx0) * (yy1 + yy0) / 2)
'total area, depending on the order of the vertexes, the result can be negatif
'therefore Abs()
Return Area
End Sub