Android Question Question about Polygon in Google map

mkvidyashankar

Active Member
Licensed User
Longtime User
I am trying to findout area of polygon using google map library
it is working fine. I struck with one issue.
with respect to first image
I select 6 points and find the area, got it correct

Please refer 2nd image
there, I insert one point, between 2 and 3 and tried to get area

It seems to be that the polygon is created following the sequence of points lat lon in the list.
if i insert some point between them, it is not giving correct area and shape

Is there any method to rearrange the coordinates to get correct area
 

Attachments

  • Screenshot_2014-12-25-13-52-55 copy.jpg
    Screenshot_2014-12-25-13-52-55 copy.jpg
    311.3 KB · Views: 233
  • Screenshot_2014-12-25-13-53-22 copy.jpg
    Screenshot_2014-12-25-13-53-22 copy.jpg
    330.9 KB · Views: 243

mkvidyashankar

Active Member
Licensed User
Longtime User
Hi Erel

I am adding that point clicking on the map
code is here

B4X:
Sub Map1_Click (Point As LatLng)
    Dim gmarker As Marker
    Dim gmarkerlistener As OnMarkerDragListener
    gmarker= gmap.AddMarker(Point.Latitude,Point.Longitude,"This is pick")
    gmarker.Draggable=True
    gmarkerlistener.Initialize("gmarkerlistener")
    gMapsExtras.SetOnMarkerDragListener(gmap, gmarkerlistener)
'    Log(Point)
  save_areadata(Point.Latitude,Point.Longitude,"This is pick",DateTime.Now)
End Sub


B4X:
Sub save_areadata(lat1 As Double,lon1 As Double,desc1 As String,dateclick1 As String)
    sql1.Initialize(filedire,"points.db",True)
    Dim sqlstring As String = lat1 &  "," & lon1 & ",'" & desc1 &"','" & dateclick1 & "'"
    sql1.ExecNonQuery("INSERT INTO " & fname & "(lat,lon,desc,datesnap) VALUES (" & sqlstring & ")")
    ToastMessageShow("Point Saved",True)
    sql1.Close
End Sub
 
Upvote 0

mkvidyashankar

Active Member
Licensed User
Longtime User
sorry i missed to paste
B4X:
Sub calculate_area(filename As String)
    Dim lt, ln As Double
    Dim ide As Int
    Dim desc As String
    Dim bbn As List
    Dim polygonoptions1 As PolygonOptions
    Dim polygon1 As Polygon
    Dim GoogleMapsExtras1 As GoogleMapsExtras
    Dim points() As LatLng
   
    bbn.Initialize
   
    sql1.Initialize(filedire,"points.db",True)
    Dim cursor1 As Cursor
    Dim sqlstring As String= "SELECT * from " & filename
    cursor1 =sql1.ExecQuery(sqlstring )
    Dim nodata As Int = cursor1.RowCount
    Dim points(nodata+1) As LatLng
    If cursor1.RowCount > 0 Then
        For i = 0 To cursor1.RowCount-1
            cursor1.Position=i
            ide = cursor1.GetInt("id")
            lt=cursor1.GetDouble("lat")
            ln=cursor1.GetDouble("lon")
            desc=cursor1.GetString("desc")
            bbn.Add(lt &","& ln)   
            points(i).Initialize(lt,ln)
        Next
            points(nodata).Initialize(points(0).Latitude,points(0).Longitude)
    End If   
    '----------------
    Dim puntos As List
    puntos.Initialize
     For i= 0 To nodata
     puntos.Add(points(i))
    
     Next
    '---------------------------
   
   
    polygonoptions1.Initialize
      polygonoptions1.FillColor=Colors.ARGB(128, 255, 0, 0)
      polygonoptions1.AddPoints(puntos)
      polygonoptions1.StrokeColor=Colors.Red
      polygonoptions1.StrokeWidth=2
    
      polygon1=GoogleMapsExtras1.AddPolygon(gmap, polygonoptions1)
   
   
    Dim bb As Navigation
    Dim areasqkm,areasqm As Double
    areasqkm =bb.SphericalArea(bbn)
    areasqm = areasqkm/(1000*1000)
    Log(areasqkm & " sqkm")
    Log(areasqm & "sqm")
    panel_result.Visible=True
    label_result.Text= "No of Points" & nodata & CRLF & NumberFormat(areasqkm,1,6) & " sqkm"
     
    cursor1.Close
    sql1.Close

End Sub
 
Upvote 0

mkvidyashankar

Active Member
Licensed User
Longtime User
hi erel
i am getting data in correct order
that is not the issue here
i will explain again

first i will insert some points to map and get the area

suppose i want to insert one more point between the exising points. i can save it to database. but area will not display correctly due to the sequence of insertion

i need to rearange based on the lat lon to get correct sequece.
that is the problem here
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
I see. This is more difficult and I'm pretty sure that you will find cases where there is no way to guess the correct order.

You can try to calculate the distance between the new point and each of the lines and then add the point between the two points that make the nearest line.

http://en.wikipedia.org/wiki/Distance_from_a_point_to_a_line
 
Upvote 0
Top