Android Question Polygon Geofences

attenzi0ne

Member
Licensed User
Longtime User
ok for anyone interested, I have created a library to findout if a given latlng is within a polygon.

everything is built upon warwounds polygon example on this thred:
http://www.b4x.com/android/forum/threads/google-maps-android-v2-tutorial.24415/page-3

B4X:
-
      Dim Point1, Point2, Point3, Point4, Point5 As LatLng
      Dim Points As List
 
      Points.Initialize
 
      '   points must be ordered in a  counterclockwise order
      '   (i found FillColor will fail if not correctly ordered)
 
      Point1.Initialize(52.756, 0.400)
      Points.Add(Point1)
      Point2.Initialize(52.417, 0.753)
      Points.Add(Point2)
      Point3.Initialize(52.638, 1.291)
      Points.Add(Point3)
      Point4.Initialize(52.902, 0.617)
      Points.Add(Point4)
      Point5.Initialize(52.756, 0.400)
      Points.Add(Point5)   '   closes the path

Points is a list of all the coordinates of our polygon

you can now use my library to check if we are inside or outside of the polygon

B4X:
_
    Dim c_raycast As PolygonRaycast
    Dim current_location As LatLng
   
    current_location.Initialize(52.902, 0.617)
    c_raycast.Initialize
    Log(c_raycast.f_check_current_location(current_location, Points))

it will return true if we are in and false if we are out

I hope it helps someone
 

Attachments

  • polygonraycast.jar
    1.7 KB · Views: 270
  • polygonraycast.xml
    1.5 KB · Views: 297
Last edited:
Upvote 0

Harris

Expert
Licensed User
Longtime User
Dim current_location As LatLng

What type of object is LatLng?
Can I create this without have to load another lib?

I just want to use PolygonRaycast as a stand alone...

Thanks
 
Upvote 0

Troberg

Well-Known Member
Licensed User
Longtime User
The algoritm for finding out if a point is within a polygon is pretty straightforward:

Pick a point that you know is outside the polygon (such as the smallest X and smallest Y, both -1). Then, simply see how many polygon border lines a line between that point and the point you want to test intersects. If it's odd, it's inside, if even, it's outside. Think of it like this: If you walked from the reference point to the test point, each time you jumped over the fence, you would alternatingly jump inside and jump out.

The math to see if two lines intersect is pretty strightforward, but I don't have it in my head. Just google that bit.
 
Upvote 0

Harris

Expert
Licensed User
Longtime User
The algoritm for finding out if a point is within a polygon is pretty straightforward:

Pick a point that you know is outside the polygon (such as the smallest X and smallest Y, both -1). Then, simply see how many polygon border lines a line between that point and the point you want to test intersects. If it's odd, it's inside, if even, it's outside. Think of it like this: If you walked from the reference point to the test point, each time you jumped over the fence, you would alternatingly jump inside and jump out.

The math to see if two lines intersect is pretty strightforward, but I don't have it in my head. Just google that bit.


The math is in the lib in post 4 above. It is derived from the link in post 3 above.
I just wanted to know the object type of LatLng so I could use the lib directly.

I have a project where I shall use a table containing many geo-zones. When in any zone, the speed of the vehicle shall be noted. If speed is above the value set for the zone, a violation record is created.

Thanks
 
Upvote 0

Harris

Expert
Licensed User
Longtime User
B4X:
<root>
  <doclet-version-NOT-library-version>1.00</doclet-version-NOT-library-version>
  <dependsOn>googlemaps</dependsOn>
  <dependsOn>googlemapsextras</dependsOn>
  <dependsOn>polygonraycast</dependsOn>
  <dependsOn>google-play-services</dependsOn>
  <class>
    <name>cyprustech.hunter.polygonraycast</name>
    <shortname>PolygonRaycast</shortname>
    <owner>process</owner>
    <method>
      <name>IsInitialized</name>
      <comment>Tests whether the object has been initialized.</comment>
      <returntype>boolean</returntype>
    </method>
    <method>
      <name DesignerName="Class_Globals">_class_globals</name>
      <returntype>String</returntype>
    </method>
    <method>
      <name DesignerName="f_check_current_location">_f_check_current_location</name>
      <returntype>boolean</returntype>
      <parameter>
        <name>ll_current_location</name>
        <type>anywheresoftware.b4a.objects.MapFragmentWrapper.LatLngWrapper</type>
      </parameter>
      <parameter>

I opened the XML for the lib.
Looks like the lib depends on Google Maps which contains the MapFragment.LatLng wrapper.

I guess (as suggested) I shall write my own routine based on code in link from post 3 above.

Thanks
 
Upvote 0
Top