# Gps Boundary or fence

#### Smee

##### Well-Known Member
Longtime User
Is it possible to take a lat and lon figure and then define a boundary of x radius around those figures?

TIA

#### Erel

##### B4X founder
Staff member
Longtime User
Do you want to check if the current position is inside this region?

#### Smee

##### Well-Known Member
Longtime User

Actually i want to do that and also set a region based on parameters. sort of like you are here and then the surronding area of x metres or x kilometers

#### Erel

##### B4X founder
Staff member
Longtime User
I think that a more concrete example is needed (at least for me...).

#### Smee

##### Well-Known Member
Longtime User
hmmmm,

An analogy :

I am in a suburb called cardiff, and its boundaries have a radius of 5km. my co-ordinates show I am smack in the middle of the suburb. If i move 1 km in any direction i can then tell that i am going to be in another suburb when i travel another 4km in that direction

Or supposing a prisoner is wearing a tracking device and it shows he will be "out of bounds" if he goes another 20metres

Does this make sense?

#### lagore

##### Active Member
Longtime User
I would see 2 options depending on the shape of the boundary, for a circle use the center as the home position then just do a distance calculation to the center home position subtract it from the radius and you have the distance to the edge. For a box it is a bit more complicated, calculate the max & min lat & lon of the box then calculate the distance to the east & west edge using your current position as the first point and your current latitude and the edge longitude as the other position, do the reverse for the north & south edge. Bear in mind that this box does not have straight sides, for small distances and the lower latitudes this should not be a problem, at the poles the box (4 sides) is in fact a triangle but the above process will still work.

#### Smee

##### Well-Known Member
Longtime User
Yes but i will only have GPS co-ords to work with. Is their a conversion that would allow me to do this?

Fortunately it is not a box but more a circle. The distance will probably be user customisable in the end

So to confirm;
If I have a co-ordinate of -32.958694,151.682911, How could i add 2KMS to that and get a circle of 2km around that?

Last edited:

#### warwound

##### Expert
Longtime User
Is the Location object's DistanceTo method of any use?

Martin.

#### Smee

##### Well-Known Member
Longtime User

Yes I looked at that. it will be helpful for checking if the current location is inside the region I think but not for defining the region itself

I am looking at a couple of fomalae that might work but no success yet

#### warwound

##### Expert
Longtime User
Is the attached project any use?
I've created a Circle object library from the code here: Circle.java

It has these methods:

Initialize (Center As Location, Radius As Double)

Initialize the Circle object.
Center is a Location object - you must include the GPS library in your project.
Radius is a distance in meter units.

Area As Double

Returns the area of the circle in units of square meters.

Contains (aLocation as Location) As Boolean

Returns whether this circle object contains aLocation.

Intersects (aCircle As Circle) As Boolean

Return whether this circle object intersects aCircle.

Perimeter As Double

Returns the length of this circle object's perimeter.

A Circle object is not an Activity object - you can declare it in Process_Globals, Globals or locally in a Sub.

Give it a test and if it does what you require but need additional methods post again.

Martin.

#### Attachments

• CircleObject.zip
7.4 KB · Views: 342

#### lagore

##### Active Member
Longtime User
But surly all you have to do is
B4X:
``````Dim HomePosition, CurrentPosition As Location
Dim DistanceToBoundary As Double
HomePosition.Initialize2("-32.958694","151.682911")

DistanceToBoundary = 2000 - HomePosition.DistanceTo(CurrentPosition)   '2000m once the value < 0 the boundary has been crossed``````
this will give you the distance in meters to a 2Km boundary, You do not need to convert the boundary to a coordinate just get the distance from the center point/home position.

#### Smee

##### Well-Known Member
Longtime User
Thanks Martin,

That is much appreciated. I will check it out over the next few days and let you know.

Thanks again

#### Smee

##### Well-Known Member
Longtime User
this will give you the distance in meters to a 2Km boundary, You do not need to convert the boundary to a coordinate just get the distance from the center point/home position.

Hmmmm

Let me re-think my problem and consult my notes. You may be correct and I could have over-analysed my problem. Fortunately I have not started coding yet, I have to get the logic of the problem sorted properly first. However, the home position is not set at any particular location. In fact it is not known until a user says " this is my home position right now at this point in time"

Cheers

UPDATE:

Yes the conversion is needed. I will be using the boundary co-ordinates to pull info from a database of places items etc that fall within those boundary co-ordinates. Unless you see a better solution?

For example Within this boundary there is a church located here, a school there etc

Joe

Last edited:

#### warwound

##### Expert
Longtime User
Using the Circle object, you could create an Array, List or Map of various Circle objects.

Iterating through that collection of Circles to find none, one or more Circles that contain a Location would be simple enough.

The Circle library could be updated with methods to change the Circle center and radius too if required.

Martin.

#### Smee

##### Well-Known Member
Longtime User
Using the Circle object, you could create an Array, List or Map of various Circle objects.

Iterating through that collection of Circles to find none, one or more Circles that contain a Location would be simple enough.

The Circle library could be updated with methods to change the Circle center and radius too if required.

Martin.

Looks like i am going to be busy for a while. I also have to work out the php code and i am an absolute novice at that.

Thanks Very much guys for your help. I hope my coding skills are up to the task of the project I have set myself

#### Harris

##### Expert
Longtime User
I looked around for code to give me the nearest locations from my table of cities with lat / lon when using the GPS. Most seemed very complex. A little experiment produced this:

Dim m as double
m = 0.5

C1 = DefCM.SQL2.ExecQuery2( "SELECT * FROM Location WHERE latitude <= ? and latitude >= ? and longitude >= ? and longitude <= ? order by city, region",Array As String(lat1+m,lat2-m,lon1-m,lon2+m ) )

lat and lon are my current position.

So, by adding and subtracting from my current position .5 of a degree, I create an area (box) to search within. The query returns all locations within this area - a table with 40,000 records and no index - in less than a second.

For each record returned, I then determine which one is THE closest to me, using a method from Tomas GeoQuery lib.

lastloc = geo1.distanceToMidpoint(lat2,lon2,c1.GetDouble("la titude"),c1.GetDouble("longitude"))

#### Smee

##### Well-Known Member
Longtime User
That Looks extremely promising. Thank you very much for sharing this. I am sure it will be of tremendous benefit and probably save me days of work

Thanks again

Joe
:sign0060::sign0060:

Replies
14
Views
815
Replies
4
Views
1K
Replies
2
Views
1K
Replies
9
Views
3K
Replies
50
Views
29K