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)

 

Attachments

  • gpsexample.zip
    7.4 KB · Views: 226
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
Cookies are required to use this site. You must accept them to continue using the site. Learn more…