ho bisogno di calcolare superficie selezionata con google map, qualcuno a mai realizzato questo! sempre per app scritta in b4j, grazie.
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
You can do it with the code below:
B4X: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 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) Private Size As Int = joPath.RunMethod("getLength", Null) joPoint = joPath.RunMethod("getAt", Array As Object (0)) Private FirstPoint As LatLng = joPoint.InitializeNewInstance("com.lynden.gmapsfx.javascript.object.LatLong", Array(joPoint)) 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)) Private Area, xx0, yy0, xx1, yy1 As Double yy0 = ll.Latitude * LatScale xx0 = ll.Longitude * LngScale Area = 0 For i = 1 To Size - 1 Private joPoint As JavaObject 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 Area = Area + (xx1 - xx0) * (yy1 + yy0) / 2 xx0 = xx1 yy0 = yy1 Next 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) Return Area End Sub
It's just google italian.Grazie Klaus che leggi pure l'italiano
In the MapPolygon object of the GPS library, the vetexes (points) of the polygon are not directly accessible in B4J.
Therefore I use JavaObjects to access the Polygon and its vertexes.
Then we need to transform the degrees to distances, meters in the routine, defined in the scale factors.
And calculate the area, for the calculation you can google.
I added a few comments in the code in post #2.
HERE you find the documentation for the GPS objcects I used.
It's just google italian.
In the image in your first post you show a rectangle.
As you didn't give any infromation about what you do, I suppose you use a GoogleMap, so how is this rectangle defined?
Can you click on it?
Or where does your image come from?
You need to give more infromation on what you have done and what exactly you want to do.
To calculate the area you need to know the coordinates of the vertices of the polygon, which are the corners points in your example.
Do you know these coordinates?