Android Tutorial GPS tutorial

Discussion in 'Tutorials & Examples' started by Erel, Nov 14, 2010.

  1. Erel

    Erel Administrator Staff Member Licensed User

    This example shows how to work with the GPS library.

    upload_2016-6-23_9-56-49.png

    The GPS object is declared in the Starter service. It is easier to put the GPS in a service and not in an Activity as the services life cycle is simpler.

    When the program starts we check whether the location features are enabled:
    Code:
    Sub Activity_Resume
       
    If Starter.GPS1.GPSEnabled = False Then
         
    ToastMessageShow("Please enable the GPS device."True)
         
    StartActivity(Starter.GPS1.LocationSettingsIntent) 'Will open the relevant settings screen.
       Else
         Starter.rp.CheckAndRequest(Starter.rp.PERMISSION_ACCESS_FINE_LOCATION)
       
    End If
    End Sub
    If not then we open the relevant settings screen.

    The next step is to check for the ACCESS_FINE_LOCATION permission (runtime permissions tutorial).

    The PermissionResult event will be raised with the result:
    Code:
    Sub Activity_PermissionResult (Permission As String, Result As Boolean)
       
    If Permission = Starter.rp.PERMISSION_ACCESS_FINE_LOCATION Then
         
    If Result Then CallSub(Starter, "StartGPS")
       
    End If
    End Sub
    If we have permission then we call the StartGPS sub from the Starter service.

    Now all that is left is to delegate the LocationChanged event from the service to the activity and show the information.
    Code:
    'Starter service
    Sub GPS_LocationChanged (Location1 As Location)
       
    CallSub2(Main, "LocationChanged", Location1)
    End Sub

    'Main activity
    Public Sub LocationChanged(Location1 As Location)
       lblLat.Text = 
    "Lat = " & Location1.ConvertToMinutes(Location1.Latitude)
       lblLon.Text = 
    "Lon = " & Location1.ConvertToMinutes(Location1.Longitude)
       lblSpeed.Text = 
    $"Speed = $1.2{Location1.Speed} m/s "$
    End Sub
    Due to Google policy change you also need to add this line to the manifest editor:
    Code:
    AddManifestText(<uses-feature android:name="android.hardware.location.gps"/>)
    This will prevent the app from being installed on devices without GPS hardware.

    The example is attached.
     

    Attached Files:

    • GPS.zip
      File size:
      8.1 KB
      Views:
      2,388
    Last edited: Sep 21, 2016
    Devan, T201016, Mashiane and 10 others like this.
  2. schimanski

    schimanski Well-Known Member Licensed User

    Result of bearingto is negativ

    Hello!

    I have calculate the bearing from my own position to the target-position with

    Code:
    Kurs=GPS1.BearingTo(Target)
    The result of Kurs is negativ and not equal with the real bearing. :confused::confused:
     
    George Adeniyi Dan likes this.
  3. Erel

    Erel Administrator Staff Member Licensed User

    GPS1 is a location object? How did you initialize the Target?
    Can you post the values of GPS1, Target and Kurs.
    Code:
    Log(GPS1)
    Log(Target)
    Log(Kurs)
     
  4. schimanski

    schimanski Well-Known Member Licensed User

    Hello Erel!

    Is it possible, that the bearing is not shown in 0-360°? When it is shown from 0-180 and -180 to 0°, I think it is correct...:sign0013:
     
  5. Erel

    Erel Administrator Staff Member Licensed User

    This is probably the case here.
     
  6. peacemaker

    peacemaker Well-Known Member Licensed User

    So, isn't possible programmatical on\off (connect\disconnect) of Android's GPS subsystem (like in WM 5.x-6.x)? Without manual user's choice.
    I mean periodical GPS auto-connect by a software only.
     
  7. agraham

    agraham Expert Licensed User

    No, it's not possible. As Erel stated above.
     
  8. Erel

    Erel Administrator Staff Member Licensed User

    You can't enable or disable the GPS device. However if it is enabled it will turn on when it needed and off when it is not needed (to save battery).
     
  9. peacemaker

    peacemaker Well-Known Member Licensed User

    Ha, so this is enough. Just an extra manual permission from a user, i see.
    Thanks, Erel.
    Now just interesting about background running application and switching the screen off, again for battery economy.
     
    Last edited: Dec 19, 2010
  10. ZJP

    ZJP Active Member Licensed User

    Hi,

    Good work ;) but what about the NMEA ? ( hxxp://developer.android.com/reference/android/location/GpsStatus.NmeaListener.html )
    How to get those frames? :confused:

    Thx
     
    Last edited: Dec 22, 2010
  11. Erel

    Erel Administrator Staff Member Licensed User

    The raw sentences are not exposed for now.
     
  12. ashrafidkaidek

    ashrafidkaidek Member Licensed User

    Why when I try this code no message shows up when pressing the home button?

    Code:
    Sub Activity_Pause (UserClosed As Boolean)

       
    Dim Results4 As Int
       Results4 = 
    Msgbox2("Just a msg""Try","OK"""""LoadBitmap (File.DirAssets,"Signal.png"))
       
    If Results4 = DialogResponse.POSITIVE Then
       
    End If
       
       GPS1.Stop
       Awake.ReleaseKeepAlive

    End Sub
    Thanks
     
  13. Erel

    Erel Administrator Staff Member Licensed User

    You cannot show a msgbox when the activity gets paused. It is not possible to suspend the pausing process (the msgbox is automatically dismissed).
     
  14. ashrafidkaidek

    ashrafidkaidek Member Licensed User

    Thanks Erel, I have noticed one other thing that dose not make since to me:
    I have created a small GPS app following your example above; (just to understand the process correctly). What I have tried to do is to make the app shows all the information to the user (Lat, Lng. …) and do some other tasks in the background (track the user movement).
    What I have noticed that while the app is running everything works fine until the user gets a phone call which forces my app to pause, and force the GPS to turn off, therefore I changed the code as shown below:

    Code:
    Sub Activity_Pause (UserClosed As Boolean)

    End Sub
    Note: I have added an if statement to the “Activity_Resume” sub to insure that I’m not calling GPS1.Start while the GPS is already on)

    Now when the app being pushed to the background because of a phone call (or maybe the user decided to play music or surf the Internet) the GPS will stay on, but the application somehow will be paused (the tracking function is not recording anything while the application is in the background)

    Is this issue due to Android hardware limitation? If yes then how come you can play music and surf the Internet at the same time?

    Thank you all
     
    Last edited: Jan 11, 2011
    Krept likes this.
  15. Erel

    Erel Administrator Staff Member Licensed User

    No code runs when the activity is paused.
     
  16. ZJP

    ZJP Active Member Licensed User

    That means it is impossible to have a background processing? :confused:

    JP
     
  17. Erel

    Erel Administrator Staff Member Licensed User

    Not with activities. In the future there will be support for services which support background processing.
     
  18. ashrafidkaidek

    ashrafidkaidek Member Licensed User

    Erel,

    If I understand this correctly, this is a Basic4Android limitation, it is not Android operating system limitation, is that right?

    And when you say in the future dose that means the next Basic4Android version?

    Regards
    :sign0148:
     
  19. Erel

    Erel Administrator Staff Member Licensed User

    No, that is not correct. This is Android OS limitation (or actually design).

    It is planned for the next major update.
     
  20. ashrafidkaidek

    ashrafidkaidek Member Licensed User

    Thanks Erel Fair enough
     
Loading...