Android Question GPS LocationChanged Event not triggering

BenF

Member
Licensed User
Longtime User
Hi All,
I am trying to set up a Georeferencing system, to determine the nearest of a set of known locations, or to save a new location. However I am running into a problem getting the GPS_LocationChanged event to fire (ie, it's not firing at all, so far as I can tell). I am running off a Sony Xperia Z (C6603), with Android 4.3, if that makes any difference. Also, Google Maps and other GPS reliant apps run fine.

I receive the message "GPS Activated", and the edittext's change to 'Searching'. However, it doesn't make it to the messages at the start of the GPS_LocationChanged event.

B4X:
Sub Process_Globals
    Dim GPS1 As GPS
End Sub

Sub Globals
    Dim Txt_Site_Lat As EditText
    Dim Txt_Site_Long As EditText
    Dim Btn_Site_GetLoc As Button
    Dim Result As Int
    Dim CurrentLat As String
    Dim CurrentLong As String
End Sub

Sub Btn_Site_GetLoc_Click
    If GPS1.IsInitialized = False Then
        GPS1.Initialize("")
    End If
    If GPS1.GPSEnabled = False Then
        Result = Msgbox2("The GPS needs to be turned on in the phone settings to use this function.","Activate GPS?","Open GPS settings","","Don't open GPS settings",Null)
        If Result = DialogResponse.POSITIVE Then
            StartActivity(GPS1.LocationSettingsIntent)
            Return
        Else If Result = DialogResponse.NEGATIVE Then
            Return
        End If
    Else If GPS1.GPSEnabled = True Then
        GPS1.Start(0,0)
        Log("GPS Activated")
        Msgbox("GPS Activated","")
        Txt_Site_Lat.Text = "Searching"
        Txt_Site_Long.Text = "Searching"
    End If
End Sub

Sub GPS_LocationChanged(Location1 As Location)
    Msgbox("Congrats! Your GPS is working!","")
    Msgbox("Latitude is " & Location1.Latitude,"")
    Msgbox("Longitude is " & Location1.Longitude,"")
    CurrentLat = Location1.Latitude
    CurrentLong = Location1.Longitude
    Txt_Site_Lat.Text = Location1.Latitude
    Txt_Site_Long.Text = Location1.Longitude
    GPS1.Stop
End Sub

I have also set the GPS permission in the ManifestEditor:
B4X:
AddManifestText(
<uses-sdk android:minSdkVersion="4" android:targetSdkVersion="14"/>
<supports-screens android:largeScreens="true"
    android:normalScreens="true"
    android:smallScreens="true"
    android:anyDensity="true"/>)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
'End of default text.

'Add Permissions
AddPermission(android.permission.ACCESS_FINE_LOCATION)
'End Add Permissions

Is this the correct way to set the permissions?

Is there something I have to do to load the AndroidManifest.xml in, or is it included in the compilation?

I have a feeling that I am missing something incredibly obvious, but have no idea what it might be...

Any help would be greatly appreciated.

Cheers,
Ben
 

BenF

Member
Licensed User
Longtime User
You have to add an event name, for example:
B4X:
GPS1.Initialize("myGPS")

...

Sub myGPS_LocationChanged(Location1 As Location)

...

Cheers for the quick response NJ, I'll plug that in and see how it goes.

EDIT:
Apparently the issue is the GPS on my phone, once I turn off mobile network triangulation, it isn't able to get a lock... One problem resolved and another rears it's head... Thanks for pointing out my code error, that would have had me thoroughly confused.

EDIT 2:
Resolved! Apparently the GPS Assistance Data had to be purged and re-downloaded (?). Accessed it through the Service menu (typing *#*#7378423#*#* into the phone dialler), and after a restart it worked straight up.

Cheers,
Ben
 
Last edited:
Upvote 0
Top