Italian calcolare superficie con google map

ivanomonti

Expert
Licensed User
Longtime User
ho bisogno di calcolare superficie selezionata con google map, qualcuno a mai realizzato questo! sempre per app scritta in b4j, grazie.

2018-04-11_004656.png
 

klaus

Expert
Licensed User
Longtime User
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

    '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
 
Last edited:

ivanomonti

Expert
Licensed User
Longtime User
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

Hi, difficult to understand, you have a written example (source) so as to understand step by step, I thank you for your cooperation, thank 1000
 

Star-Dust

Expert
Licensed User
Longtime User
Grazie Klaus che leggi pure l'italiano :)
 

klaus

Expert
Licensed User
Longtime User
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.

Grazie Klaus che leggi pure l'italiano
It's just google italian.
 

Star-Dust

Expert
Licensed User
Longtime User
I had guessed you were using a translator. I meant a thing.
I was surprised that you attended the Italian forum and read it (even if with a translator). :D
 

klaus

Expert
Licensed User
Longtime User
I read the titles of the threads, and when I think I could help or the subject might interest me, I read it.
I use Google Translator from the foreign language to english, not to french, the translations to english are better.
 
Last edited:

ivanomonti

Expert
Licensed User
Longtime User
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.

I do not know where to start this is the truth, I repeat, if you can give me an example in b4java I would be grateful
 

klaus

Expert
Licensed User
Longtime User
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?
 

ivanomonti

Expert
Licensed User
Longtime User
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?

http://solarconfigurator.wolmann.com/
 
Top