Android Question Getting GPS coordinates just once

Sub7

Active Member
Licensed User
Longtime User
Hello, i am looking for a library that allow to get GPS coordinates on click of a button.
While gps tutorial from @Erel for me is working, it seems i am not able to stop the event locationchanged with gps.stop
I need to get the coordinate only on click, not if i move.
This is how i start the gps: GPS1.Start(0, 0)

Thanks
 

DonManfred

Expert
Licensed User
Longtime User
You must do something wrong i suppose...
Note that in erels example the
B4X:
GPS1.Start(0, 0) 'Listen to GPS with no filters.
is inside the Activity_Resume so if your activity comes to front the gps is running again (and firing events)

remove the line from activity_resume and put a button to start gps.

Try to MANUALLY start gps on button-click.
Then wait for the locationchanged-events to fire.
Call
B4X:
GPS1.Stop
and watch the event firing or not (put log-commands)

Copying updated assets files (1)
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
16:01:02: Activity_Resume -> GPS.Start
16:01:03: GPS_GpsStatus()
[..]
16:03:07: GPS_GpsStatus()
16:03:07: GPS_LocationChanged((Location) Location[gps 50,833606,6,440591 acc=97 et=+17h43m53s17ms alt=119.0 {Bundle[mParcelledData.dataSize=44]}])
16:03:08: GPS_LocationChanged((Location) Location[gps 50,833621,6,440553 acc=92 et=+17h43m53s946ms alt=119.0 {Bundle[mParcelledData.dataSize=44]}])
16:03:23: GPS_LocationChanged((Location) Location[gps 50,833619,6,440531 acc=94 et=+17h44m8s952ms alt=131.0 {Bundle[mParcelledData.dataSize=44]}])
16:03:24: GPS_LocationChanged((Location) Location[gps 50,833611,6,440575 acc=90 et=+17h44m9s952ms alt=131.0 {Bundle[mParcelledData.dataSize=44]}])
16:03:25: GPS_LocationChanged((Location) Location[gps 50,833600,6,440592 acc=85 et=+17h44m10s958ms alt=130.0 {Bundle[mParcelledData.dataSize=44]}])
16:03:26: GPS_LocationChanged((Location) Location[gps 50,833585,6,440630 acc=84 et=+17h44m11s967ms alt=130.0 {Bundle[mParcelledData.dataSize=44]}])
16:03:27: GPS_LocationChanged((Location) Location[gps 50,833567,6,440652 acc=93 et=+17h44m12s960ms alt=130.0 {Bundle[mParcelledData.dataSize=44]}])
16:03:33: GPS_LocationChanged((Location) Location[gps 50,833473,6,440782 acc=98 et=+17h44m18s959ms alt=137.0 {Bundle[mParcelledData.dataSize=44]}])
16:03:34: GPS_LocationChanged((Location) Location[gps 50,833461,6,440766 acc=97 et=+17h44m19s893ms alt=137.0 {Bundle[mParcelledData.dataSize=44]}])
16:03:35: GPS_LocationChanged((Location) Location[gps 50,833451,6,440753 acc=91 et=+17h44m21s65ms alt=137.0 {Bundle[mParcelledData.dataSize=44]}])
16:03:36: GPS_LocationChanged((Location) Location[gps 50,833443,6,440739 acc=86 et=+17h44m21s964ms alt=137.0 {Bundle[mParcelledData.dataSize=44]}])
16:03:37: GPS_LocationChanged((Location) Location[gps 50,833437,6,440700 acc=83 et=+17h44m22s971ms alt=137.0 {Bundle[mParcelledData.dataSize=44]}])
16:03:38: GPS_LocationChanged((Location) Location[gps 50,833436,6,440657 acc=82 et=+17h44m23s968ms alt=137.0 {Bundle[mParcelledData.dataSize=44]}])
16:03:39: GPS_LocationChanged((Location) Location[gps 50,833438,6,440621 acc=84 et=+17h44m24s951ms alt=137.0 {Bundle[mParcelledData.dataSize=44]}])
16:03:40: GPS_LocationChanged((Location) Location[gps 50,833435,6,440562 acc=96 et=+17h44m25s985ms alt=138.0 {Bundle[mParcelledData.dataSize=44]}])
16:03:48: GPS_LocationChanged((Location) Location[gps 50,833387,6,440739 acc=98 et=+17h44m33s969ms alt=138.0 {Bundle[mParcelledData.dataSize=44]}])
16:03:49: GPS_LocationChanged((Location) Location[gps 50,833386,6,440677 acc=95 et=+17h44m34s963ms alt=138.0 {Bundle[mParcelledData.dataSize=44]}])
[...]
16:04:13: GPS_LocationChanged((Location) Location[gps 50,833645,6,440786 acc=95 et=+17h44m58s961ms alt=143.0 {Bundle[mParcelledData.dataSize=44]}])
16:04:14: GPS_LocationChanged((Location) Location[gps 50,833662,6,440824 acc=90 et=+17h44m59s965ms alt=143.0 {Bundle[mParcelledData.dataSize=44]}])
16:04:15: GPS_LocationChanged((Location) Location[gps 50,833675,6,440852 acc=97 et=+17h45m0s982ms alt=143.0 {Bundle[mParcelledData.dataSize=44]}])
16:04:48: buttonstop_Click -> GPS.Stop


** Activity (main) Pause, UserClosed = true **
 

Attachments

  • gpsexample.zip
    7.4 KB · Views: 245
Upvote 0

Sub7

Active Member
Licensed User
Longtime User
Hey Manfred, thanks.
That's what i did at begin but no luck: i commented GPS1.Start(0, 0) from Activity resume and put on a click event.
Maybe, if you move even a centimeter the locationchanged event fire again and again?

Good evening.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Maybe, if you move even a centimeter the locationchanged event fire again and again?
could happen sometimes, yes.... BUT if you call gps.stop IT WILL STOP firing events.... See my log. After i clicked on the button i did not get one event till i quit the app after a few minutes of waiting
 
Upvote 0

Sub7

Active Member
Licensed User
Longtime User
This is working on my phone, on a secondary phone the even is triggered different times before stop.
I am not sure but i think the refresh is so fast that make this happen.

Thank you.
 
Upvote 0

Peter Simpson

Expert
Licensed User
Longtime User
This lot is from memory, but it should work...

B4X:
'Your button routine sub
Sub BtnGetLocation_Click
     If GPS1.IsInitialized = False Then GPS1.Initialize("GPS")
     ProgressDialogShow("Retrieving your GPS location, please wait...")
     GPS1.Start(0, 0)
End Sub

'GPS location changed sub
Sub GPS_LocationChanged(Location1 As Location)
     Latitude = Location1.Latitude
     Longitude = Location1.Longitude

     'After you store the current GPS location in declarations Longitude and Latitude, just use the following line.
     GPS1.Stop
End Sub
 
Last edited:
Upvote 0
Top