Android Tutorial GPS tutorial

Status
Not open for further replies.
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:
B4X:
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)
       Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
       If Result Then CallSubDelayed(Starter, "StartGPS")
   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).
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.
B4X:
'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:
B4X:
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.

Modified example with NMEA listener: https://www.b4x.com/android/forum/t...-not-firing-on-android-10.112140/#post-699351
 

Attachments

  • GPS.zip
    8.1 KB · Views: 7,265
Last edited:

Cableguy

Expert
Licensed User
Longtime User
Is there anyway to send Lat/Lon retrieved from a DB directly to the most common Navigators? (such as TOM TOM,NDRIVE, NAVIGATION from Google, etc)
 

schimanski

Well-Known Member
Licensed User
Longtime User
Hello, this is a very big kapital. I have made it under basic4ppc with creating 2 virtual ports with an external dll. It allows to send gps-data in a stream to every navigation software like tomtom etc. More about it here:

http://www.b4x.com/forum/questions-help-needed/2094-send-data-serial-port-other-application.html#post11375

Under Android it seems to be more difficulty. You can send the data with an intent, but only once, not in a stream or live. I have tried it with a service every second, but it ends in a chaos. More about it here:

http://www.b4x.com/forum/basic4android-updates-questions/7280-starting-geo-uri-intent.html#post41755

Now, we are working at an own Mapviewer to send such data to a map. All about it here:

http://www.b4x.com/forum/basic4android-share-your-creations/7323-openstreetmap-tilemapviewer.html#post41825

I don't know other solutions.

Rgds
 
Last edited:

mistermentality

Active Member
Licensed User
Longtime User
I'm trying to work with gps and seem to be the only one who this doesnt even run for :BangHead:

If I run the original example I get an error message saying:
"An error has occurred in sub: main_gps_gpsstatus (B4A line 43) lblSatellites.Text + "Satellites:" & CRLF
java.lang.RuntimeException: Object should first be initialised (Label). Continue?"

So I wrote a line initialising it and then tried again but all I get is a blank screen.

If I try the replacement code posted earlier in this thread:
B4X:
Sub GPS_GpsStatus (Satellites As List)
  lblSatellites.Text = "Satellites:" & CRLF
  s=0
  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
    If Satellite.UsedInFix=True Then s=s+1
      If s>0 Then 
        Status=s
      Else
        Status="Lost GPS!"
     End If
  Next
End Sub

I get a compile error:
"Compiling code. Error
Error parsing program.
Error description: Only comments are allowed after a continuation character.
Occurred on line: 49
lblSatellites.Text = lblSatellites.Text & CRLF & Satellite.Prn & _
" " & Satellite.Snr & " " & Satellite.UsedInFix & " " & Satellite.Azimuth _ & " " & "

So I take away the _ characters and it still wont run until the whole split lines are one long one but then another blank screen.

So have I misunderstood the code or should it be showing me something on screen on my phone with gps enabled?

I know I am a newb but all I did was copy and paste the code to try learning and can't get it doing anything :sign0104:

Dave
 

SeaDooFan

Member
Licensed User
Longtime User
:sign0085: oops, got a problem, this code shows me only 3 satellites, all as 'false', for example "5 9 false 260 53" and does not show any lat or lon. any ideas what could be wrong?

Thanks!

P.S. google maps works and show my location correctly.
 

fabero

Member
Licensed User
Longtime User
Hi All,
I'm trying to use this example, but instead a Label, I want use a Scrollview, to get information in tabular mode.

B4X:
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

For example I want catch true or false in usedinfix to make the row green or red. Can someone explain me more?

Thanks...
 

timo

Active Member
Licensed User
Longtime User
I hope this could be helpful to someone.
To calculate the distance between Point A and Point B you can use this formula, where geo positions are in Radiant (Deg * Pi /180):

arccos(cos(LonA-LonB)*cos(LatA)*cos(LatB)+sin(LatA)*sin(LatB))

The result is a distance in radiant. To translate it in Kilometers you just multiple *6360
 
Last edited:

timo

Active Member
Licensed User
Longtime User
An example for translating Deg to decimalDEg and then to radiant:

Lat N 46.57.46.3

deg 46 = 46 = 46.00
min 57 = 57/60 = 00.95
sec 46.3= 46.3/3600 = 00.01286111

SUM = 46.96286111

Radiant= 46.96286111*Pi/180 = 0.819656382

(Lat N and Lon E are positive - Lat S and Lon W negative)
 
Last edited:

profzamyusuf

New Member
hei

I'm still beginner for basic4android....I've tried GPS in sdk-Manager...why did the numbers of longitude,latitude,and others not shown in the emulator??

thank you....
 

yuhong

Member
Licensed User
Longtime User
Not with activities. In the future there will be support for services which support background processing.

This is what I need to function in the service to obtain GPS information.:sign0085:
 

Cor

Active Member
Licensed User
Longtime User
example needed to write gps location to image file

Has someone a small example how to write the latlon to an image file

with jpgutils and the camera

B4X:
Dim exif As ExifData  'jpgutils

Sub Camera1_PictureTaken (Data() As Byte)
    Dim latlon(2) As Float
    Dim latstr As String
      
camera1.StartPreview
   
   Dim out As OutputStream
    out = File.OpenOutput(File.DirRootExternal, "Image.jpg", False)
    out.WriteBytes(Data, 0, Data.Length)
    out.Close
   exif.Initialize(File.DirRootExternal, "Image.jpg")

Sub GPS_LocationChanged (Location1 As Location)
   'how to write in exit tags?
    exif.TAG_GPS_LATITUDE
end sub


thanks in advance
 

neelmon

Member
Licensed User
Longtime User
Not Showing Location

Hi There,

I am new here.

I tried to run the GPS code in my cell, buy its not showing any location related info. Please check the attached image. Can you please tell me the probable solution.

gps.png
 

neelmon

Member
Licensed User
Longtime User
Thanks for your reply, but how other GPS based systems like GMap, Sygic etc are working in the same device?
 
Status
Not open for further replies.
Top