GPS - Detecting if a phone is in a certain range of a coordinate

mtmoore

Member
Licensed User
Longtime User
Hi all,

am very much a newbie.

How can I detect if my phone is within say a 5 metre range of a coordinate stored within the app?

Really appreciate any advice or examples.

Thanks!
Marcus
 

mtmoore

Member
Licensed User
Longtime User
Erel,

the coordinates would be outside, as an example of what I am trying to achieve:

In the app the coordinates for a shop for instance are stored.

When a person with a phone and the app installed and running, walks within 5 metres of the stored coordinates the app flags up some kind of message.

How could this be achieved?

I obviously don't want to use the EXACT coordinates as phones are not that accurate and you wouldn't want to rely on someone having to stand in the exact location.

Thanks,
Marcus
 
Upvote 0

mtmoore

Member
Licensed User
Longtime User
Only for as long as the user wanted it to run... otherwise of course GPS being on ALL the time will drain the battery...
 
Upvote 0

mtmoore

Member
Licensed User
Longtime User
Erel, thanks for this and I have that up and running.

Question though, what is the units of measurement that DistanceTo returns?

The reason I ask, is that I have used the same co-ordinates of where I am located to where my phone is and used DistanceTo to calculate the distance and the number it returns is 866...?? Is that in centimetres or something? Can't be metres as one post suggested as that would mean I am 866 metres away from myself, almost half a mile..

Thanks,
Marcus
 
Upvote 0

mtmoore

Member
Licensed User
Longtime User
Erel, here is the code... it's just a modified version of an example you have posted.. any hints on what is wrong?

Thanks, really appreciate your patience with this newbie!

Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
Dim GPS1 As GPS
End Sub

Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.
Dim lblLon As Label
Dim lblLat As Label
Dim lblSpeed As Label
Dim lblSatellites As Label
Dim lblDistance As Label
Dim Latitude2, Longitude2 As Double
Dim Location2 As Location

End Sub

Sub Activity_Create(FirstTime As Boolean)
'Do not forget to load the layout file created with the visual designer. For example:
'Activity.LoadLayout("Layout1")
If FirstTime Then
GPS1.Initialize("GPS")
End If
Activity.LoadLayout("1")
End Sub

Sub Activity_Resume
If GPS1.GPSEnabled = False Then
ToastMessageShow("Please enable the GPS device.", True)
StartActivity(GPS1.LocationSettingsIntent) 'Will open the relevant settings screen.
Else
GPS1.Start(0, 0) 'Listen to GPS with no filters.
End If
End Sub

Sub Activity_Pause (UserClosed As Boolean)
GPS1.Stop
End Sub

Sub GPS_LocationChanged (Location1 As Location)

Location2.Initialize2(51.058, -0.326)
lblLat.Text = "Lat = " & Location1.Latitude
lblLon.Text = "Lon = " & Location1.Longitude
lblSpeed.Text = "Speed = " & Location1.Speed
lblDistance.Text ="Distance..." & Location1.DistanceTo(Location2)


End Sub

Sub GPS_UserEnabled (Enabled As Boolean)
ToastMessageShow("GPS device enabled = " & Enabled, True)
End Sub

Sub GPS_GpsStatus (Satellites As List)
lblSatellites.Text = "Satellites:" & CRLF
For i = 0 To Satellites.Size - 1
Dim Satellite As GPSSatellite
Satellite = Satellites.Get(i)
lblSatellites.Text = lblSatellites.Text & CRLF & Satellite.Prn & _
" " & Satellite.Snr & " " & Satellite.UsedInFix & " " & Satellite.Azimuth _
& " " & Satellite.Elevation
Next
End Sub
 
Upvote 0

mtmoore

Member
Licensed User
Longtime User
It almost looks like there's a rogue 8 in front of the number... I tested it out by driving around today.

When I was about 100 metres from the target location it shows 8100.xx When I was about a mile away it jumped up to 81500.xx etc..

Any ideas??

I don't have an 8 in the string being displayed.
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Are you sure that the coordinates are the right ones ?
Location2.Initialize2(51.058, -0.326)

When you are at home what coordinates get you displayed in:
lblLat.Text = "Lat = " & Location1.Latitude
lblLon.Text = "Lon = " & Location1.Longitude
Almost the coordinates above ?

Best regards.
 
Upvote 0

mtmoore

Member
Licensed User
Longtime User
Klaus,

Almost yes... the long and lang shown are longer numbers (13 decimal places as opposed to 3) but still start with:

51.058 and -0.326

Example:

51.05824150610715, -0.3268796391785145

Distance from no. is showing as

866.655 etc....

When you use 3 decimal places the location is out by about 100 metres.. would that affect things this much?

Thanks..
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
I wanted to make sure that your coordinates were correct.
The difference between the two locations with many decimals and only three is 67.269 meters.
This doesn't justify the big difference which is very strange, I don't know where this difference could come from.

Best regards.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…