B4J Question [Geofence] Rectangle area,

Beja

Expert
Licensed User
Longtime User
Hi All,
Thanks to Erel for the circle example, now I am trying to draw a rectangle area (4 gMap markers or a box drown around the area)
The rectangle is defined by 4 lat/long coordinates.. The 4 corners of the rectangle are as follows:

39.904861, -74.33508
39.866925, -74.186765
39.592326, -74.357053
39.617719, -74.477903

A small project is appreciated (or code snippet) will donate $30
 

Magma

Expert
Licensed User
Longtime User
Hi there... hope that will help you...

I know that can create (with gmap) polygons (also polygon are better than rectangles !!!)... and is very easy to check inside (geofence)...

a way to create them:

not tested (the example created, but other older software created working good with all these) hope... works, create a sub and write that, will create your polygon (if called)...
B4X:
dim pollst as list

...

sub yoursub

dim pol As MapPolygon
Dim latlst As List
Dim lonlst As List

pollst.initialize
latlst.initialize
lonlst.initialize

'1st point
latlst.add(39.904861)
lonlst.add(-74.33508)
'2nd point
latlst.add(39.866925)
lonlst.add(-74.186765)
'3rd point
latlst.add(39.592326)
lonlst.add(-74.357053)
'4th point
latlst.add(39.617719)
lonlst.add(-74.477903)

        For c=0 To latlst.Size-1
            Dim newposition As LatLng
            newposition.Initialize(latlst.Get(c),lonlst.Get(c))
            pollst.Add(newposition)
        Next
        pol=gmap.AddPolygon(pollst, 0.5,fx.Colors.Red,fx.Colors.Red,0.1)
end sub

but the special sub (PointInsidePolygon) need to check... if your current lat, lng is into polygon is this:
B4X:
Sub PointInsidePolygon(point As LatLng, LatLngList As List) As Boolean
    Dim pa, pb, ptemp As LatLng
    Dim i As Int
    Dim Inside As Boolean=False
    For j=0 To LatLngList.Size-1
        pa=LatLngList.Get(j)
        i=j+1
        If i=LatLngList.Size Then i=0
        pb=LatLngList.Get(i)
        If pb.Latitude<pa.Latitude Then
            ptemp=pa
            pa=pb
            pb=ptemp
        End If
        If RayIntersect(point.Latitude, pa.Latitude, pb.Latitude, point.Longitude, pa.Longitude, pb.Longitude) Then Inside=Not(Inside)
    Next
    Return Inside
End Sub

Sub RayIntersect(Py As Float, Ay As Float, By As Float, Px As Float, Ax As Float, Bx As Float) As Boolean
    Dim RedIsInfinity, BlueIsInfinity As Boolean
    Dim mred, mblue As Float
    If Py=Ay Or Py=By Then Py=Py+0.00001
    If Py<Ay Or Py>By Then
        Return False
    else if Px>Max(Ax,Bx) Then
        Return False
    Else
        If Px<Min(Ax,Bx) Then
            Return True
        Else
            If Ax<>Bx Then
                mred=(By-Ay)/(Bx-Ax)
            Else
                RedIsInfinity=True
            End If
            If Ax<>Px Then
                mblue=(Py-Ay)/(Px-Ax)
            Else
                BlueIsInfinity=True
            End If
            If BlueIsInfinity Then
                Return True
            else if RedIsInfinity Then
                Return False
            Else if mblue>=mred Then
                Return True
            Else
                Return False
            End If
        End If
    End If
End Sub
If will return True... you are into it...

ps: pollst, have it as public... so you can use it to check your current point if it is into pollst (with the special sub) !
ps2: Ready "Geofence" of googlemaps never worked for me... also as i know is only for android... by the way the same code I ve written here... using in b4a too!
 
Last edited:
Upvote 0

Hamied Abou Hulaikah

Well-Known Member
Licensed User
Longtime User
Geofence is for circle coverage.
I was faced this before and solved it as the following:
The simplest way I follow to cover rectangle or any other shape is dividing that shape in to circles as pic:
1653664700147.png

Then I make geofence instances for that circles.
I assumed all geofence instances returned false as the point outside them, only one or two return true if point inside them.
So making a global boolean variable with default false value, when any circle point inside changing value to true, and so on.
 
Upvote 0

Beja

Expert
Licensed User
Longtime User
Thank you Magma
the solution I am looking for is a little different. I will supply the text file with the coordinates. the app will read my text file and then draw the rectangle..
That's all. the text file shows the 4 lat/long corner coordinates of the rectangle.
example of the text file.
39.904861, -74.33508
39.866925, -74.186765
39.592326, -74.357053
39.617719, -74.477903

another solution, 4 edit text boxes for the coordinates may be a good idea so I can fill then each time with the lat/long variables and then click a button so the rectangle appears on google maps
 
Upvote 0

Magma

Expert
Licensed User
Longtime User
Thank you Magma
the solution I am looking for is a little different. I will supply the text file with the coordinates. the app will read my text file and then draw the rectangle..
That's all. the text file shows the 4 lat/long corner coordinates of the rectangle.
example of the text file.
39.904861, -74.33508
39.866925, -74.186765
39.592326, -74.357053
39.617719, -74.477903

another solution, 4 edit text boxes for the coordinates may be a good idea so I can fill then each time with the lat/long variables and then click a button so the rectangle appears on google maps
My first post I think ? is the same thing.. the second shows a more detailed solution I ve used for my app.. with draw option.. but first I think is the same u want..

It is easy to edit the code I ve posted, to read from textboxes or file...
If you want I can write it (Monday) for you. PM me..

B4X:
'1st point
latlst.add(39.904861) 'replace the direct numbers with variables read from file or with values from  textboxes. 
lonlst.add(-74.33508)
'2nd point
latlst.add(39.866925)
lonlst.add(-74.186765)
'3rd point
latlst.add(39.592326)
lonlst.add(-74.357053)
'4th point
latlst.add(39.617719)
lonlst.add(-74.477903)


 
Last edited:
Upvote 0

Beja

Expert
Licensed User
Longtime User
My first post I think ? is the same thing.. the second shows a more detailed solution I ve used for my app.. with draw option.. but first I think is the same u want..

It is easy to edit the code I ve posted, to read from textboxes or file...
If you want I can write it (Monday) for you. PM me..

B4X:
'1st point
latlst.add(39.904861) 'replace the direct numbers with variables read from file or with values from  textboxes.
lonlst.add(-74.33508)
'2nd point
latlst.add(39.866925)
lonlst.add(-74.186765)
'3rd point
latlst.add(39.592326)
lonlst.add(-74.357053)
'4th point
latlst.add(39.617719)
lonlst.add(-74.477903)

Thank you Magma,
I would appreciate it if you have a small project by Monday.. without multiple options.. just a rectangle drawn from a text file 4 points (corners). it's that simple.
I hope this is a beginning of our cooperation on bigger projects.
 
Upvote 0

Beja

Expert
Licensed User
Longtime User
Hi friends
Pls note.. I don't want to use inside or outside polygon events.. I want to just draw the polygon and that's it. Draw polygon on Google maps.
 
Upvote 0

Magma

Expert
Licensed User
Longtime User
B4X:
dim pollst as list

...

sub yoursub

dim pol As MapPolygon
Dim latlst As List
Dim lonlst As List

pollst.initialize
latlst.initialize
lonlst.initialize

' I will replace following code with file readstring the following code at my office PC..  to read every line different and regex comma to have lat lng  as double values... now I am writing from my mobile 
'--‐--------------------------------------
'1st point
latlst.add(39.904861)
lonlst.add(-74.33508)
'2nd point
latlst.add(39.866925)
lonlst.add(-74.186765)
'3rd point
latlst.add(39.592326)
lonlst.add(-74.357053)
'4th point
latlst.add(39.617719)
lonlst.add(-74.477903)
'--‐--------------------------------------

        For c=0 To latlst.Size-1
            Dim newposition As LatLng
            newposition.Initialize(latlst.Get(c),lonlst.Get(c))
            pollst.Add(newposition)
        Next
        pol=gmap.AddPolygon(pollst, 0.5,fx.Colors.Red,fx.Colors.Red,0.1)
end sub


MAYBE seems complicated... but this is the code.. to complete drawing the follow polygon
B4X:
pol=gmap.AddPolygon(pollst, 0.5,fx.Colors.Red,fx.Colors.Red,0.1)
 
Upvote 0

Harris

Expert
Licensed User
Longtime User


Finding a point that is within a polygon.... if helpful....
This is a major function that runs every second within one of my apps to determine where the current user is located - and display on screen where he is...
Required because he/she could be in fog or a snowstorm (limited visibility) and has to advise other users of the road their current location to avoid collision on a narrow mine haul road.

Thx
 
Upvote 0

Beja

Expert
Licensed User
Longtime User


Finding a point that is within a polygon.... if helpful....
This is a major function that runs every second within one of my apps to determine where the current user is located - and display on screen where he is...
Required because he/she could be in fog or a snowstorm (limited visibility) and has to advise other users of the road their current location to avoid collision on a narrow mine haul road.

Thx

Thank you
As explained.. The goal is simply to draw a rectangle on a Google map.. That's all.
The coordinates of the rectangle are imported to the app from a text file.
 
Upvote 0

Magma

Expert
Licensed User
Longtime User
Hi there...
you are ready...

Hope this working for you - if need anything - tell me...

It is reading from a (txt) file but also gives you the "option to edit/save:" (this can comment if you don't need it) + draw the polygon...

d1.jpg
d2.jpg
 

Attachments

  • gmap-drawpoly.zip
    4.4 KB · Views: 148
Last edited:
Upvote 0

Beja

Expert
Licensed User
Longtime User
Hi there...
you are ready...

Hope this working for you - if need anything - tell me...

It is reading from a (txt) file but also gives you the "option to edit/save:" (this can comment if you don't need it) + draw the polygon...

View attachment 129732 View attachment 129731

Thanks so much for this.. a lib is missing.. preferencesdialog .. will look for it in the forum.
here's the log:

Main - 66: Unknown member: showdialog
Main - 64: Undeclared variable 'prefdialog' is used before it was assigned any value.
Main - 57: Undeclared variable 'prefdialog' is used before it was assigned any value.
Main - 56: Undeclared variable 'prefdialog' is used before it was assigned any value.
Main - 55: Undeclared variable 'prefdialog' is used before it was assigned any value.
Main - 54: Unknown type: preferencesdialog<br />Are you missing a library reference?
Main - 67: Undeclared variable 'result'. (warning #8)
File 'form.json' is not used. (warning #15)
 
Upvote 0

Magma

Expert
Licensed User
Longtime User
Upvote 0

Beja

Expert
Licensed User
Longtime User
Still looking for preferencesdialog class or library.. all I see now are questions!!
 
Upvote 0
Top